|
YOUR FEEDBACK
Did you read today's front page stories & breaking news?
SYS-CON.TV SYS-CON.TV WEBCASTS |
MXDJ TOP LINKS YOU MUST CLICK ON ! ColdFusion Rolling Your Own Event Gateway
Writing and using event gateways in CFMX 7
By: Jim Schley; Tom Jordahl
Mar. 31, 2005 12:00 AM
Event gateways are an exciting, new feature in Macromedia ColdFusion MX 7 that arose from one simple idea: that there are many applications out there that aren't part of the Web and don't communicate through the HTTP protocol. These applications are on all types of devices. They run the gamut from the ubiquitous instant messaging clients to SMS on mobile phones to new things that haven't even been invented yet. ColdFusion does a great job powering applications that run on the Web -why not power non-Web applications too? Not only should it be possible for ColdFusion developers to write applications for non-Web applications and devices, but it should be easier to write them in ColdFusion than any other way. ColdFusion MX 7 ships with several exciting types of event gateways that make it easy for you to get off the ground running with things like SMS. The server contains a simple Java API so that you can write your event gateway to connect to just about anything you want. With this extensibility, third-party software vendors can easily provide event gateways so that CFMX can talk to their non-CFMX applications. Requirements Overview of Event Gateways in ColdFusion 7 Among the event gateways provided with ColdFusion MX 7 are: SMS for mobile text messaging; XMPP for open-standard instant messenger networks such as Jabber; Lotus Sametime for enterprise instant messenger communications; and asynchronous CFML for sending requests from your CFML to a CFC for processing in a separate thread. ColdFusion 7 also provides some sample event gateways types (with source code), including JMS for messaging applications that support the J2EE standard; TCP/IP Socket for use with a telnet client to interact with your applications; and Directory Watcher for watching a file system directory and to run your CFC when a user or application creates, edits, or deletes a file in that directory. You create gateway instances from a gateway type. Instances correspond to individual copies of a gateway that are running. This is an object that is started/stopped (through the Administrator). Each gateway instance specifies a CFC to handle incoming messages. You can have more than one instance of an event gateway type, and each instance will have its own configuration. For example, you can have multiple instances of a given gateway type, each with different logins, phone numbers, buddy names, directories to watch, and so forth. New Application Development Paradigm There are two basic types of event gateway applications: initiator applications and responder applications:
The DirectoryWatcherGateway event gateway comes preconfigured on ColdFusion MX 7 as an example to help you build your first CFML gateway application. The Java source code is also available for you to learn how to write your own Java gateway. This event gateway watches a directory for changes. Based on your configuration file, which specifies the directory you want to watch and file system events you want to watch for (such as file creation, deletion, and changes), you can also send event objects to your CFC with the information when any event occurs. In the following example, a CFC processes events using the Directory WatcherGateway event gateway:
<cffunction name="onAdd" output="no">
<cfargument name="CFEvent"
type="struct">
<!--- get event data --->
<cfset data=CFEvent.data>
<!--- log a message --->
<cflog file="watch"
text="a file was #data.type#
and " &
"the name was #data.file
name#">
<!--- watcher will ignore outgoing
messages --->
</cffunction>
The code is pretty simple - the function takes the event object as an argument and treats it like a simple CFML struct datatype. The data key of the struct contains a message that describes the file system event. The two entries in this struct are as follows:
Detailed Look at the ColdFusion Event Structure
The instant messenger (IM) event gateway event data structure contains the following information:
In the event object, use the gatewayType key to identify the event gateway type sending the message to the CFC. This is useful for a CFC that handles input from multiple event gateway types. For instance, you might have an application that communicates with mobile phones using SMS and Lotus Sametime IM clients. Because there are two different event gateway types sending input to your application, each with its own version of the event object, it's best to evaluate the value of the gatewayType key before processing the incoming data. The sendGatewayMessage Function endGatewayMessage (gwid, data)
<cfset data = structNew()> <cfset data.message = "hello"> <cfset data.buddyId = "cfguy"> When sending the message through the XMPP event gateway, use the following syntax:
<cfscript>
sendGatewayMessage("XMPP
server1",data);
</cfscript>
What to Specify in Outgoing Data The SMS event gateway has a long list of optional data items that an outgoing event object can contain. The optional data items correspond to specific options in the SMPP protocol, which communicates SMS messages on mobile carrier networks. The ColdFusion team did our best to include every single option in the SMPP specification, even though different carriers implement the specification differently. For instance, you can optionally set a registeredDelivery data item in your outgoing event object to request delivery receipt for your SMS. The getGatewayHelpers Function Use the following code syntax: getGatewayHelper (gwid)
The following code example adds a new buddy with the user ID, cfuser2, to the server user's buddy list:
<cfscript>
imhelper=getGatewayHelper("XMPP_
server1");
imhelper.addBuddy("cfuser2");
</cfscript>
Instant Message Example The following is an example of what your configuration file for the XMPP event gateway might look like: # XMPP Instant Message Configuration file userid=yourid@jabber.org password=yourpassword # everything else is OK to default serverip=jabber.org serverport=5222 retries=5 retryinterval=10 onInComingMessageFunction= onIncomingMessage onAddBuddyRequestFunction= onAddBuddyRequest onAddBuddyResponseFunction= onAddBuddyResponse onBuddyStatusFunction=onBuddyStatus onIMServerMessageFunction= onIMServerMessage You also specify the listener CFC. The following is an example of a simple CFC that echoes an instant message sent to it with the original text:
<cfcomponent>
<cffunction name="onIncomingMessage"
output="no">
<cfargument name="CFEvent"
type="struct" required="yes">
<!--- Get the message --->
<cfset data=cfevent.DATA>
<cfset message="#data.message#">
<!--- where did it come from? --->
<cfset orig="#CFEvent.
originatorID#">
<!--- make a struct to return --->
<cfset retValue = structNew()>
<cfset retValue.message
= "Your message: " & message>
<cfset retValue.buddyId = orig>
<!--- send the return message
back --->
<cfreturn retValue>
</cffunction>
</cfcomponent>
Once you have your configuration file and CFC set up (see Figure 2), you're ready to create the event gateway instance in the ColdFusion Administrator. After you start the event gateway instance (see Figure 3), you're ready to start using the IM application. Rolling Your Own Event Gateway You must know Java to write an event gateway, but you can start with the documented sample code. Also check out of the chapter on ColdFusion MX Developer's Guide: Creating Custom Event Gateways in the documentation; it walks you through all the necessary details to create and run your own event gateway type. Event gateway applications are easy to write once you understand the programming model available to you. Once you realize that your ColdFusion applications do not depend on an HTTP request/response paradigm, you'll see how event gateways open up a whole new world of possibilities for you. ColdFusion is no longer confined to the Web. LATEST FLEX STORIES & POSTS
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||