ColdFusion and JMS : Learn by example
Bookmark and Share
 

Posted by Rahul in ,

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 of javax.jms.ExceptionListener and 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.PERSISTENT used above directly instead of creating object of javax.jms.DeliveryMode to 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>
  • Learn more about the SAP Netweaver JMS applications.

  • 3 comments

    It would be much easier if ColdFusion shipped with a JMS event gateway that supported queues (and transacted message consumption and message selectors and...).

    Interesting post. Would it be possible to use JMS to enable multiplayer games in Flash/Flex i.e. a multiple swf's connecting to the JMS gateway in CF and exchanging game data?

    At the University of Illinois, we employ heavily on JMS and we have deployed SonicMQ as our JMS provider. Coldfusion 7 and before, we never had much problem with SonicMQ and coldfusion. But with Coldfusion 8, we have been unable to connect to SonicMQ till date. Does anyone know about any incompatibilities? Has anyone tried Coldfusion 8 with SonicMQ? We have both SonicMQ 6.1 and 7.5 versions. We have both Coldfusion 7 and 8 versions. Coldfusion 7 does not seem to have any issues but 8 just doesnt connect and does not provide us with a traceable error.

    Post a Comment

    About Me

    My Photo
    Rahul Narula

    Application Architect Webteam@Adobe

    View my complete profile

    Subscribe via email

    Enter your email address:

    Live Traffic Map

      Powered by Blogger

    Twitter

    Archives