poidasmith / winrun4j

WinRun4J Java Application Launcher
http://winrun4j.sourceforge.net
212 stars 63 forks source link

Added AbstractService.shutdownRequest() to allow subclasses to override ... #66

Closed RedTailedHawk closed 6 years ago

RedTailedHawk commented 10 years ago

...and respond to a shutdown request on a thread other than main.

This allows my main thread to wait for a signal that the Windows service is shutting down, instead of polling and sleeping.

So instead of this:

            while (!agentWindowsService.isShutdown()) {
                try {
                    Thread.sleep(6000);
                } catch (InterruptedException e) {
                }

This:

            _blockingQueue = new ArrayBlockingQueue<Boolean>(1);

            try {
                _blockingQueue.take();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

@Override
public void shutdownRequest() {
    try {
        _blockingQueue.put(true);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
neandrake commented 10 years ago

The serviceRequest method can be overridden to provide this functionality already, and is likely preferred to handle it in this manner rather than create a separate function to override. The AbstractService in general is not very useful - we have our own implementations of Service rather than extend AbstractService in order to get similar behavior of triggering events when requested to shutdown.

RedTailedHawk commented 10 years ago

I just thought an overridable event would have been nicer than having to copy and paste the switch statement code from serviceRequest. But that approach is workable.

Thanks for looking at the pull request.