// Copyright © 2002 // John M. Thompson // Boulder, Colorado USA // jt@iwaytechnology.com // http://www.iwaytechnology.com // // A limited right to copy this page for // individual (non-commercial) educational // use only is hereby granted. import iwaypublishing.util.IMsgPortManager; import java.io.*; import java.net.*; /********************************************************************** * A server app that requires an orderly shutdown feature. *
*
* Usage (Windows):
*
* Start first Server app (in cmd wdw 1):
*
* > java -classpath . myserver 1789 sample.txt
*
* Start second Server app w/stop (in cmd wdw 2):
*
* > java -classpath . myserver 1789 stop
*
*/
public class myserver
{
/**
* Port number for termination communication.
* 'static' implies we'll have one instance of
* this server per JVM.
*/
private static int msgPort_ = 0;
/**
* IMsgPortManager portManager_ will listen for a second
* instance of myserver attempting to notify us to
* shutdown in orderly fashion. This communication is
* achieved with IMsgPortManager.sendTerminate().
*/
private static IMsgPortManager portManager_ = null;
/** Where 'important work' is 'documented' */
private static PrintWriter out_ = null;
// Optional upper bound on processing iterations
// (demonstration purposes only, not required).
private static final int INTERNAL_MAX_COUNT = 100000;
/*******************************************************************
* @param args String array of command line args - first one is
* port number to monitor or terminate with, and if followed by
* 'stop' (no quotes), attempt to terminate running server.
*/
public static void main( String[] args )
throws IOException, SocketException, InterruptedException
{
// See processArgs() for reasons it may return false
if( ! processArgs( args ) ) return;
// Start up a message port manager
startupPortManager();
// Do work, waiting for termination request;
// when returns, shutdown
go();
shutdownPortManager();
shutdownPrintWriter();
// Hold up app for a response
byte[] response = new byte[ 256 ];
System.out.println( "\nmyserver.main() all done. Press