|
From: Joerg T. <Joe...@ma...> - 2007-04-27 08:10:47
|
On 04/26/07 17:46, Toli Kuznets wrote: >> 3.) Examples of the custom Initiator classes have a 'System.in' >> statement waiting for a user to hit a key on the keyboard to exit the >> engine. If we want to put this process in the background, this isn't a >> feasible design obviously...so I was curious what people are doing to >> build an engine that can be set as a background process and then when >> brought back to the foreground, how is the engine being stopped >> elegantly so the appropriate logout and disconnect procedures occur? > > I'd be intersted to hear how others approach this problem as well. For > now, we create the socket initiator and FIX listenrers in Spring, and > then the main thread just goes to sleep (we do a new > Semaphore(0).wait()). The OMS is then killed by pressing Ctrl-C if you > run it on the console, or with an /etc/init.d script wrapping a 'kill > -9' on our virtual appliance. Here's the code for reference: > http://trac.marketcetera.org/trac.fcgi/browser/platform/trunk/oms/src/main/java/org/marketcetera/oms/OrderManagementSystem.java#L59 On UNIX, you can send your process into background by a double-fork: You start a background process starter & and this starter process starts your application into the background: starter: application </dev/null >logfile 2>&1 & echo $! >application.pid exit Now this process is a real daemon, ie child of the init process with PID 1. In addition, it is not tight to a terminal by redirecting stdin ("<"), stdout (">") and stderr ( "2>" ). You can also consider to put this in your own process group if you enable the monitor mode in the bash: set -m Then every background process started with "&" is put into his own process group. The PID of the initial process is then the process group id. To kill all processes in this group: kill -$PID (i.e. the negative value) Using the saved "application.pid" you can stop this process using kill $(< application.pid) # bash kill `cat application.pid` # bourne shell This signal is then catched by the shutdown handler (copy from previous posting): Thread shutdownHook = new Thread( "Gateway.shutdownHook" ) { public void run() { shutdown( "Terminated by runtime system" ); } }; Runtime.getRuntime().addShutdownHook( shutdownHook ); Then in the shutdown() method you can do something like this: acceptor.stop(); Hope this helps! I have no idea how to do this using Windows since I simply do not use it for anything. Cheers, Jörg -- Joerg Thoennes http://www.macd.com Tel.: +49 (0)241 44597-24 Macdonald Associates GmbH Geschäftsführer: Roger Macdonald Lothringer Str. 52, D-52070 Aachen Amtsgericht Aachen, HRB 8151, Ust.-Id DE813021663 |