Object.finalize()
deprecated on Java 9, and I think I understand the reasons, but I am having problems with how to replace it.
I have a utility class called Configuration, which essentially has one instance that owns everything in the application and lasts for the duration of the application. One of the services it provides is registration: the first request for registering a message creates a log (for various well-established reasons, its own Logger, and not the standard one), with a link stored in the field of the Configuration object, and when the application terminates, normal or abnormal, I want to free up any resources stored in the log (this is a black box, as users of my library can provide their own implementation).
Currently, this is achieved using a method Configuration.finalize()
that calls logger.close()
.
What should I do instead?
source
share