Using the JMS JCA Wizard

ProcedureTo Receive a JMS TextMessage

This topic provides instructions on building a Message-Driven Bean (MDB) that will monitor a designated queue on a JMS destination (of the JMS Server) in order to receive JMS messages. Upon receipt of the a JMS message, the MDB will print out the content of the message, if it is of type TextMessage.

  1. Start the Glassfish AppServer and use a browser to connect to the Admin Console via http://localhost:4848.

  2. Create an Admin Object Resource.

    Figure 1–1 Admin Object Resources

    Admin Object Resources

    1. Because the message is being received from Queue1, create the corresponding JMS Queue object resource in Glassfish.

    2. Navigate to Resources->Connectors->Admin Object Resources

    3. Click New.

      The New Admin Object Resource window appears (Step 1 of 2).

  3. Enter the required fields.

    Figure 1–2 New Admin Object Resources (Step 1 of 2)

    New Admin Object Resources (Step 1of 2)

    • JNDI Name = jms/Queue1

    • Resource Type = javax.jms.Queue

    • Resource Adapter = sun-jms-adapter

  4. Click Next.

    You will be directed to step 2 of the process.

  5. Enter the physical destination name of this resource.

    Figure 1–3 New Admin Object Resources (Step 2 of 2)

    New Admin Object Resources (Step 2of 2)

    • Name = Queue1

  6. Click Finish.

  7. Start NetBeans IDE and create a new EJB Module Project. Choose Project by selecting New Project->Enterprise->EJB Module.

    Figure 1–4 Choose New Project

    Choose Project

  8. Click Next.

    The Name and Location window will be displayed.

  9. Enter the Project Name and Location fields.

    Figure 1–5 EJB Module Project Name and Location

    Name and Location

    • Project Name = JMSJCASample

    • Project Location = a directory on your filesystem

  10. Click Next.

    The Server and Settings window will be displayed.

  11. In the Server and Settings window, leave everything as default and Click Finish.

  12. Right-click on the Project node. Select New->Other->Enterprise->JCA Message-Driven Bean.

    Figure 1–6 Choose JCA Message-Driven Bean

    Choose File Type

  13. Click Next.

    The JCA Message-Driven Bean Name and Location window will appear.

  14. Enter the Name and Location fields.

    Figure 1–7 JCA Message-Driven Bean Name and Location

    File Name and Location

    • Class Name = JCAMessageBeanSample

    • Package = jmsjca.sample

  15. Click Next.

    The Choose Inbound JCA window is displayed.

  16. Select JMS Adapter and click Next.


    Note –

    Currently only JMS Adapter can be selected in the window.


    The Edit Activation Configuration window is displayed.

  17. Configure the Inbound JMS connection by clicking on the "..." button next to the Connection URL box (as shown below).

    You can configure many different options for the Inbound JMS connection. Things such as the JNDI name of the JMS connection resource to use or the JNDI name of the JMS destination. You can also configure the more advanced options such as message re-delivery, selector, concurrency mode, etc. In this simple case, only the Connection URL and Destination options for our sample code to work.

    Figure 1–8 Edit Activation Configuration

    Edit Activation Configuration

  18. Expand the tree node all the way and select jms/tx/jmq1 (as shown below).

    This resource connects the embedded Sun MQ JMS server inside the Glassfish AppServer and is created by default with the installer. The default connection url is mq://localhost:7676

    Figure 1–9 Connector Resource — Connection URL

    Edit Activation Configuration

  19. Click on the "..." button next to Destination box.

    The Connector Resource dialog box for the Destination is displayed.

  20. Expand the tree node all the way and select jms/Queue1 (as shown below).

    This is the Admin Object Resource created earlier for the Queue1 destination using the Glassfish admin console.

    Figure 1–10 Connector Resource — Destination

    Edit Activation Configuration

  21. Click Finish in the wizard dialog box to create the Message-Driven Bean.

    A Java source file will be created and opened in the editor view. The source file is a skeleton file with most of the boiler-plate code already generated, as shown below.

    Figure 1–11 Java Source Code

    Java Source Code

    Any JMS messages sent to the Queue1 destination will be passed to the onMessage(...) method in this Java file. The login can be processed inside the onMessage() method as needed. Because the purpose of this task is to simply print out the message content of the JMS message (if the message is of type javax.jms.TextMessage), the implementation code would look something like the following:

    Figure 1–12 TextMessage

    TextMessage

  22. Click Save when you are done editing the file.

  23. Run the sample code by completing the following steps:

    1. Right-click on the Project node and select Build

    2. Right-click on the Project node and select Undeploy and Deploy.

    3. Use your favorite JMS client to send a TextMessage to Queue1 on the to JMS server, located at mq://localhost:7676.

    The contents of the TextMessage will be logged in the Glassfish AppServer's server.log file.