This tutorial demonstrates how to use the JMS API to create a
simple message producer using GlassFish 3.1.2 and NetBeans 7.
Time to Complete
Approximately 45 minutes.
Messaging is a method of communicating between software components or applications. Messaging allows loosely-coupled communication between distributed applications. Message clients can send and receive messages by connecting to a messaging agent that facilitates message receipt and delivery. Clients need not know anything about other clients that will consume or produce messages. A message client only needs to know the format of the message to send and the destination. Thus messaging differs from other tightly coupled technologies, such as Remote Method Invocation (RMI), which require an application to know a remote applications methods.
The Java Message Service API was designed by Sun Microsystems
and several other companies to address the need to connect
intra-company applications through enterprise messaging
products, sometimes referred to as Message Oriented Middleware
(MOM). JMS provides a way for Java applications to access
messaging systems. JMS is a set of interfaces and associated
semantics that defines how a JMS client access the facilities of
messaging implementation. In this regard JMS is very much like
Messaging systems are peer-to-peer facilities, allowing clients
to send and receive messages from any other client. Some
messaging systems can also broadcast messages to many
destinations, and clients subscribe to a specific channel or
topic to receive messages from that are broadcast. JMS is
implemented to support both models, depending upon the
implementation of the JMS provider. The figure below illustrates
the participating components of a Java Message Service
The JMS API Messaging System Participants
A JMS technology provider (JMS provider) is a messaging system that provides an implementation of the JMS API. For an application server to support JMS technology, you must place the administered objects (connection factories, queue destinations, and topic destinations) in the JNDI technology namespace of the application server. Typically, you would use the administrative tool supplied by the application server to perform this task. However, in this tutorial, you will use the capabilities built into NetBeans to define and create the administered objects.
Specifically, you will define a Queue Destination and
Connection Factory in NetBeans. After deploying the application
once, you can use a feature in NetBeans to generate the code
that uses the Connection Factory you specified to generate a
Connection object. With the Connection object, the generated
code creates a Session object, which is used to create a
MessageProducer object to send the string entered on JSF page to
the queue as a Message object.
The following software is required to complete this tutorial in
Windows platform. You must install the software
in the given order.
- Download and install Java JDK 7 from this link.
- Download and install NetBeans IDE 7.1.2 Java EE version
which includes GlassFish 3.1.2 (Java EE download bundle) from
During installation, be sure to check the box to install Glassfish. JUnit is an optional installation and not required for this tutorial.
Before starting this tutorial, you should:
- Have the software installed as listed under Software
- Start the NetBeans IDE.
- Download and unzip the MDBExample.zip
file that contains a NetBeans project you need to complete
Note: It is recommended that the location where you unzip the NetBeans projects does not contain spaces or non-alphanumeric characters.
Create a NetBeans Web Application Project
NetBeans provides a number of different project options. In this tutorial, you will create a Web Application project and use the JSF framework.
Create a JMS Producer Managed Bean
In this topic, you will create a managed bean for the JSF Facelet.
Create a new JSF Managed Bean.
Implement the JSF managed bean with a String message field
Implement the JSF page
Add components to the JSF page to write to the message field in the managed bean.
Add a message queue and connection factory to your
Add a JMS queue Admin Object Resource.
Add a JMS Queue Connector Resource
Start Glassfish Application Server and Deploy the
Generate the JMS code in the ManagedBean.
Modify your send method to call the generated code.
The send() method will attempt to send the String message
to the JMS queue destination you added to the project.
If the message is sent properly, a FacesMessage is added to the FacesContext instance that represents the current view page. The FacesMessage will be displayed to the browser client when the page is rendered.
Test the application.
Looking at Message Queue statistics
Although you cannot see the content of the messages on the server, you can determine how many messages have been sent to a destination (and are not yet picked up.)
Using the command line
There is one message in myQueue.
Using the admin console
Reading the messages on the queue
A simple Message-Driven Bean (MDB) NetBeans project has been included in this tutorial to allow you to "see" the messages in the queue. In another OBE, you will explore more advanced application of MDBs, including how to take messages from the queue and store them for another application.
In this tutorial, you created a JMS Producer application. The
application uses a JSF page to create a string message and a
managed bean to put the string onto a JMS Message Queue.
- The Java EE 6 Tutorial: Java Message Service
- Java Message Service Documentation
- Developing Java EE 6 Applications for the
Java EE 6 Platform
- To learn more about using the Java Message Service in Java
EE applications, refer to additional OBEs in the Oracle Learning Library.
- Lead Curriculum Developer: Tom McGinn
- Other Contributors: Matt Heimer
To help navigate this Oracle by Example, note the following:
- Hiding Header Buttons:
- Click the Title to hide the buttons in the header. To show the buttons again, simply click the Title again.
- Topic List Button:
- A list of all the topics. Click one of the topics to navigate to that section.
- Expand/Collapse All Topics:
- To show/hide all the detail for all the sections. By default, all topics are collapsed
- Show/Hide All Images:
- To show/hide all the screenshots. By default, all images are displayed.
- To print the content. The content currently displayed or hidden will be printed.
To navigate to a particular section in this tutorial, select the topic from the list.