ColdFusion and JMS : Learn by example
Posted by Rahul in ColdFusion 8, JMS
Continuing with my previous post which talked about my experiences with working with ColdFusion and JMS, I thought of writing a simple script that demonstrates how simple it can be integrating java messaging with ColdFusion.
The code below is used to send messages to JMS queue, which is basically a point-to-point messaging model. If you are new to messaging and wanted to know about JMS checkout this nice tutorial.
<cfscript>
//Test message to be publishhed
message ="JMS message using ColdFusion";
// queue Name
queueName = "<--QUEUE_NAME-->";
// Queue Connection Factory. Use default connection factories provided by the J2EE Engine
qcf = "jmsfactory/default/QueueConnectionFactory";
// Create the InitialContext Object used for look up
ctx = createObject("java","javax.naming.Context");
prop = createObject("java","java.util.Properties").init();
prop.put(ctx.PROVIDER_URL, "localhost:50004");
prop.put(ctx.INITIAL_CONTEXT_FACTORY,
"com.sap.engine.services.jndi.InitialContextFactoryImpl");
//prop.put (ctx.SECURITY_PRINCIPAL, "anonymous");
//prop.put (ctx.SECURITY_CREDENTIALS, "anonymous");
ic = createObject("java","javax.naming.InitialContext").init(prop);
//Step 1 = Look Up a Connection Factory in JNDI
queueConnectionFactory = ic.lookup(qcf);
//Step 2 = Look Up a Destination Queue
queue = ic.lookup(queueName);
//Step 3 = Create a Connection Using the Connection Factory
qc = queueConnectionFactory.createQueueConnection();
//Step 4 = Create a Session Using the Connection
qs = qc.createQueueSession(false,1);
//Step 5 = Create Message Producers
sender = qs.createSender(queue);
//Step 6 = create ascii text message
textmsg = qs.createTextMessage();
textmsg.setText(message);
//Step 7 = Start the queue & send the message
qc.start ();
deliveryMode = createObject("java","javax.jms.DeliveryMode");
sender.send(textmsg, deliveryMode.PERSISTENT, 4, 0);
qs.close();
qc.close();
writeoutput("Message sent");
</cfscript>
This is the *only* code we need in ColdFusion to interact with Java messaging system. Isn't it cooool !!! The above code uses the SAP Netweaver underlying Queue Factory implementation.
A few things to consider.
- There is no need to typecast the lookup objects into required type as in the case of Java. ColdFusion will do that for use automatically due to its dynamic nature.
- We really can't set up the ExceptionListener using this approach to detect a problem with the connection as
setExceptionListener()expects an object ofjavax.jms.ExceptionListenerand we have no way of implementing the interface directly in ColdFusion. To overcome this problem and to report and handle the connectivity issue we can just place the sender.send() method in a try/catch block ;-) and handle 'any' exception. - We can directly use the constant values like
deliveryMode.PERSISTENTused above directly instead of creating object ofjavax.jms.DeliveryModeto refer the constant. Refer here for the complete list of constants. - I am using this code in an event gateway implementation, hence no
<cfthread>else this is the prospective candidate to be inside<cfthread>

