YOUR FEEDBACK
ASP.NET
mark bosley wrote: Good article. Please post the code or send it to me. It ...
AJAXWorld RIA Conference
$300 Savings Expire July 25
Register Today and SAVE!


2007 West
GOLD SPONSORS:
Active Endpoints
Your SOA Needs BPEL for Orchestration
BEA
Virtualized SOA: Adaptive Infrastructure for Demanding Applications
Nexaweb
Overcoming Bandwidth Challenges with Nexaweb
TIBCO
What is Service Virtualization?
SILVER SPONSORS:
WSO2
Using Web Services Technologies and FOSS Solutions
Click For 2007 East
Event Webcasts

2008 East
PLATINUM SPONSORS:
Appcelerator
Think Fast: Accelerate AJAX Development with Appcelerator
GOLD SPONSORS:
DreamFace Interactive
The Ultimate Framework for Creating Personalized Web 2.0 Mashups
ICEsoft
AJAX and Social Computing for the Enterprise
Kaazing
Enterprise Comet: Real–Time, Real–Time, or Real–Time Web 2.0?
Nexaweb
Now Playing: Desktop Apps in the Browser!
Sun
jMaki as an AJAX Mashup Framework
POWER PANELS:
The Business Value
of RIAs
What Lies Beyond AJAX?
KEYNOTES:
Douglas Crockford
Can We Fix the Web?
Anthony Franco
2008: The Year of the RIA
Click For 2007 Event Webcasts
SYS-CON.TV
TOP COLDFUSION LINKS


Event Gateways
An exciting and powerful feature

Digg This!

Page 2 of 4   « previous page   next page »

Note: For the directory gateway configuration file, you want to use the forward slash (/) rather than the normal backslash (\).

Note: Not all event gateways will have a configuration file, and each configuration file will be unique to that specific gateway.

Next, we'll create a CFC that listens for events from the gateway. For this example, we'll only use the onAdd method supplied by the DirectoryWatcher gateway. What we want our CFC to do is listen for an event from the directory watcher, saying that files have been added to the gallery directory. Then the CFC will call the imagemanipulator.cfc (from Chapter 29) to create the thumbnails. For this example we are assuming that the DirectoryWatcher.cfc is being placed in the same directory as the imagemanipulator.cfc which is webroot\JavaInteg\ and the directory gallery is beneath that. Last, we'll log the event. The code to do all this is in Listing 1.

Most of the code here is pretty straightforward. You'll see some things that are specific to working with event gateways. The first is that the listener CFC expects a struct called CFEvent, which is a Java object that is mapped to a ColdFusion struct. The CFEvent object contains a variety of information, including a GatewayID, OriginatorId, GatewayType, CFCPath, CFCMethod, CFCTimeout, and Data. Figure 2 shows an example of what the CFEvent message might look like in our example. Table 2 describes each node in the structure.

In Listing 1, the CFEvent message is used only to log the DirectoryWatcher method that was used, data.type, as well as the file and file path, data.filename. In a more complex application, you may want your CFC to take actions based on specific information from the CFEvent message.

Now to get our code to actually work, we need to do two other things. The first is to go into the ColdFusion Administrator and create a mapping for the DirectoryWatcher CFC; otherwise, the event gateway will not know where to look. The second thing we need to do is create an instance of the event gateway.

Creating an Event Gateway Instance
Before you can use the example in Listing 1, you must create an instance of the event gateway.

First go to the ColdFusion Administrator and select Event Gateways > Gateway Instances. You'll see something like Figure 3.

You should now see a form with a number of fields including Gateway ID, Gateway Type, CFC Path, etc. To create the gateway follow these steps:

  • The first field is the Gateway ID, which can be anything; for our example, let's just use PhotoAlbum.
  • The next field is Gateway Type, which is a drop-down list of all the registered gateways. For this example, we select DirectoryWatcher, which watches a directory for file changes.
  • The next field is CFC Path, which is the CFC path to our listener DirectoryWatcher.cfc. After that you need to add the path to the directory-watcher.cfg.
  • Finally, select the Startup Mode drop-down list to choose whether you want the event instance to be started automatically, manually, or disabled on startup of ColdFusion. Choose Automatic and then click Add Gateway Instance.
You should now see your gateway instance in the Configured ColdFusion Event Gateway Instances area of the page. Be sure to click the green button to start your event gateway instance. Now your event gateway instance is running and ready to respond to events.

You can make as many instances as you want of a specific gateway, so you could create several DirectoryWatcher instances in order to watch many directories (although it would probably be better to just change the DirectoryWatcher class to support multiple directories). For each event gateway for which you want to create applications, you must have at least one instance actually running if you want to use it.

Now that you have the DirectoryWatcher instance running, you can test the example by copying some images into the gallery folder. Then navigate to your ColdFusion logs directory; you should see a new log created, watcher.log, which is our listener CFC's log. If you have set your directorywatcher.cfg to 60 seconds, you may have to wait, but eventually you'll see the watcher.log update. Look there, and you'll see that it has recorded the action, add, and the file and file path of the images you added to the directory. Examine the thumbnails directory under gallery to see the thumbnails that have been created. At this point it is easy to add code to support things like unzipping zip files of images, moving them to their own new directory based on the zip file names, and then creating the thumbnails - or any other cool functionality you like.

