|
YOUR FEEDBACK Did you read today's front page stories & breaking news?
SYS-CON.TV SYS-CON.TV WEBCASTS |
TOP COLDFUSION LINKS CFMX The Asynchronous CFML Gateway
A Real-World Example of a Great New Technology
By: Matthew Woodward
Jun. 16, 2005 01:00 PM
This was still a less-than-ideal situation. I hope this doesn't come as a shock to any of you, but in case you were unaware, users can be impatient at times. No matter how many different ways I explained the situation, because most users are accustomed to clicking on a link and having the next page finish processing more or less instantly, some users just couldn't understand why generating 50,000 e-mails was taking so long. Even with the addition of flushing each address to the screen, there were still occasional timeout issues that would cause the browser to throw an error of one kind or another. At a minimum seeing an error at the end of the batch would create a sense of uncertainty in the user's mind as to whether or not the e-mails were sent successfully. I thought about going to some relatively insane lengths to change this situation, but in the end I just stuck with the solution and dealt with continually educating my users on the process.
Asynchronous Processing to the Rescue This is an amazingly powerful solution, and as usual ColdFusion makes doing powerful things such as this extremely simple. Let's walk through the steps involved with creating a CFC and registering it in the ColdFusion administrator so it can be used via the asynchronous gateway. (Just a reminder: event gateways are only available in ColdFusion Enterprise and Developer editions. Gateways are not available in ColdFusion Standard.)
Step One: Create AsynchE-mailer.cfc In order to be able to use a CFC via the asynchronous gateway there is one key method that must exist in the CFC: onIncomingMessage. This function is what is called when SendGatewayMessage() is called from your ColdFusion application (more on this in a moment). The onIncomingMessage method in the CFC takes a struct as an argument, and this struct contains the information you want to use within the asynchronous CFC. In the case of the E-Mail Blaster, the struct contains the recipient list, the HTML contents of the e-mail, and a few other bits of information such as the administrator and sender's e-mail address and a path to which a log file is written. See Listing 1 for the complete AsynchE-mailer.cfc code.
Step Two: Register AsynchE-mailer.cfc in the Event Gateway Administrator Click on Gateway Instances to be taken to the form you will use to configure the AsychMailer.cfc to run as a gateway. You'll see the following fields:
Step Three: Call the Asynch E-mailer Gateway from the E-Mail Blaster Application First let's build a struct to hold our data. Since we're using the gateway to send e-mails, the struct we pass to the gateway contains the recipient list, the HTML contents, the sender information such as name and e-mail, and a few other items used for housekeeping and error handling. This is just a plain old ColdFusion struct, so simply populate the struct in your CFML or CFC and pass it to the gateway. Since I've converted the original application into a more object-oriented model, I'm using an E-mailBlast Session bean to store the e-mail blast data prior to beginning the send process, so the code populating this struct looks like this:
e-mailBlastData.recipients = Session.e-mailBlast.getRecipientList(); Once the struct is populated, we simply make a call to the asynchronous gateway by using the SendGatewayMessage() function: status = SendGatewayMessage("Asynch E-mailer", e-mailBlastData); Taking another look at the AsynchE-mailer.cfc code, you will see that the gateway's onIncomingMessage function takes in a struct argument called CFEvent. You can call this anything, but CFEvent is the naming convention used in the ColdFusion documentation so I retained it here. Please note that once the struct is in the gateway, rather than calling the struct's members directly as you would in your application (e.g. e-mailBlastData.content) the struct members are called using CFEvent.Data.myStructMember notation. Using the previous example, the content data would come into the gateway as CFEvent.Data.content. Just be aware of the addition of "Data" to the struct. CFDJ LATEST STORIES . . .
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 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||