package org.endeavourhealth.hl7receiver;

import org.endeavourhealth.core.data.config.ConfigManagerException;
import org.endeavourhealth.hl7receiver.hl7.HL7Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Main {

	private static final String PROGRAM_DISPLAY_NAME = "EDS HL7 receiver";
	private static final Logger LOG = LoggerFactory.getLogger(Main.class);

	public static void main(String[] args) {
		try {
            Configuration configuration = Configuration.getInstance();

		    LOG.info("--------------------------------------------------");
            LOG.info(PROGRAM_DISPLAY_NAME);
            LOG.info("--------------------------------------------------");

			HL7Service serviceManager = new HL7Service(configuration);
            serviceManager.start();

            LOG.info("Press any key to exit...");

            System.in.read();

            LOG.info("Shutting down...");
            serviceManager.stop();

            LOG.info("Shutdown");
            System.exit(0);

        } catch (ConfigManagerException cme) {
            System.err.println("Fatal exception occurred initializing ConfigManager [" + cme.getClass().getName() + "] " + cme.getMessage());
		    LOG.error("Fatal exception occurred initializing ConfigManager", cme);
            System.exit(-2);
        }
        catch (Exception e) {
			LOG.error("Fatal exception occurred", e);
			System.exit(-1);
		}
	}
}
@@ -382,16 +382,60 @@
 lass);%0A%0A
+%09private static HL7Service serviceManager;%0A%0A
 %09public 
@@ -864,205 +864,112 @@
 fo(%22
-Press any key to exit...%22);%0A%0A            System.in.read();%0A%0A            LOG.info(%22Shutting down...%22);%0A            serviceManager.stop();%0A%0A            LOG.info(%22Shutdown%22);%0A            System.exit(0
+Started succesfully...%22);%0A%0A            Runtime.getRuntime().addShutdownHook(new Thread(() -%3E shutdown())
 );%0A%0A
@@ -1031,26 +1031,27 @@
     
-System.err.println
+printToErrorConsole
 (%22Fa
@@ -1103,64 +1103,14 @@
 ager
- %5B%22 + cme.getClass().getName() + %22%5D %22 + cme.getMessage()
+%22, cme
 );%0A%09
@@ -1325,10 +1325,542 @@
 ;%0A%09%09%7D%0A%09%7D
+%0A%0A%09private static void shutdown() %7B%0A%09    try %7B%0A            LOG.info(%22Shutting down...%22);%0A%0A            if (serviceManager != null)%0A                serviceManager.stop();%0A        %0A%09    %7D catch (Exception e) %7B%0A            printToErrorConsole(%22Exception occurred during shutdown%22, e);%0A%09        LOG.error(%22Exception occurred during shutdown%22, e);%0A        %7D%0A    %7D%0A%0A    private static void printToErrorConsole(String message, Exception e) %7B%0A        System.err.println(message + %22 %5B%22 + e.getClass().getName() + %22%5D %22 + e.getMessage());%0A    %7D
 %0A%7D

Change main thread to support upstart