Class ServiceWrapper

java.lang.Object
com.iizix.server.tools.ServiceWrapper

public final class ServiceWrapper extends Object
The Java service wrapper of a Windows service (CC-SERVER-RESTART-UPDATE-V5, after NetPhantom's StartService with its defects fixed): installed by bin\ntservice\is.bat as the process SrvAny runs, it starts the server and supervises it with the exit-code contract of com.iizix.server.ServerExit as Windows reads it, the same as iiziServer.bat:
  • 0, 8, 9: no relaunch, the wrapper exits with the server's code (SrvAny reports it);
  • 100: relaunch at once;
  • 101: the updater first (ServerUpdater, from this very jar: the updater copies it to a temporary file itself), honouring its exit code - 0 updated, 1 refused or rolled back (the previous version is relaunched and the log says so), 2 or more the rollback failed: the wrapper exits 1 and the installation needs the operator;
  • anything else, a crash, and 1 as well: on Windows a JVM crash (hs_err), an uncaught OutOfMemoryError and a kill (TerminateProcess, Task Manager's End task) all end the process with 1, which on POSIX is the code of a fatal error (measured on JDK 8 to 26, CC-NP-UPGRADE-FOLLOWUP NP-F-1; CC-FIX-WRAPPER-RELAUNCH): a relaunch after 10000L ms, at most 5 times within 300000L ms, then the wrapper exits with the last code (SrvAny reports a clean stop whatever the code, so the Windows recovery never triggers: the back-off is here, D-7).
Before every start an apply the last update left interrupted is rolled back (ServerUpdater -rollback, which does nothing without its marker).

The graceful stop (stage-native of the order): the new SrvAny writes the file "service.stop" into the installation on a service STOP and waits; the wrapper sees it, writes "server.stop", which the server watches (ServiceStopWatcher) and answers with its own stop (exit 0 through its dispose), and does not relaunch; a server that has not ended within 90000L ms is destroyed. The old SrvAny kills the tree instead: nothing here runs then, and nothing needs to.

The output. The server's standard output and error go to logs/iiziServer-console.log through ProcessBuilder's redirection (no reader threads); the wrapper's own timestamped lines - start, restart, update, stop, crash - go to logs/iiziServerWrapper.log and, when the event log is on, to the NT Event Log through setEventLogger(BiConsumer). Both files are rotated at 20000000L bytes.

Usage: ServiceWrapper [-install <dir>] [-ntEventLog off|on|info] [-ntEventSource <name>] <javaExe> <java options...> <mainClass> [<args...>] - everything from the Java executable on is the server's command line, to which the wrapper adds -supervisor wrapper and, ONLY when given, the two event-log options. Test options: -updaterClass <class>, -crashBackoffMs <n>, -crashWindowMs <n>, -updaterClassPath <path>.

Nothing here depends on another IIZI class: the wrapper runs from the tools jar, before the server.

Author:
Christopher Mindus
  • Field Details

    • CRASH_BACKOFF_MS

      public static final long CRASH_BACKOFF_MS
      The back-off after a crash, in milliseconds (10 s, D-7).
      See Also:
    • CRASH_RELAUNCHES

      public static final int CRASH_RELAUNCHES
      The relaunches after crashes within CRASH_WINDOW_MS before the wrapper gives up (5).
      See Also:
    • CRASH_WINDOW_MS

      public static final long CRASH_WINDOW_MS
      The window of CRASH_RELAUNCHES, in milliseconds (300 s).
      See Also:
    • STOP_TIMEOUT_MS

      public static final long STOP_TIMEOUT_MS
      How long a server may take to stop after the stop file, before it is destroyed.
      See Also:
    • SERVICE_STOP

      public static final String SERVICE_STOP
      The file SrvAny writes into the installation on a service STOP.
      See Also:
    • SERVER_STOP

      public static final String SERVER_STOP
      The file the wrapper writes for the server, which watches it and stops.
      See Also:
    • WRAPPER_LOG

      public static final String WRAPPER_LOG
      The logs.
      See Also:
    • CONSOLE_LOG

      public static final String CONSOLE_LOG
      The logs.
      See Also:
    • ROTATE_BYTES

      public static final long ROTATE_BYTES
      The size at which a log is rotated to .1.
      See Also:
    • EVENT_ERROR

      public static final int EVENT_ERROR
      The event classes of the NT Event Log (the DLL's): error, warning, informational.
      See Also:
    • EVENT_WARNING

      public static final int EVENT_WARNING
      The event classes of the NT Event Log (the DLL's): error, warning, informational.
      See Also:
    • EVENT_INFORMATIONAL

      public static final int EVENT_INFORMATIONAL
      The event classes of the NT Event Log (the DLL's): error, warning, informational.
      See Also:
  • Method Details

    • setEventLogger

      public static void setEventLogger(BiConsumer<Integer,String> logger)
      Sets the event logger: what receives the wrapper's own lines besides its log file.
      Parameters:
      logger - The logger (event class, message), null for none.
    • main

      public static void main(String[] args)
      The entry: the wrapper's exit code is the server's.
      Parameters:
      args - The options, then the server's command line.
    • run

      public static int run(String[] args, PrintStream out)
      Runs the wrapper and returns its exit code (the test's entry).
      Parameters:
      args - The options, then the server's command line.
      out - The standard output.
      Returns:
      The exit code.