This tutorial covers Java Messaging Service 2.0 (JMS 2.0), a new API for sending and receiving messages in Java Platform, Enterprise Edition 7 (Java EE 7) web applications by using GlassFish and NetBeans.
Time to Complete
Approximately 60 minutes
JMS 2.0 is part of the Java EE 7 platform and was released in April 2013. JMS 2.0 is the first update to the JMS specification since version 1.1 was released in 2002. JMS 2.0 introduces a new API for sending and receiving messages that reduces the amount of code which a developer must write. For applications that run in a Java EE application server, the new API also supports resource injection. This feature allows the application server to take care of creating and managing JMS objects, thereby simplifying the application even further. You can use JMS in Java EE web or Enterprise Java Beans (EJB) applications or you can use it alone in a Java Platform, Standard Edition (Java SE) environment.
The new API introduced in JMS 2.0 is known as the simplified API. As the name suggests, it is simpler and easier to use than the JMS 1.1 API. The simplified API consists of three new interfaces:
JMSContextreplaces the separate
Sessionobjects in the classic API with a single object.
JMSProduceris a lightweight replacement for the
MessageProducerobject in the classic API. It allows message delivery options, headers, and other properties.
MessageConsumerobject in the classic API and is used in a similar way.
The JMS 1.1 API is now referred to as the classic API
(with the familiar
objects of JMS 1.1) or the simplified API (the
introduced in JMS 2.0). It is not deprecated and will remain
part of JMS indefinitely.
In this tutorial, you learn how to:
- Create a Java EE 7 web application
- Develop two JavaServer Faces (JSF) managed beans:
- Develop two JSF pages:
- Modify the JSF managed beans to send and receive a JMS message
- Configure JMS resources in GlassFish Server
- Deploy the project to the GlassFish Server
- Test the project to send and receive a JMS message
Hardware and Software Requirements
The following is a list of hardware and software requirements:
- Download and install the latest JDK from this link (Java Platform, Standard Edition 7u21 recommended).
- Download and install NetBeans 7.3.1 with Java EE, which includes GlassFish 4 (Java EE download bundle) from this link. During installation, be sure to select the check box to install GlassFish. JUnit is an optional installation and is not required for this tutorial.
Before starting this tutorial, you should:
- Have installed the required software.
- Be familiar with JMS 1.1.
- Ensure that NetBeans is running.
Creating a Web Application
In this section, you create a Java EE 7 web application in the NetBeans IDE.
Creating the JMS Producer and Receiver Managed Beans
In this section, you create two JSF Managed Beans:
Creating SenderBean and ReceiverBean
Modifying SenderBean to Include the Field String Message
In this section, you modify
to implement a String message field,
Developing the JSF Pages
In this section, you create two JSF pages:
In this section, you modify the
so that the user can enter text that will be
sent as a JMS message to a
JMS Queue. You will
JMS Queue in the GlassFish Server in
a later section. Next, you modify the
page, which retrieves the messages from the
and displays it.
Modifying the sender
Modifying the receiver JSF Page
Configuring JMS Resources
In this section, you configure the JMS resource,
in the GlassFish Server. You do not configure Connection Factory
because you are using the default connection factory.
Verifying the JMS Resources
In this section, you verify the JMS resources (
Queue,myQueue, and the default connection factory,
jms/_defaultConnectionFactory) by deploying the
application to GlassFish Server.
Generating the JMS Code in the Managed Beans
In this section, you modify the JSF managed beans (
ReceiverBean). You modify
to generate JMS code to send a message to the configured
Queue,jms/myQueue. You modify
to retrieve JMS messages from the
Modifying the SenderBean
Modifying the ReceiverBean
Testing the Application
In this tutorial, you learned how to:
- Use new features added to JMS 2.0, which enable developers to write significantly fewer lines of code
- Use the JMS 2.0 simplified API in a Java EE 7 web application
- Configure JMS resources in GlassFish Server
- Send and receive JMS messages
- Java EE 7 Tutorial
- JSR 343: Java
Message Service 2.0
- What's New in JMS 2.0, Part One: Ease of Use
- To learn more about Java EE 7, refer to additional OBEs in the Java EE 7: New Features Series in Oracle Learning Library.
- Lead Curriculum Developer: Anjana Shenoy
- Editor: Susan Moxley
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.