diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2018-09-15 23:40:30 +1200 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2018-09-15 23:40:30 +1200 |
commit | 089e7262ab9395d0530c375b8f6cfcff35f45798 (patch) | |
tree | 28afcdd22f0fdc4d3b5d776851928db169ee5751 | |
parent | 22e360f7dd6ad6ee89896340123f85bd2537714a (diff) | |
download | trackermap-server-089e7262ab9395d0530c375b8f6cfcff35f45798.tar.gz trackermap-server-089e7262ab9395d0530c375b8f6cfcff35f45798.tar.bz2 trackermap-server-089e7262ab9395d0530c375b8f6cfcff35f45798.zip |
Improve service handling
-rw-r--r-- | src/org/traccar/Main.java | 7 | ||||
-rw-r--r-- | src/org/traccar/WindowsService.java | 16 |
2 files changed, 22 insertions, 1 deletions
diff --git a/src/org/traccar/Main.java b/src/org/traccar/Main.java index 73b18c139..48f2323ba 100644 --- a/src/org/traccar/Main.java +++ b/src/org/traccar/Main.java @@ -120,6 +120,13 @@ public final class Main { } }, 0, CLEAN_PERIOD); + Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() { + @Override + public void uncaughtException(Thread t, Throwable e) { + LOGGER.error("Thread exception", e); + } + }); + Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { diff --git a/src/org/traccar/WindowsService.java b/src/org/traccar/WindowsService.java index c1a5a1017..263d75a0e 100644 --- a/src/org/traccar/WindowsService.java +++ b/src/org/traccar/WindowsService.java @@ -36,6 +36,8 @@ public abstract class WindowsService { private static final Advapi32 ADVAPI_32 = Advapi32.INSTANCE; + private final Object waitObject = new Object(); + private String serviceName; private ServiceMain serviceMain; private ServiceControl serviceControl; @@ -186,12 +188,21 @@ public abstract class WindowsService { run(); + try { + synchronized (waitObject) { + waitObject.wait(); + } + } catch (InterruptedException ex) { + } + reportStatus(Winsvc.SERVICE_STOPPED, WinError.NO_ERROR, 0); // Avoid returning from ServiceMain, which will cause a crash // See http://support.microsoft.com/kb/201349, which recommends // having init() wait for this thread. // Waiting on this thread in init() won't fix the crash, though. + + System.exit(0); } } @@ -203,7 +214,10 @@ public abstract class WindowsService { case Winsvc.SERVICE_CONTROL_STOP: case Winsvc.SERVICE_CONTROL_SHUTDOWN: reportStatus(Winsvc.SERVICE_STOP_PENDING, WinError.NO_ERROR, 5000); - System.exit(0); + synchronized (waitObject) { + waitObject.notifyAll(); + } + break; default: break; } |