Using the JMS JCA Wizard

ProcedureTo Create the JCA Message-Driven Bean

  1. Right-click on the Project node, and then select New->Other.

  2. On the wizard, select Java EE under Categories, and select JCA Message-Driven Bean under File Types.

    Figure 6 Choose JCA Message-Driven Bean

    Choose File Type

  3. Click Next.

    The JCA Message-Driven Bean Name and Location window appears.

  4. Enter the Name and Location fields.

    For this exercise, enter the following values:

    • Class Name = JCAMessageBeanSample

    • Package = jmsjca.sample

    Figure 7 JCA Message-Driven Bean Name and Location

    File Name and Location

  5. Click Next.

    The Choose Inbound JCA window appears.

  6. Select JMS Adapter and click Next.


    Note –

    Currently only JMS Adapter can be selected in the window.


    The Edit Activation Configuration window appears.

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

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

    Figure 8 Edit Activation Configuration

    Edit Activation Configuration

  8. 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 server and is created by default with the installer. The default connection URL is mq://localhost:7676.

    Figure 9 Connector Resource — Connection URL

    Edit Activation Configuration

  9. Click on the ellipsis button next to Destination box.

    The Connector Resource dialog box for the Destination appears.

  10. 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 10 Connector Resource — Destination

    Edit Activation Configuration

  11. Click Finish.

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

    Figure 11 Java Source Code

    Java Source Code

    Any JMS messages sent to the Queue1 destination are 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 be similar to the following:


    public void onMessage(Message message) {
        try {
            if (message instanceof javax.jms.TextMessage) {
                logger.log(Level.INFO, "JMS message conecnt is: " + 
                  ((javax.jms.TextMessage) message).getText());
            }
        } catch (JMSException ex) {
            Logger.getLogger(JCAMessageBeanSample.class.getName()).log(Level.SEVERE, 
                             null, ex);
            return;
        }
    }

    Note –

    The above code has been wrapped to fit the page.


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