Index Changes
Back to JMSJCA Wizard Home

JMSJCA Wizard Quickstart

So let's get started and see JMSJCA Wizard in action. First you need to run the Open ESB Installer if you haven't done so.

Scenario 1: Receive a JMS TextMessage

In this scenario, we will build a Message-Driven Bean (MDB) that will listen on "Queue1" JMS destination on JMS Server to receive JMS messages. The MDB upon receipt of the a JMS message, if print out the content of the message if the message is of type TextMessage.

  • First startup Glassfish AppServer, use a browser to connect to Admin Console via http://localhost:4848.

  • Create an Admin Object Resource.
    • Since we are receiving from Queue1, we have to create the corresponding JMS Queue object resource in Glassfish.
    • Goto "Resources->Connectors->Admin Object Resources->New..."
01.png

  • Fill in the boxes,
    • JNDI Name = "jms/Queue1"
    • Resource Type = "javax.jms.Queue"
    • Resource Adapter = "sun-jms-adapter"
    • Click "Next"
02.png

  • On the next page, fill in the physical destination name of this resource
    • Name = "Queue1"
    • Click "Finish"
03.png

  • Start NetBeans IDE and create a new "EJB Module Project"
    • Select "New Project->Enterprise->EJB Module"
    • Click "Next"
04.png

  • Fill in the boxes
    • Project Name = "JMSJCASample"
    • Project Location = "a directory on your filesystem"
    • Click "Next"
05.png

  • Leave everything as default
    • Click "Finish"
06.png

  • Right-click on Project node
    • Select "New->Other->Enterprise->JCA Message-Driven Bean"
    • Click "Next"
07.png

  • Fill in the boxes
    • Class Name = "JCAMessageBeanSample"
    • Package = "jmsjca.sample"
    • Click "Next"
08.png

  • Currently only "JMSAdapter" can be selected in the window,
    • Click "Next"
09.png

  • Next user can configure the many different options for the Inbound JMS connection. Things such as the JNDI name of the JMS connection resource to use, the JNDI name of the JMS destination. Also more advanced options can be set as well, such as message redelivery, selector, concurrency mode, etc. In this simple case, we only need to set the "Connection URL" and "Destination" options for our sample code to work.
    • Click on the "..." button next to "Connection URL" box as shown below
10.png
    • Expand the tree node all the way down, and select "jms/tx/jmq1" as shown below. This is the connection resource to connect to embedded Sun MQ JMS server inside the Glassfish AppServer, and this connection resource should be created by default with the installer. The default connection url is "mq://localhost:7676"
11.png
    • Next click on the "..." button next to "Destination" box as shown below
12.png
    • Expand the tree node all the way down, and select "jms/Queue1" as shown below. This is the Admin Object Resource we created earlier for "Queue1" destination using the Glassfish admin console.
13.png
    • Click "Finish" in the wizard dialog box to create the Message-Driven Bean.

  • A file Java source file will be created, and opened in editor view. The source file is a skeleton file with most of the boiler-plate code already generated, shown below.
14.png
    • Any JMS messages sent to the destination "Queue1" will be passed the the "onMessage(...)" method in this Java file. User can do the processing login inside this "onMessage()" method as appropriate. In this scenario, we just simply want to print out the message content of the JMS message if the message is of type javax.jms.TextMessage. So the implementation code should look something like below:
    public void onMessage(Message message) {
        try {
            if (message instanceof javax.jms.TextMessage) {
                // implement listener interface here
                logger.log(Level.INFO, "JMS message content is: " + ((javax.jms.TextMessage) message).getText());
            }
        } catch (JMSException ex) {
            Logger.getLogger(JCAMessageBeanSample.class.getName()).log(Level.SEVERE, null, ex);
            return;
        }
    }
    • Click "Save" button after you are done with editing the file.

  • This is really all a user needs to do in order to receive a JMS message. To run the sample code:
    • Right-click on the Project node, and select "Build"
    • Right-click on the Project node, and select "Undeploy and Deploy"
    • Use your favorite JMS client to send a TextMessage to "Queue1" to JMS server located at "mq://localhost:7676", and you should be able to see the contents of this TextMessage being logged in the Glassfish AppServer's server.log file.

Scenario 2: Send a JMS TextMessage

Now you have seen how to receive a JMS message in a Message-Driven Bean, but you can also send a JMS message to a destination as well. Let us build upon the previous scenario and send a new JMS message to "Queue2". The message content to "Queue2" would be "Hello " concatenated with the message content received from the "onMessage()" method from "Queue1".

  • Go to Glassfish admin console, create an Admin Object Resource for "Queue2", similar to the steps for "Queue1"
    • Goto "Resources->Connectors->Admin Object Resources->New..."
    • JNDI Name = "jms/Queue2"
    • Resource Type = "javax.jms.Queue"
    • Resource Adapter = "sun-jms-adapter"
    • Click "Next"
    • On the next page, fill in the physical destination name of this resource
    • Name = "Queue2"
    • Click "Finish"

  • Go back to NetBeans IDE, inside the same MDB file, after we received the JMS message inside the "onMessage()" method, we want to send a message to Queue2. To do that, we need to first obtain a JMS Session instance, from the Session we can then create new JMS Message objects, message producers, etc.
    • Drag-n-drop the "Session" icon from the palette window located on the right side, to the inside of "onMessage()" method as shown below:
15.png
    • A dialog box will popup, and you would have to fill in some of the boxes
    • Method Name = "queueToQueue"
    • Resource JNDI Name = "jms/tx/jmq1"
    • Click "Finish"
16.png
    • A bunch of Java code fragments will be generated as a result, in particular the method "queueToQueue(...)" would be the method in which user can implement the logic of using the method arguements to process the incoming message.
17.png
  • Next we need to create reference to the "Queue2" destination object, so we can send message to this destination object in Java code
    • Drag-n-drop the "Queue" icon from the palette window located on the right side, to any place in Java editor as shown below:
18.png
    • A dialog box will popup, fill in the boxes
    • JNDI name = "jms/Queue2", you can use the browse button instead of typing
    • Variable Name = "queue2"
    • Click "OK"
19.png
  • Now you are ready to write the actual code to create a new JMS message and send it to Queue2
    • The code fragment inside method "queueToQueue(...)" looks like something below:
    private void queueToQueue(Message message, javax.jms.Session jmsSession) throws java.lang.Exception {
        if (message instanceof javax.jms.TextMessage) {
            String oldContent = ((javax.jms.TextMessage) message).getText();
            javax.jms.TextMessage newMessage = jmsSession.createTextMessage("Hello " + oldContent);
            jmsSession.createProducer(queue2).send(newMessage);
        }
    }
    • Save the modifications you have made
  • To test that JMS messages are indeed being passthrough from "Queue1" to "Queue2"
    • Right-click on the Project node, and select "Build"
    • Right-click on the Project node, and select "Undeploy and Deploy"
    • To testUse your favorite JMS client to send a TextMessage to "Queue1" to JMS server located at "mq://localhost:7676", and another client or within the same client to receive a TextMessage from "Queue2" from JMS server located at "mq://localhost:7676".

Conclusion

I hope this Quickstart Guide is helpful to get you started with using the JMSJCA Wizard to write JMS code in your Java EE applications. For more detailed information please refer to Using The JMSJCA Wizard.

Back to JMSJCA Wizard Home

JSPWiki v2.4.100
[RSS]
« Home Index Changes Prefs
This page (revision-4) was last changed on 08-Apr-09 22:53 PM, -0700 by FrankKieviet