Note: If you change or delete a file in the watched directory, you'll see an error in the gateway.log file and the cfexception log file. This is because we did not define these methods in our CFC. You can easily do this and just add these methods and have them do nothing, or log the change to a file, etc.

At this point you have created a simple responder application and have set up an event gateway instance. You've learned something about how ColdFusion event gateways work, but there is a lot more to event gateways. Now we'll explore them further, discussing initiator applications, some of the particular differences between coding CFML for gateways and other applications, and how to debug your CFML gateway applications. Finally, we'll look at a simple example of creating your own custom gateway using ColdFusion MX 7's API for gateways using Java.

Creating an Initiator Application Using the ColdFusion Gateway
So far, you've seen how to create a responder application that listens for events from an event gateway instance and takes some sort of action. Now we'll study an application called an initiator application that sends a message to an event gateway. This example uses the ColdFusion Gateway to asynchronously log messages to a file via a simple CFC.

Logging may seem trivial, especially since you can just use CFLOG, but CFML pages that use CFLOG to write large amounts of data to a log file can seriously degrade an application's performance, as well as tie up threads that could be better used serving your application's Web users. In addition, you might have an application like a B2B that needs to log large amounts of information, such as every type of transaction between partners for legal reasons. Thus you might want to decouple logging into its own subsystem, not only for performance but for good design. Using the ColdFusion Gateway allows you to create applications that call CFCs asynchronously, which is perfect for this example. Okay, let's look at some code (see Listing 2).

As you can see, this is a very simple CFC that accepts a CFEvent message and logs information from it. After you save this file to a directory, create an event gateway instance in the Event Gateways page of the Administrator, as described earlier. For this example, give the instance a Gateway ID of DataLogger and point it to this CFC. Then leave the Configuration File field blank, create the instance, and start it running.

Now we can take our previous code and, instead of using CFLOG directly for logging information, send a message to the event gateway that will call the DataLogger.cfc to log the event. We do this using SendGatewayMessage with "GatewayID" and a struct with an event message. Let's look at Listing 3.

We have just added some simple code in a CFSCRIPT block and then used the SendGatewayMessage() function, which takes two required attributes. The first is the Gateway ID and the second is data, a structure that conforms to the specific gateway type. In this case, the gateway expects the event to contain a message node as well as an optional file node.

As you can see, creating an initiator application is simple enough, and in this case we have a listener that both responds to an event from a gateway and fires an event to a gateway.

Debugging CFML Applications for Event Gateways
When you need to develop ColdFusion applications that use event gateways, you need to be particularly careful - CFCs that are responding to events work differently than when they are responding to a normal page request. If an event gateway triggers a CFC and that CFC throws an error, the event gateway continues to function without pause and does not display any sort of debugging information back to you. For this reason, you'll need to follow some different development paradigms - especially with regard to debugging, so that you can make sure your CFCs and event gateways are functioning as expected. In this section we'll examine some techniques you can and should use to help you debug and write better code.


Page 2 of 4   « previous page   next page »

About Ben Forta
Ben Forta is Adobe's evangelist for the ColdFusion product line. He is the author of several books.

About Sheldon Sargent
Sarge is Sr. Technical Support Engineer, Adobe Systems, based in Gilbert, Arizona. He has been coding advanced applications in CF since version 2.0.

CFDJ LATEST STORIES . . .
Adobe's Kevin Lynch and Microsoft's Scott Guthrie to Keynote AJAX World RIA Conference & Expo
Two of the biggest launches in Rich Internet Application history took place in 2007/2008 when Adobe launched AIR 1.0 in February '08 and Microsoft launched Silverlight (September '07). At the 6th International AJAXWorld RIA Conference & Expo in October SYS-CON Events is delighted to be
Voyager Offers Android, .NET CF, Java Runtime Support
Recursion Software released a private beta version of their Voyager mobile platform, with powerful interoperability for Android, Microsoft .NET and Compact Framework (CF), all Java editions (JME CDC, JSE and JEE), and more than 15 embedded operating systems. The Voyager platform is a p
AJAX and Enterprise RIA Tools - JSF, Flex, and JavaFX
2008 is going to be an important year for Rich Internet Applications. Most organizations are delivering or planning to deliver Rich Internet Applications; however, at the same time, most IT managers are facing a dilemma: which Rich Internet Application technology and platform to use? T
CFDynamics Announces Renewed Agreement with SmarterTools
CFDynamics, a ColdFusion web host, has renewed an agreement with SmarterTools that will allow them to pass on immediate value to their customers. When a customers signs up for a dedicated hosting account they will now receive $750 worth of features including SmarterMail, SmarterStats a
Microsoft's Virtualization Chief Mike Neil To Keynote SYS-CON's Virtualization Conference & Expo
Mike Neil is general manager for virtualization strategy in the Windows Server Division at Microsoft. Mike is focused on the delivery of the Windows virtualization technology, including Windows Server 2008 Hyper-V, Microsoft Hyper-V Server and Virtual PC 2007. Mike also directs the tec
SYS-CON's Virtualization Conference & Expo: Themes & Topics
From Application Virtualization to Xen, a round-up of the virtualization themes & topics being discussed in NYC June 23-24, 2008 by the world-class speaker faculty at the 3rd International Virtualization Conference & Expo being held by SYS-CON Events in The Roosevelt Hotel, in midtown
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