Writing Simple JMS Applications
This section shows how to create, package, and run simple JMS clients that are packaged as application clients.
The following topics are addressed here:
Overview of Writing Simple JMS Application
The clients demonstrate the basic tasks a JMS application must perform:
-
Creating a
JMSContext -
Creating message producers and consumers
-
Sending and receiving messages
Each example uses two clients: one that sends messages and one that receives them. You can run the clients in two terminal windows.
When you write a JMS client to run in an enterprise bean application, you use many of the same methods in much the same sequence as for an application client. However, there are some significant differences. Using the JMS API in Java EE Applications describes these differences, and this chapter provides examples that illustrate them.
The examples for this section are in the
tut-install/examples/jms/simple/ directory, under the following
subdirectories:
producer/
synchconsumer/
asynchconsumer/
messagebrowser/
clientackconsumer/
Before running the examples, you need to start GlassFish Server and create administered objects.
Starting the JMS Provider
Creating JMS Administered Objects
This example uses the following JMS administered objects:
-
A connection factory
-
Two destination resources: a topic and a queue
Before you run the applications, you can use the asadmin add-resources
command to create needed JMS resources, specifying as the argument a
file named glassfish-resources.xml. This file can be created in any
project using NetBeans IDE, although you can also create it by hand. A
file for the needed resources is present in the
jms/simple/producer/src/main/setup/ directory.
The JMS examples use a connection factory with the logical JNDI lookup
name java:comp/DefaultJMSConnectionFactory, which is preconfigured in
GlassFish Server.
You can also use the asadmin create-jms-resource command to create
resources, the asadmin list-jms-resources command to display their
names, and the asadmin delete-jms-resource command to remove them.
To Create Resources for the Simple Examples
A glassfish-resources.xml file in one of the Maven projects can create
all the resources needed for the simple examples.
-
Make sure that GlassFish Server has been started (see Starting and Stopping GlassFish Server).
-
In a command window, go to the
Producerexample.cd tut-install/jms/simple/producer -
Create the resources using the
asadmin add-resourcescommand:asadmin add-resources src/main/setup/glassfish-resources.xml -
Verify the creation of the resources:
asadmin list-jms-resourcesThe command lists the two destinations and connection factory specified in the
glassfish-resources.xmlfile in addition to the platform default connection factory:jms/MyQueue jms/MyTopic jms/__defaultConnectionFactory Command list-jms-resources executed successfully.In GlassFish Server, the Java EE
java:comp/DefaultJMSConnectionFactoryresource is mapped to a connection factory namedjms/__defaultConnectionFactory.
Building All the Simple Examples
To run the simple examples using GlassFish Server, package each example
in an application client JAR file. The application client JAR file
requires a manifest file, located in the src/main/java/META-INF/
directory for each example, along with the .class file.
The pom.xml file for each example specifies a plugin that creates an
application client JAR file. You can build the examples using either
NetBeans IDE or Maven.
The following topics are addressed here:
To Build All the Simple Examples Using NetBeans IDE
-
From the File menu, choose Open Project.
-
In the Open Project dialog box, navigate to:
-
Expand the
jmsnode and select thesimplefolder. -
Click Open Project to open all the simple examples.
-
In the Projects tab, right-click the
simpleproject and select Build to build all the examples.This command places the application client JAR files in the
targetdirectories for the examples.
To Build All the Simple Examples Using Maven
-
In a terminal window, go to the
simpledirectory:cd tut-install/jms/simple/ -
Enter the following command to build all the projects:
This command places the application client JAR files in the
targetdirectories for the examples.
Sending Messages
This section describes how to use a client to send messages. The
Producer.java client will send messages in all of these examples.
The following topics are addressed here:
General Steps Performed in the Example
General steps this example performs are as follows.
-
Inject resources for the administered objects used by the example.
-
Accept and verify command-line arguments. You can use this example to send any number of messages to either a queue or a topic, so you specify the destination type and the number of messages on the command line when you run the program.
-
Create a
JMSContext, then send the specified number of text messages in the form of strings, as described in Message Bodies. -
Send a final message of type
Messageto indicate that the consumer should expect no more messages. -
Catch any exceptions.
The Producer.java Client
The sending client, Producer.java, performs the following steps.
-
Injects resources for a connection factory, queue, and topic:
@Resource(lookup = "java:comp/DefaultJMSConnectionFactory") private static ConnectionFactory connectionFactory; @Resource(lookup = "jms/MyQueue") private static Queue queue; @Resource(lookup = "jms/MyTopic") private static Topic topic; -
Retrieves and verifies command-line arguments that specify the destination type and the number of arguments:
final int NUM_MSGS; String destType = args[0]; System.out.println("Destination type is " + destType); if ( ! ( destType.equals("queue") || destType.equals("topic") ) ) { System.err.println("Argument must be \"queue\" or " + "\"topic\""); System.exit(1); } if (args.length == 2){ NUM_MSGS = (new Integer(args[1])).intValue(); } else { NUM_MSGS = 1; } -
Assigns either the queue or the topic to a destination object, based on the specified destination type:
Destination dest = null; try { if (destType.equals("queue")) { dest = (Destination) queue; } else { dest = (Destination) topic; } } catch (Exception e) { System.err.println("Error setting destination: " + e.toString()); System.exit(1); } -
Within a
try-with-resources block, creates aJMSContext:try (JMSContext context = connectionFactory.createContext();) { -
Sets the message count to zero, then creates a
JMSProducerand sends one or more messages to the destination and increments the count. Messages in the form of strings are of theTextMessagemessage type:int count = 0; for (int i = 0; i < NUM_MSGS; i++) { String message = "This is message " + (i + 1) + " from producer"; // Comment out the following line to send many messages System.out.println("Sending message: " + message); context.createProducer().send(dest, message); count += 1; } System.out.println("Text messages sent: " + count); -
Sends an empty control message to indicate the end of the message stream:
context.createProducer().send(dest, context.createMessage());Sending an empty message of no specified type is a convenient way for an application to indicate to the consumer that the final message has arrived.
-
Catches and handles any exceptions. The end of the
try-with-resources block automatically causes theJMSContextto be closed:} catch (Exception e) { System.err.println("Exception occurred: " + e.toString()); System.exit(1); } System.exit(0);
To Run the Producer Client
You can run the client using the appclient command. The Producer
client takes one or two command-line arguments: a destination type and,
optionally, a number of messages. If you do not specify a number of
messages, the client sends one message.
You will use the client to send three messages to a queue.
-
Make sure that GlassFish Server has been started (see Starting and Stopping GlassFish Server) and that you have created resources and built the simple JMS examples (see Creating JMS Administered Objects and Building All the Simple Examples).
-
In a terminal window, go to the
producerdirectory: -
Run the
Producerprogram, sending three messages to the queue:appclient -client target/producer.jar queue 3The output of the program looks like this (along with some additional output):
Destination type is queue Sending message: This is message 1 from producer Sending message: This is message 2 from producer Sending message: This is message 3 from producer Text messages sent: 3The messages are now in the queue, waiting to be received.
Note:
When you run an application client, the command may take a long time to complete.
Receiving Messages Synchronously
This section describes the receiving client, which uses the receive
method to consume messages synchronously. This section then explains how
to run the clients using GlassFish Server.
The following topics are addressed here:
The SynchConsumer.java Client
The receiving client, SynchConsumer.java, performs the following
steps.
-
Injects resources for a connection factory, queue, and topic.
-
Assigns either the queue or the topic to a destination object, based on the specified destination type.
-
Within a
try-with-resources block, creates aJMSContext. -
Creates a
JMSConsumer, starting message delivery:consumer = context.createConsumer(dest); -
Receives the messages sent to the destination until the end-of-message-stream control message is received:
int count = 0; while (true) { Message m = consumer.receive(1000); if (m != null) { if (m instanceof TextMessage) { System.out.println( "Reading message: " + m.getBody(String.class)); count += 1; } else { break; } } } System.out.println("Messages received: " + count);Because the control message is not a
TextMessage, the receiving client terminates thewhileloop and stops receiving messages after the control message arrives. -
Catches and handles any exceptions. The end of the
try-with-resources block automatically causes theJMSContextto be closed.
The SynchConsumer client uses an indefinite while loop to receive
messages, calling receive with a timeout argument.
To Run the SynchConsumer and Producer Clients
You can run the client using the appclient command. The
SynchConsumer client takes one command-line argument, the destination
type.
These steps show how to receive and send messages synchronously using
both a queue and a topic. The steps assume you already ran the
Producer client and have three messages waiting in the queue.
-
In the same terminal window where you ran
Producer, go to thesynchconsumerdirectory: -
Run the
SynchConsumerclient, specifying the queue:appclient -client target/synchconsumer.jar queueThe output of the client looks like this (along with some additional output):
Destination type is queue Reading message: This is message 1 from producer Reading message: This is message 2 from producer Reading message: This is message 3 from producer Messages received: 3 -
Now try running the clients in the opposite order. Run the
SynchConsumerclient:appclient -client target/synchconsumer.jar queueThe client displays the destination type and then waits for messages.
-
Open a new terminal window and run the
Producerclient:cd tut-install/jms/simple/producer appclient -client target/producer.jar queue 3When the messages have been sent, the
SynchConsumerclient receives them and exits. -
Now run the
Producerclient using a topic instead of a queue:appclient -client target/producer.jar topic 3The output of the client looks like this (along with some additional output):
Destination type is topic Sending message: This is message 1 from producer Sending message: This is message 2 from producer Sending message: This is message 3 from producer Text messages sent: 3 -
Now, in the other terminal window, run the
SynchConsumerclient using the topic:appclient -client target/synchconsumer.jar topicThe result, however, is different. Because you are using a subscription on a topic, messages that were sent before you created the subscription on the topic will not be added to the subscription and delivered to the consumer. (See Publish/Subscribe Messaging Style and Consuming Messages from Topics for details.) Instead of receiving the messages, the client waits for messages to arrive.
-
Leave the
SynchConsumerclient running and run theProducerclient again:appclient -client target/producer.jar topic 3Now the
SynchConsumerclient receives the messages:Destination type is topic Reading message: This is message 1 from producer Reading message: This is message 2 from producer Reading message: This is message 3 from producer Messages received: 3Because these messages were sent after the consumer was started, the client receives them.
Using a Message Listener for Asynchronous Message Delivery
This section describes the receiving clients in an example that uses a message listener for asynchronous message delivery. This section then explains how to compile and run the clients using GlassFish Server.
Note: In the Java EE platform, message listeners can be used only in application clients, as in this example. To allow asynchronous message delivery in a web or enterprise bean application, you use a message-driven bean, shown in later examples in this chapter. |
The following topics are addressed here:
Writing the AsynchConsumer.java and TextListener.java Clients
The sending client is Producer.java, the same client used in
Sending Messages and Receiving Messages
Synchronously.
An asynchronous consumer normally runs indefinitely. This one runs until
the user types the character q or Q to stop the client.
-
The client,
AsynchConsumer.java, performs the following steps. -
Injects resources for a connection factory, queue, and topic.
-
Assigns either the queue or the topic to a destination object, based on the specified destination type.
-
In a
try-with-resources block, creates aJMSContext. -
Creates a
JMSConsumer. -
Creates an instance of the
TextListenerclass and registers it as the message listener for theJMSConsumer:listener = new TextListener(); consumer.setMessageListener(listener); -
Listens for the messages sent to the destination, stopping when the user types the character
qorQ(it uses ajava.io.InputStreamReaderto do this). -
Catches and handles any exceptions. The end of the
try-with-resources block automatically causes theJMSContextto be closed, thus stopping delivery of messages to the message listener. -
The message listener,
TextListener.java, follows these steps: -
When a message arrives, the
onMessagemethod is called automatically. -
If the message is a
TextMessage, theonMessagemethod displays its content as a string value. If the message is not a text message, it reports this fact:public void onMessage(Message m) { try { if (m instanceof TextMessage) { System.out.println( "Reading message: " + m.getBody(String.class)); } else { System.out.println("Message is not a TextMessage"); } } catch (JMSException | JMSRuntimeException e) { System.err.println("JMSException in onMessage(): " + e.toString()); } }
For this example, you will use the same connection factory and destinations you created in To Create Resources for the Simple Examples.
The steps assume that you have already built and packaged all the examples using NetBeans IDE or Maven.
To Run the AsynchConsumer and Producer Clients
You will need two terminal windows, as you did in Receiving Messages Synchronously.
-
In the terminal window where you ran the
SynchConsumerclient, go to theasynchconsumerexample directory:cd tut-install/jms/simple/asynchconsumer -
Run the
AsynchConsumerclient, specifying thetopicdestination type:appclient -client target/asynchconsumer.jar topicThe client displays the following lines (along with some additional output) and then waits for messages:
Destination type is topic To end program, enter Q or q, then <return> -
In the terminal window where you ran the
Producerclient previously, run the client again, sending three messages:appclient -client target/producer.jar topic 3The output of the client looks like this (along with some additional output):
Destination type is topic Sending message: This is message 1 from producer Sending message: This is message 2 from producer Sending message: This is message 3 from producer Text messages sent: 3In the other window, the
AsynchConsumerclient displays the following (along with some additional output):Destination type is topic To end program, enter Q or q, then <return> Reading message: This is message 1 from producer Reading message: This is message 2 from producer Reading message: This is message 3 from producer Message is not a TextMessageThe last line appears because the client has received the non-text control message sent by the
Producerclient. -
Enter
Qorqand press Return to stop theAsynchConsumerclient. -
Now run the clients using a queue.
In this case, as with the synchronous example, you can run the
Producerclient first, because there is no timing dependency between the sender and receiver:appclient -client target/producer.jar queue 3The output of the client looks like this:
Destination type is queue Sending message: This is message 1 from producer Sending message: This is message 2 from producer Sending message: This is message 3 from producer Text messages sent: 3 -
In the other window, run the
AsynchConsumerclient:appclient -client target/asynchconsumer.jar queueThe output of the client looks like this (along with some additional output):
Destination type is queue To end program, enter Q or q, then <return> Reading message: This is message 1 from producer Reading message: This is message 2 from producer Reading message: This is message 3 from producer Message is not a TextMessage -
Enter
Qorqand press Return to stop the client.
Browsing Messages on a Queue
This section describes an example that creates a QueueBrowser object
to examine messages on a queue, as described in
JMS Queue Browsers. This section then
explains how to compile, package, and run the example using GlassFish
Server.
The following topics are addressed here:
The MessageBrowser.java Client
To create a QueueBrowser for a queue, you call the
JMSContext.createBrowser method with the queue as the argument. You
obtain the messages in the queue as an Enumeration object. You can
then iterate through the Enumeration object and display the contents
of each message.
The MessageBrowser.java client performs the following steps.
-
Injects resources for a connection factory and a queue.
-
In a
try-with-resources block, creates aJMSContext. -
Creates a
QueueBrowser:QueueBrowser browser = context.createBrowser(queue); -
Retrieves the
Enumerationthat contains the messages:Enumeration msgs = browser.getEnumeration(); -
Verifies that the
Enumerationcontains messages, then displays the contents of the messages:if ( !msgs.hasMoreElements() ) { System.out.println("No messages in queue"); } else { while (msgs.hasMoreElements()) { Message tempMsg = (Message)msgs.nextElement(); System.out.println("Message: " + tempMsg); } } -
Catches and handles any exceptions. The end of the
try-with-resources block automatically causes theJMSContextto be closed.
Dumping the message contents to standard output retrieves the message
body and properties in a format that depends on the implementation of
the toString method. In GlassFish Server, the message format looks
something like this:
Text: This is message 3 from producer
Class: com.sun.messaging.jmq.jmsclient.TextMessageImpl
getJMSMessageID(): ID:8-10.152.23.26(bf:27:4:e:e7:ec)-55645-1363100335526
getJMSTimestamp(): 1129061034355
getJMSCorrelationID(): null
JMSReplyTo: null
JMSDestination: PhysicalQueue
getJMSDeliveryMode(): PERSISTENT
getJMSRedelivered(): false
getJMSType(): null
getJMSExpiration(): 0
getJMSPriority(): 4
Properties: {JMSXDeliveryCount=0}
Instead of displaying the message contents this way, you can call some
of the Message interface’s getter methods to retrieve the parts of the
message you want to see.
For this example, you will use the connection factory and queue you created for Receiving Messages Synchronously. It is assumed that you have already built and packaged all the examples.
To Run the QueueBrowser Client
To run the MessageBrowser example using the appclient command,
follow these steps.
You also need the Producer example to send the message to the queue,
and one of the consumer clients to consume the messages after you
inspect them.
To run the clients, you need two terminal windows.
-
In a terminal window, go to the
producerdirectory:cd tut-install/examples/jms/simple/producer/ -
Run the
Producerclient, sending one message to the queue, along with the non-text control message:appclient -client target/producer.jar queueThe output of the client looks like this (along with some additional output):
Destination type is queue Sending message: This is message 1 from producer Text messages sent: 1 -
In another terminal window, go to the
messagebrowserdirectory:cd tut-install/jms/simple/messagebrowser -
Run the
MessageBrowserclient using the following command:appclient -client target/messagebrowser.jarThe output of the client looks something like this (along with some additional output):
Message: Text: This is message 1 from producer Class: com.sun.messaging.jmq.jmsclient.TextMessageImpl getJMSMessageID(): ID:9-10.152.23.26(bf:27:4:e:e7:ec)-55645-1363100335526 getJMSTimestamp(): 1363100335526 getJMSCorrelationID(): null JMSReplyTo: null JMSDestination: PhysicalQueue getJMSDeliveryMode(): PERSISTENT getJMSRedelivered(): false getJMSType(): null getJMSExpiration(): 0 getJMSPriority(): 4 Properties: {JMSXDeliveryCount=0} Message: Class: com.sun.messaging.jmq.jmsclient.MessageImpl getJMSMessageID(): ID:10-10.152.23.26(bf:27:4:e:e7:ec)-55645-1363100335526 getJMSTimestamp(): 1363100335526 getJMSCorrelationID(): null JMSReplyTo: null JMSDestination: PhysicalQueue getJMSDeliveryMode(): PERSISTENT getJMSRedelivered(): false getJMSType(): null getJMSExpiration(): 0 getJMSPriority(): 4 Properties: {JMSXDeliveryCount=0}The first message is the
TextMessage, and the second is the non-text control message. -
Go to the
synchconsumerdirectory. -
Run the
SynchConsumerclient to consume the messages:appclient -client target/synchconsumer.jar queueThe output of the client looks like this (along with some additional output):
Destination type is queue Reading message: This is message 1 from producer Messages received: 1
Running Multiple Consumers on the Same Destination
To illustrate further the way point-to-point and publish/subscribe
messaging works, you can use the Producer and SynchConsumer examples
to send messages that are then consumed by two clients running
simultaneously.
-
Open three command windows. In one, go to the
producerdirectory. In the other two, go to thesynchconsumerdirectory. -
In each of the
synchconsumerwindows, start running the client, receiving messages from a queue:appclient -client target/synchconsumer.jar queueWait until you see the "Destination type is queue" message in both windows.
-
In the
producerwindow, run the client, sending 20 or so messages to the queue:appclient -client target/producer.jar queue 20 -
Look at the output in the
synchconsumerwindows. In point-to-point messaging, each message can have only one consumer. Therefore, each of the clients receives some of the messages. One of the clients receives the non-text control message, reports the number of messages received, and exits. -
In the window of the client that did not receive the non-text control message, enter Control-C to exit the program.
-
Next, run the
synchconsumerclients using a topic. In each window, run the following command:appclient -client target/synchconsumer.jar topicWait until you see the "Destination type is topic" message in both windows.
-
In the
producerwindow, run the client, sending 20 or so messages to the topic:appclient -client target/producer.jar topic 20 -
Again, look at the output in the
synchconsumerwindows. In publish/subscribe messaging, a copy of every message is sent to each subscription on the topic. Therefore, each of the clients receives all 20 text messages as well as the non-text control message.
Acknowledging Messages
JMS provides two alternative ways for a consuming client to ensure that a message is not acknowledged until the application has finished processing the message:
-
Using a synchronous consumer in a
JMSContextthat has been configured to use theCLIENT_ACKNOWLEDGEsetting -
Using a message listener for asynchronous message delivery in a
JMSContextthat has been configured to use the defaultAUTO_ACKNOWLEDGEsetting
Note: In the Java EE platform, |
The clientackconsumer example demonstrates the first alternative, in
which a synchronous consumer uses client acknowledgment. The
asynchconsumer example described in Using a Message
Listener for Asynchronous Message Delivery demonstrates the second
alternative.
The following table describes four possible interactions between types of consumers and types of acknowledgment.
Table 49-3 Message Acknowledgment with Synchronous and Asynchronous Consumers
Consumer Type |
Acknowledgment Type |
Behavior |
Synchronous |
Client |
Client acknowledges message after processing is complete |
Asynchronous |
Client |
Client acknowledges message after processing is complete |
Synchronous |
Auto |
Acknowledgment happens immediately after |
Asynchronous |
Auto |
Message is automatically acknowledged when
|
The example is under the tut-install`/examples/jms/simple/clientackconsumer/` directory.
The example client, ClientAckConsumer.java, creates a JMSContext
that specifies client acknowledgment:
try (JMSContext context =
connectionFactory.createContext(JMSContext.CLIENT_ACKNOWLEDGE);) {
...
The client uses a while loop almost identical to that used by
SynchConsumer.java, with the exception that after processing each
message, it calls the acknowledge method on the JMSContext:
The example uses the following objects:
-
The
jms/MyQueueresource that you created for Receiving Messages Synchronously. -
java:comp/DefaultJMSConnectionFactory, the platform default connection factory preconfigured with GlassFish Server
To Run the ClientAckConsumer Client
-
In a terminal window, go to the following directory:
tut-install/examples/jms/simple/producer/ -
Run the
Producerclient, sending some messages to the queue:appclient -client target/producer.jar queue 3 -
In another terminal window, go to the following directory:
tut-install/examples/jms/simple/clientackconsumer/ -
To run the client, use the following command:
appclient -client target/clientackconsumer.jarThe client output looks like this (along with some additional output):
Created client-acknowledge JMSContext Reading message: This is message 1 from producer Acknowledging TextMessage Reading message: This is message 2 from producer Acknowledging TextMessage Reading message: This is message 3 from producer Acknowledging TextMessage Acknowledging non-text control messageThe client acknowledges each message explicitly after processing it, just as a
JMSContextconfigured to useAUTO_ACKNOWLEDGEdoes automatically after aMessageListenerreturns successfully from processing a message received asynchronously.