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.
Start the Glassfish AppServer and use a browser to connect to the Admin Console via http://localhost:4848.
Create an Admin Object Resource.
Enter the required fields.
JNDI Name = jms/Queue1
Resource Type = javax.jms.Queue
Resource Adapter = sun-jms-adapter
Click Next.
You will be directed to step 2 of the process.
Enter the physical destination name of this resource.
Name = Queue1
Click Finish.
Start NetBeans IDE and create a new EJB Module Project. Choose Project by selecting New Project->Enterprise->EJB Module.
Click Next.
The Name and Location window will be displayed.
Enter the Project Name and Location fields.
Project Name = JMSJCASample
Project Location = a directory on your filesystem
Click Next.
The Server and Settings window will be displayed.
In the Server and Settings window, leave everything as default and Click Finish.
Right-click on the Project node. Select New->Other->Enterprise->JCA Message-Driven Bean.
Click Next.
The JCA Message-Driven Bean Name and Location window will appear.
Enter the Name and Location fields.
Class Name = JCAMessageBeanSample
Package = jmsjca.sample
Click Next.
The Choose Inbound JCA window is displayed.
Select JMS Adapter and click Next.
Currently only JMS Adapter can be selected in the window.
The Edit Activation Configuration window is displayed.
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.
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
Click on the "..." button next to Destination box.
The Connector Resource dialog box for the Destination is displayed.
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 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.
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:
Click Save when you are done editing the file.
Run the sample code by completing the following steps:
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 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.