Comments
bruce.armstrong wrote: Somebody just said it better than I did, and with more chops to say it: Open Letter to Mark Zuckerberg, Sheryl Sandberg & Facebook Mobile
Cloud Expo on Google News


2008 West
DIAMOND SPONSOR:
Data Direct
SOA, WOA and Cloud Computing: The New Frontier for Data Services
PLATINUM SPONSORS:
Red Hat
The Opening of Virtualization
GOLD SPONSORS:
Appsense
User Environment Management – The Third Layer of the Desktop
Cordys
Cloud Computing for Business Agility
EMC
CMIS: A Multi-Vendor Proposal for a Service-Based Content Management Interoperability Standard
Freedom OSS
Practical SOA” Max Yankelevich
Intel
Architecting an Enterprise Service Router (ESR) – A Cost-Effective Way to Scale SOA Across the Enterprise
Sensedia
Return on Assests: Bringing Visibility to your SOA Strategy
Symantec
Managing Hybrid Endpoint Environments
VMWare
Game-Changing Technology for Enterprise Clouds and Applications
Click For 2008 West
Event Webcasts

2008 West
PLATINUM SPONSORS:
Appcelerator
Get ‘Rich’ Quick: Rapid Prototyping for RIA with ZERO Server Code
Keynote Systems
Designing for and Managing Performance in the New Frontier of Rich Internet Applications
GOLD SPONSORS:
ICEsoft
How Can AJAX Improve Homeland Security?
Isomorphic
Beyond Widgets: What a RIA Platform Should Offer
Oracle
REAs: Rich Enterprise Applications
Click For 2008 Event Webcasts
SYS-CON.TV
Top Links You Must Click On


Rolling Your Own Event Gateway
Writing and using event gateways in CFMX 7

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
To complete this tutorial you will need to install the following software and files: ColdFusion MX 7 (www.macromedia.com/cfusion/tdrc/ index.cfm?product=coldfusion&promoid=devcenter_tutorial_product_090903)

Overview of Event Gateways in ColdFusion 7
ColdFusion 7 contains a new subsystem to support event gateways. The gateway types are Java classes that implement an application programming interface (API) provided by ColdFusion. Figure 1 shows the process for event gateway communication. The gateways communicate with the CF Event Gateway Services subsystem through CFEvent objects. The subsystem, in turn, queues the CFEvent object requests and passes them to the ColdFusion runtime engine for processing, as input to ColdFusion components (Listener CFCs). The Listener CFC might return output to the Event Gateway Services subsystem and then back to the event gateway.

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
Getting into the mindset of writing event gateway applications requires a change in thinking from writing traditional Web applications. The paradigm is different because you're no longer tied to the request/response nature of HTTP. Event gateways are asynchronous and can receive and send messages without depending on each other.

There are two basic types of event gateway applications: initiator applications and responder applications:

  1. An initiator application is a ColdFusion application that generates an event message from CFML code and sends the message using a configured event gateway instance. An example of an initiator application is an e-commerce application that sends an SMS notification when a user's order has shipped. The application uses the SMS gateway to send a message, responding to some logic in the CFML code. You can enhance any CFML application to be the initiator. Use the sendGatewayMessage function to send outgoing messages, such as SMS text messages through an event gateway. In this case, you use the function just like the CFMAIL tag.
  2. A responder application is a ColdFusion application that receives an event message that comes from external source through the listening event gateway instance. The event gateway service forwards that event to the listener CFC, and the CFC method returns a response back to the event gateway instance. An example of a responder application is an IM bot that responds to questions. Responder applications are written in CFML and use a CFC as the listener for a gateway. The CFC listens for an event from a given gateway instance and processes the event structure passed to it to return a response. The event structure contains the message, along with some detail about its origin (see Table 1).
Writing an Event Gateway Application The best way to learn about event gateway applications is to dive into the examples that ship with ColdFusion MX 7. Look for these examples under the "gateway" subdirectory in your install directory.

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:

  • filename of the file that changed
  • type of change that happened: add, delete, or change
The event gateway also contains an entry, lastModified, which specifies the time an added or changed file was last modified.

Detailed Look at the ColdFusion Event Structure
Understanding what an event object contains and passes to your CFC as an argument through the event gateway is essential to writing your application. The event object is a Java object that maps to a CFML structure. Each event gateway type puts different data into an event object, so it's important to look at the gateway author's documentation to know what to expect. The CFEvent object includes whatever it is that the event gateway is trying communicate to your application. The event gateways provided with ColdFusion are well documented. The SMS event gateway event data structure contains the following information:

  • shortMessage is the text message data
  • sourceAddress is the phone number of the message sender
  • destAddress is the phone number of the message recipient
