Creating a Simple Java Message Service (JMS) Producer with NetBeans
and GlassFish
Overview
- 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
this link.
During installation, be sure to check the box to install Glassfish. JUnit is an optional installation and not required for this tutorial.
- Have the software installed as listed under Software
Requirements.
- Start the NetBeans IDE.
- Download and unzip the MDBExample.zip
file that contains a NetBeans project you need to complete
this tutorial.
Note: It is recommended that the location where you unzip the NetBeans projects does not contain spaces or non-alphanumeric characters.
Purpose
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.
Introduction
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
JDBC.
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
implementation.
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.
Software Requirements
The following software is required to complete this tutorial in
Windows platform. You must install the software
in the given order.
Prerequisites
Before starting this tutorial, you should:
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
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
project.
Start Glassfish Application Server and Deploy the
application.
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
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.
Summary
- The Java EE 6 Tutorial: Java Message Service
Concepts
- 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
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.