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:
setData method.
setOriginator method to specify the source of the message. (This is required if the ColdFusion application will send a response.)
setGateWayType method to specify the event gateway type.
setCFCPath method to replace the default listener CFC. (For information on default CFEvent fields, see CFEvent class.)
gatewayService.addEvent method to dispatch the CFEvent instance to ColdFusion.
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:
getQueueSize and getMaxQueueSize methods, and retry the addEvent method when the queue size is less than the maximum.
getLogger method. (For more information, see Logging events and using log files.)
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);
}
}