The SMS event gateway event data contains additional fields that you may fill, depending on the network carrier. You can read more about the SMS event gateway in the detailed chapter from ColdFusion MX Developer's Guide (SMS Event Gateway).

The instant messenger (IM) event gateway event data structure contains the following information:

  • message is the instant message text
  • sender is the user ID of the message sender
  • recipient is the user ID of the message recipient
  • timestamp includes date/time data when the message was sent
You can read more about the IM event gateway in the chapter from ColdFusion MX Developer's Guide (Click Here for the Guide)

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
Use the new sendGatewayMessage CFML function to send event objects to an event gateway directly from your CFML code-from any CFC or CFML page. Use the following code syntax:


endGatewayMessage (gwid, data)
  • Argument 1: Gateway ID - Name of the gateway instance configured in the ColdFusion Administrator
  • Argument 2: Data - CFML struct with name/value pairs that corresponds to an event object for the gateway
When creating a structure for an outgoing IM event object, use the following syntax:


<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
Just as each gateway type has different event object data to pass to the CFC, each gateway type requires different data in the event object that it will process for outgoing messages. The event gateway's documentation specifies what it requires for data. Many gateways have a few required data items and many optional items. The design is flexible and allows for an unlimited number of data items. For example, the IM event gateways requires the buddyID and message data items, but optionally accepts the command data item, with values of accept, decline, or submit. If the value is accept or decline (for instance, to respond to a request to add the gateway user to a sender's buddy list), you can optionally set the reason data item, which specifies explanation text.

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 getGatewayHelper function to provide additional functionality to ColdFusion applications according to the event gateway's purpose. The IM event gateways, for example, provide buddy-list management functionality using an event gateway helper.

Use the following code syntax:


getGatewayHelper (gwid)
  • Argument: Gateway ID - Name of the event gateway instance configured in the ColdFusion Administrator
This function returns an object on which helper functions can be called. For the IM event gateways, this returned object has addBuddy (), remveBuddy (), and getBuddyList () functions available for buddy-list management. These are just some of the functions that the IM gateway helper makes available.

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
Getting started with an instant messaging application requires first setting up an event gateway instance in the ColdFusion Administrator. To set up the instance, specify a configuration file with the connection details for the IM network.

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
If you need to create an event gateway for an application, protocol, or purpose that you can't accomplish with the built-in, sample, or third-party event gateway types, remember that ColdFusion MX 7 is extensible. Anyone can write an event gateway using the ColdFusion API.

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.

About Jim Schley
This site contains content written by Jim Schley A LONG TIME AGO.

About Tom Jordahl
Tom Jordahl has been a Principal Engineer at Allaire/Macromedia for over six years and has spent the last two years on the front lines of Web services. He is one of the implementers of Apache Axis and is the Macromedia representative on the W3C Web Service Description WSDL 1.2 working group. Before getting involved in Web services, he was the technical lead for the ColdFusion on UNIX products. Tom is currently "back home" working on ColdFusion.

In order to post a comment you need to be registered and logged in.

Register | Sign-in

Reader Feedback: Page 1 of 1

Enterprise Open Source Magazine Latest Stories . . .
Before embarking on using open source cloud technology for your web property, a basic understanding of cloud, as it’s used in the industry, is essential. While there might be exceptions, here are the definitions. A software application delivered on the web instead of installing standa...
Businesses today generate billions of events or 100s of TBs of data in a month. These data contain valuable insights into customer behavior, key trends, buying patterns, etc. If these are successfully mined, they can lead to successful decision-making to maximize revenue and traffic fo...
Grid Dynamics, an eCommerce technology solutions company, and GridGain Systems, makers of an open source in-memory platform for Big Data processing, on Wednesday announced the expansion of their partnership which began in 2008. Grid Dynamics provides personalization and big data solut...
Private clouds solve many problems for enterprises and bring unique operational challenges along with them. There are dozens of companies of all sizes that will build you a private cloud and turn over the keys – then what? Trying to convert a traditional enterprise IT operations team t...
The networking industry has gone through different waves over last 30+ years. In the ’80s, the first wave was all about connecting and sharing; how to connect a computer to other peripheral devices and other computers. There were many players who developed technology and services to ad...
If your organization already uses virtualized infrastructure, you are well on your way to providing IT as a Service. But as businesses demand faster results in today’s competitive market, organizations look to gain more benefits from cloud computing than just virtualized infrastructure...
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021


SYS-CON Featured Whitepapers
ADS BY GOOGLE