Most of us (and not just CFers) build applications with inline processing, For example, if a user uploads a file we process it in the ACTION page while the user waits. Or if a report needs to be generated we create it while the user waits. Or if we need to send out a batch of e-mail messages … yes, we do it while the user waits.
That’s not a bad thing, some processing must in fact be real-time. If a user fills in a search form and wants to see results, well, those results need to be retrieved while the user waits.
But must all processing happen inline? Suppose you were using the new reporting features in ColdFusion MX 7, and your user filled in a form to select the data to be reported on, and you then generated and displayed the report. Now suppose that you are generating lots of reports, big complex ones. Instead of making the users wait while the reports were generated, you could tell them “working on it, it’ll be in your inbox shortly”. Users will be happier because they’d not be sitting idle waiting, the response would be quicker, and the whole application may run faster too.
That was a simplistic example, but you get the idea. ColdFusion applications have traditionally been processed synchronously, line after line, sequentially. While ColdFusion has always been capable of processing multiple requests at once, we as developers have not had an easy way to fire off our own requests so as to process asynchronously.
But that has changed. ColdFusion MX 7 Enterprise features event gateways, and one included gateway makes performing asynchronous processing very easy.
Here is how it works:
- The first thing you need to do is create the ColdFusion code that you’ll be calling. This must be in a ColdFusion Component, and you could very feasibly just move existing code into a CFC method. That CFC method would accept data passed to it (details about the processing to be performed), and would not generate output (which would never really be seen).
- Next you’ll define an instance of the asynchronous processing gateway in the ColdFusion Administrator, giving it a unique name, and pointing it to the CFC you just created.
- And finally, you’d start the gateway.
With the gateway running you can start submitting requests to it. This is what you’d do:
- Create a simple ColdFusion structure containing whatever data you need to pass to your CFC method (the name of the uploaded file to be processed, details about the report to be generated, etc.).
- Use the SendGatewayMessage() function to fire the request, you’ll specify 2 parameters, the name of the gateway, and your new structure.
And that’s it! ColdFusion will send the request to the gateway engine which will route it to the correct gateway and your code will execute.
The only gotcha in all of this is how to get information back to users if a problem occurs or if some feedback is required. There are lots of options for this one, including:
- Write status information to a log file which you’d subsequently check.
- Use
to send a mail message. - Use another gateway request to send an IM or SMS alert.
Of course, if you truly do need to fire requests that are guaranteed (to be delivered or to inform you of failure) then you probably will want to use the JMS gateway instead of asynchronous processing. And while this is a little more involved, once configured the process is much the same as described above.
If you want example code to play with, Damon Cooper blogged one a while back.
Leave a Reply