Responding to incoming messages

One or more listener threads respond to incoming messages (events). The threads must include code to dispatch the messages to ColdFusion event gateway services, as follows:

  1. Create a CFEvent instance.
  2. Create a Map instance that contains the message and any other event gateway-specific information, and pass it to the CFEvent setData method.
  3. Call the CFEvent setOriginator method to specify the source of the message. (This is required if the ColdFusion application will send a response.)
  4. Call the CFEvent setGateWayType method to specify the event gateway type.
  5. Set any other CFEvent fields where the default behavior is not appropriate; for example, call the setCFCPath method to replace the default listener CFC. (For information on default CFEvent fields, see CFEvent class.)
  6. Call the gatewayService.addEvent method to dispatch the CFEvent instance to ColdFusion.
  7. Handle cases where the event is not added to the event gateway service queue (the addEvent method returns False).

If your application sends any messages to multiple listener CFCs, the gateway must create and configure a CFEvent instance and call the gatewayService.addEvent method to send the message to each separate listener CFC. The gateway's setCFCListeners method must make the CFC paths available to the gateway for configuring the CFEvent instances.

If your ColdFusion server carries a heavy event gateway message load, the ColdFusion event gateway services event queue might reach the maximum value set in the ColdFusion MX Administrator. When this happens, the gatewayService.addEvent method returns False and fails. Your code can do any of the following:

The SocketGateway class implements the listener using a java.net.ServerSocket class object and SocketServerThread listener threads. (See the SocketGateway source for the SocketServerThread code.) When the listener thread gets a message from the TCP/IP socket, it calls the following processInput method to dispatch the message to ColdFusion. This method explicitly sets all required and optional CFEvent fields and sends the event to ColdFusion. If the addEvent call fails, it logs the error.

Note: Much of the processInput method code supports multiple listener CFCs. A gateway that uses only a single listener cfc, would require only the code in the latter part of this method.

private void processInput(String theInput, String theKey)
{
   // Convert listeners list to a local array
   // Protect ourselves if the list changes while we are running
   String[] listeners;
   int size = cfcListeners.size();
   if (size > 0)
   {
      // Capture the listeners list
      synchronized (cfcListeners)
      {
         listeners = new String[size];
         cfcListeners.toArray(listeners);
      }
   }
   else
   {
      // Create a dummy list
      listeners = new String[1];
      listeners[0] = null;
   }
   // Broadcast to all the CFC listeners
   // Send one message at a time with different CFC address on them
   for (int i = 0; i < listeners.length; i++)
   {
      String path = listeners[i];
      CFEvent event = new CFEvent(gatewayID);
      Hashtable mydata = new Hashtable();
      mydata.put("MESSAGE", theInput);
      event.setData(mydata);
      event.setGatewayType("SocketGateway");
      event.setOriginatorID(theKey);
      event.setCfcMethod(cfcEntryPoint);
      event.setCfcTimeOut(10);
      if (path != null)
         event.setCfcPath(path);
         boolean sent = gatewayService.addEvent(event);
      if (!sent)
         log.error("SocketGateway(" + gatewayID + ") Unable to put message on
            vent queue. Message not sent from " + gatewayID + ", thread " + theKey
            + ".  Message was " + theInput);
   }
}

View comments in LiveDocs