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 (Listing 31.2)

Listing 31.2. DataLogger.cfcSimple CFC that logs incoming CFEvents
 <cfcomponent>     <cffunction name="onIncomingMessage" output="no">     <cfargument name="CFEvent" type="struct" required="yes">     <cfif not IsDefined("CFEvent.Data.file")><cfset CFEvent.Data.file="defaultEventLog"></cfif>     <cfif not IsDefined("CFEvent.Data.type")><cfset CFEvent.Data.type="info"></cfif>     <cflog text="#CFEvent.Data.message#"         file="#CFEvent.Data.file#"         type="#CFEvent.Data.type#"         thread="yes"         date="yes"         time="yes"         application="yes">     </cffunction> </cfcomponent> 

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 in this chapter. 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 31.3.

Listing 31.3. DirectoryWatcher.cfcAddition of asynchronous logging requests

[View full width]

 <cfcomponent>     <cffunction name="onAdd" output="no">     <cfargument name="CFEvent" type="struct">     <!--- get event data --->     <cfset data=CFEvent.data>     <!--- watcher will ignore outgoing messages --->     <!--- Location of images --->     <cfset GalleryFolder = ExpandPath("gallery")>     <!--- Get list of images --->     <cftry>     <cfdirectory  action="list" name="GetImages"  directory="#GalleryFolder#"   filter="*.jpg">     <!--- Loop over images --->     <cfloop query="GetImages">     <!--- Proposed location of thumbnail --->     <cfset ThumbPath = ExpandPath("gallery/thumbs/#Name#")>     <!--- If the thumbnail does not exist --->     <cfif not FileExists(ThumbPath)>     <!--- Invoke our image-resizing function --->     <cfinvoke component="JavaInteg.imageio.resize.ImageManipulator"  method="createResizedImage" sourcePath="#GalleryFolder#/#Name#" destPath="#ThumbPath#"   newWidth="100">     </cfif>     <cfscript>         logMessage = structNew();         logMessage.message = "watch";         logMessage.message = "a file was #data.type# and the name was #data.filename#";     </cfscript>     <cfset logAppInfo = SendGatewayMessage("DataLogger", logMessage)>     </cfloop>     <!--- log a message --->     <cfcatch type="Any">     <cfscript>         logMessage = structNew();         logMessage.message = "watch";         logMessage.message = "A exception, #CFCATCH.TYPE#, was thrown in DirectoryWatcher .CFC, the error message is #cfcatch.message#";         </cfscript>         </cfcatch>     </cftry>     </cffunction> </cfcomponent> 

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.

In Chapter 32, "Integrating with SMS and IM," you'll make extensive use of the SendGatewayMessage() function.



Advanced Macromedia ColdFusion MX 7 Application Development
Advanced Macromedia ColdFusion MX 7 Application Development
ISBN: 0321292693
EAN: 2147483647
Year: 2006
Pages: 240
Authors: Ben Forta, et al

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net