Right-click on the Project node, and then select New->Other.
On the wizard, select Java EE under Categories, and select JCA Message-Driven Bean under File Types.
Click Next.
The JCA Message-Driven Bean Name and Location window appears.
Enter the Name and Location fields.
For this exercise, enter the following values:
Class Name = JCAMessageBeanSample
Package = jmsjca.sample
Click Next.
The Choose Inbound JCA window appears.
Select JMS Adapter and click Next.
Currently only JMS Adapter can be selected in the window.
The Edit Activation Configuration window appears.
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.
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.
Click on the ellipsis button next to Destination box.
The Connector Resource dialog box for the Destination appears.
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.
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.
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; } } |
The above code has been wrapped to fit the page.
Click Save when you are done editing the file.