0

I have the following code:

public class LogWriter implements Runnable {
private static BlockingQueue<LogRecord> logQueue;

static {
    logQueue = new ArrayBlockingQueue<LogRecord>(30);
}

@Override
public void run() {
    Integer errorNo = 0;
    configureLogger();

    while (true) {
        try {
            LogRecord record = logQueue.take();
            consumeLogRecord(record);
            System.out.println(++errorNo + " - Logged error in file '" + LoggerConfig.LOG_PATH + "'");
            record = null;
        } catch (InterruptedException e) {
            System.out.println(e.getMessage());
        }
    }
}
}

This is part of a logger for a LibreOffice pluggin written in Java. When LibreOffice is closing, it simply kills it's plugins (as I can tell so far, not sure of it), but not before sending a signal to them that it is closing, which I can detect in my code (through the UNO API). After I receive the termination signal from LibreOffice, I want to flush my LogRecord queue to the log file and change that while(true) to false so the method run() can finish appropriately, releasing the resources it have. So my question is, how can I tell the JVM that waiting for this operation is of high priority and it shouldn't terminate before finishing it?

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Bruno Finger
  • 2,105
  • 3
  • 27
  • 47
  • http://stackoverflow.com/questions/5747803/running-code-on-program-exit-in-java – MariuszS Jan 27 '14 at 17:07
  • Shutdown hooks are the best approach - you can't *force* them to always work per-se, but in general use they're the best tool you have for this job. – Michael Berry Jan 27 '14 at 17:09
  • If you know how to detect the shutdown signal in your code... what exactly is the problem? – TwoThe Jan 27 '14 at 20:36
  • The problem was that even though LibreOffice says that it is shutting down, it wouldn't wait for the code to do something about it, like closing files, releasing resources... That's why I asked if there's a way to keep the JVM alive until this piece of code finishes. – Bruno Finger Jan 28 '14 at 01:14

2 Answers2

2

The advice about shutdown hooks must be taken with a large grain of salt. The shutdown hook is a last resort device where you can try to salvage what you couldn't possibly by any other means. You can't rely on any normal assumption, such as that System.out is still open, that your log file is still open, that even the filesystem is available, and so on.

A use case for a shutdown hook is to try to gracefully close acquired resources, with no attempt at further data transfer.

The approach you should take is:

  1. inform yourself exactly what terms LibreOffice gives you: do you have a certain timeout within which to complete your work?

  2. minimize the work pending at any point in time, thereby maximizing your chance to have it completed within the timeout.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • Thanks, the shutdown hook is a nice tip, I added it as a last resort as you said, but during correct flow of the shutdown process, the solution now is to Java capture the shutdown signal from LibreOffice, do not allow any more logs to be added to the queue and flush the logger, thus finishing the plugin. – Bruno Finger Jan 27 '14 at 18:05
0

You can use.

Runtime.getRuntime().addShutdownHook(Thread);

Shutdown hooks will be the best option to go.

ashokramcse
  • 2,841
  • 2
  • 19
  • 41