logging - Closing Java FileHandler when quitting a routine that runs as a service -


i have little routine schedules timer that:

timer.schedule(new myroutine(), 1000, 60000); 

in run() method of myroutine logger opened , filehandler attached it:

filehandler fh = new filehandler("app.log"),true); logger.addhandler(fh); 

since there no real exit point application (as runs service), have no chance close , remove logger´s filehandler, hence file lock (app.log.lck) remains, resulting in new logfile (app.log.1) on next start of service.

is there way ensure filehandler closed , removed?

you add shutdown hook this:

in method schedules timer:

public void schedulefiletask() {     final filehandler fh = new filehandler("app.log"),true);     runtime.getruntime().addshutdownhook(new thread() {         @override         public void run() {             fh.close();         }     });     timer.schedule(new myroutine(fh), 1000, 60000); } 

and in myroutine.java

public class myroutine extends timertask {     private final filehandler handler;      public myroutine(filehandler hander) {         this.handler = handler;     }      public void run() {         // use handler here          handler.dosomething();     } } 

for more information can look java docs


Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -