Starting, stopping, and restarting the event gateway

Because an event gateway uses at least one listener thread, it must have start, stop, and restart methods to control the threads. These methods must also maintain the status variable that the Gateway class getStatus method checks, and change its value among STARTING, RUNNING, STOPPING, STOPPED, and FAILED, as appropriate.

The start method

The start method initializes the event gateway. It starts one or more listener threads that monitor the gateway's event source and respond to any messages it receives from the source.

The start method should return within a time-out period that you can configure for each event gateway type in the ColdFusion MX Administrator. If it does not, the ColdFusion MX Administrator has a Kill on Startup Timeout option for each gateway type. If you select the option, and a time-out occurs, the ColdFusion starter thread calls an interrupt on the gateway thread to try to kill it, and then exits.

Note: If the start method is the listener (for example, in a single-threaded gateway), the method does not return until the gateway stops. Do not set the Kill on Startup Timeout option in the ColdFusion MX Administrator for such gateways.

If the gateway uses a configuration file, the start method should load the configuration from the file. Doing so lets users change the configuration file and restart the gateway without restarting ColdFusion. Applications should also load the configuration file in the constructor; for more information, see Class constructor.

In the SocketGateway class, the start method starts an initial thread. (In a single-threaded Gateway, this would be the only thread.) When the thread starts, it calls a socketServer method, which uses the Java ServerSocket class to implement a multithreaded socket listener and message dispatcher. For more information on the listener, see Responding to incoming messages.

public void start()
{
   status = STARTING;
   listening=true;
   // Start up event generator thread
   Runnable r = new Runnable()
   {
      public void run()
      {
         socketServer();
      }
   };
   Thread t = new Thread(r);
   t.start();
   status = RUNNING;
}

The stop method

The stop method performs the event gateway shutdown tasks, including shutting down the listener thread or threads and releasing any resources. The following example shows the SocketGateway stop method:

public void stop()
{
   // Set the status variable to indicate that the server is stopping.
   status = STOPPING;
   // The listening variable is used as a switch to stop listener activity.
   listening=false;
   // Close the listener thread sockets.
   Enumeration e = socketRegistry.elements();
   while (e.hasMoreElements()) {
      try
      {
         ((SocketServerThread)e.nextElement()).socket.close();
      }
      catch (IOException e1)
      {
         // We don't care if a close failed.
         //log.error(e1);
      }
   }
   // Close and release the serverSocket instance that gets requests from the
   // network.
   if (serverSocket != null) {
      try
      {
         serverSocket.close();
      }
      catch (IOException e1)
      {
      }
      //Release the serverSocket.
      serverSocket = null;
   }
   // Shutdown succeeded; set the status variable.
   status = STOPPED;
}

The restart method

In most cases, you implement the restart method by calling the stop method and the start method consecutively, but you might be able to optimize this process for some services. The following code shows the SocketGateway class restart method:

public void restart() {
   stop();
   start();
}

View comments in LiveDocs