|
ColdFusion 9.0 Resources |
Using the CFML event gateway for asynchronous CFCsThe ColdFusion CFML event gateway lets CFML code send a message to CFC methods asynchronously. This event gateway lets you initiate processing by a CFC method without waiting for it to complete or return a value. Possible uses for asynchronous CFCs that you access using this event gateway include the following:
Because asynchronous CFCs run independently of a request, they do not provide feedback to the user. Save any results or error information to a file, data source, or other external resource. By default, ColdFusion delivers the message to a CFC method named onIncomingMessage. You can specify any method name, however, in the SendGatewayMessage method’s data parameter. CFML event gateway data structureThe structure that you use in the CFML SendGatewayMessage function can include two types of fields:
The CFML gateway looks for the following optional fields, and, if they exist, uses them to determine how it delivers the message. Do not use these field names for data that you send to your CFC method.
Using the CFML gatewayThe following procedure describes how to use an asynchronous CFC that has a single, onIncomingMessage method. Use an asynchronous CFC
Example: logging messagesThe following asynchronous CFML event gateway CFC uses the cflog tag to log a message to a file in the ColdFusion logs directory. The CFC takes a message with the following fields:
<cfcomponent>
<cffunction name="onIncomingMessage" output="no">
<cfargument name="CFEvent" type="struct" required="yes">
<cfscript>
if (NOT IsDefined("CFEvent.Data.file")) {
CFEvent.Data.file="defaultEventLog"; }
if (NOT IsDefined("CFEvent.Data.type")) {
CFEvent.Data.type="info"; }
</cfscript>
<cflog text="#CFEvent.Data.message#"
file="#CFEvent.Data.file#"
type="#CFEvent.Data.type#"
thread="yes"
date="yes"
time="yes"
application="yes">
</cffunction>
</cfcomponent>
The following minimal CFML page tests the event gateway: Sending an event to the CFML event gateway that is registered in the
ColdFusion Administrator as Asynch Logger.<br>
<cfscript>
status = false;
props = structNew();
props.Message = "Replace me with a variable with data to log";
status = SendGatewayMessage("Asynch Logger", props);
if (status IS True) WriteOutput('Event Message "#props.Message#" has been sent.');
</cfscript>
|