Skip Navigation Links | |
Exit Print View | |
Oracle Java CAPS JMS JCA Wizard User's Guide Java CAPS Documentation |
To Create an Admin Object Resource
To Create the EJB Module Project
To Create an Admin Object Resource
To Define a JMS Session Instance
Create a Reference to the Destination Object
Initiating a Request-Reply Transaction
To Create the EJB Module Project
This topic provides instructions for 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 the type TextMessage.
Perform the following steps to receive a JMS text message:
For this example, the message is being received from Queue1, so you need to create the corresponding JMS Queue object resource in GlassFish.
The URL for the Admin Console is http://HostName:PortNumber. The default port number is 4848.
Figure 1 Admin Object Resources
The New Admin Object Resource window appears (Step 1 of 2).
For the purposes of this exercise, use the following values:
JNDI Name = jms/Queue1
Resource Type = javax.jms.Queue
Resource Adapter = sun-jms-adapter
Figure 2 New Admin Object Resources (Step 1 of 2)
For this exercise, enter Queue1. This is the physical destination name of the resource.
Figure 3 New Admin Object Resources (Step 2 of 2)
Figure 4 Choose New Project
The Name and Location window appears.
For the purposes of this exercise, enter the following values:
Project Name = JMSJCASample
Project Location = the location to store NetBeans project files
Figure 5 EJB Module Project Name and Location
The Server and Settings window appears.
Figure 6 Choose JCA Message-Driven Bean
The JCA Message-Driven Bean Name and Location window appears.
For this exercise, enter the following values:
Class Name = JCAMessageBeanSample
Package = jmsjca.sample
Figure 7 JCA Message-Driven Bean Name and Location
The Choose Inbound JCA window appears.
Note - Currently only JMS Adapter can be selected in the window.
The Edit Activation Configuration window appears.
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 are needed for our sample code to work.
Figure 8 Edit Activation Configuration
This resource connects the embedded 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
The Connector Resource dialog box for the Destination appears.
This is the Admin Object Resource created earlier for the Queue1 destination using the GlassFish Admin Console.
Figure 10 Connector Resource — Destination
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
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 content 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.
The contents of the message is logged in the server log file.