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 the Application Server.
The following sections describe the steps in creating and running the example:
To create a QueueBrowser for a queue, you call the Session.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/src/java/MessageBrowser.java program performs the following steps:
Injects resources for a connection factory and a queue.
Creates a Connection and a Session.
Creates a QueueBrowser:
QueueBrowser browser = session.createBrowser(queue);
Retrieves the Enumeration that contains the messages:
Enumeration msgs = browser.getEnumeration();
Verifies that the Enumeration contains 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); } }
Closes the connection, which automatically closes the session and QueueBrowser.
The format in which the message contents appear is implementation-specific. In the Application Server, the message format looks like this:
Message contents: Text: This is message 3 Class: com.sun.messaging.jmq.jmsclient.TextMessageImpl getJMSMessageID(): ID:14-129.148.71.199(f9:86:a2:d5:46:9b)-40814-1129061034355 getJMSTimestamp(): 1129061034355 getJMSCorrelationID(): null JMSReplyTo: null JMSDestination: PhysicalQueue getJMSDeliveryMode(): PERSISTENT getJMSRedelivered(): false getJMSType(): null getJMSExpiration(): 0 getJMSPriority(): 4 Properties: null |
You will use the connection factory and queue you created in Creating JMS Administered Objects for the Synchronous Receive Example.
To compile and package the MessageBrowser example using NetBeans IDE, follow these steps:
In NetBeans IDE, choose Open Project from the File menu.
In the Open Project dialog, navigate to tut-install/javaeetutorial5/examples/jms/simple/.
Select the messagebrowser folder.
Select the Open as Main Project check box.
Click Open Project.
Right-click the project and choose Build.
To compile and package the MessageBrowser example using Ant, follow these steps:
In a terminal window, go to the messagebrowser directory. If you are currently in the asynchconsumer/dist directory, you need to go up two levels:
cd ../../messagebrowser |
Type the following command:
ant |
The targets place the application client JAR file in the dist directory for the example.
You also need the Producer example to send the message to the queue, and one of the consumer programs to consume the messages after you inspect them. If you did not do so already, package these examples.
To run the programs using NetBeans IDE, follow these steps.
Run the Producer program, sending one message to the queue:
Right-click the producer project and choose Properties.
Select Run from the Categories tree.
In the Arguments field, type the following:
queue |
Click OK.
Right-click the project and choose Run.
The output of the program looks like this:
Destination type is queue Sending message: This is message 1 |
Run the MessageBrowser program. Right-click the messagebrowser project and choose Run.
The output of the program looks like this:
Message: Text: This is message 1 Class: com.sun.messaging.jmq.jmsclient.TextMessageImpl getJMSMessageID(): ID:12-129.148.71.199(8c:34:4a:1a:1b:b8)-40883-1129062957611 getJMSTimestamp(): 1129062957611 getJMSCorrelationID(): null JMSReplyTo: null JMSDestination: PhysicalQueue getJMSDeliveryMode(): PERSISTENT getJMSRedelivered(): false getJMSType(): null getJMSExpiration(): 0 getJMSPriority(): 4 Properties: null Message: Class: com.sun.messaging.jmq.jmsclient.MessageImpl getJMSMessageID(): ID:13-129.148.71.199(8c:34:4a:1a:1b:b8)-40883-1129062957616 getJMSTimestamp(): 1129062957616 getJMSCorrelationID(): null JMSReplyTo: null JMSDestination: PhysicalQueue getJMSDeliveryMode(): PERSISTENT getJMSRedelivered(): false getJMSType(): null getJMSExpiration(): 0 getJMSPriority(): 4 Properties: null |
The first message is the TextMessage, and the second is the non-text control message.
Run the SynchConsumer program to consume the messages.
Right-click the synchconsumer project and choose Properties.
Select Run from the Categories tree.
In the Arguments field, type the following:
queue |
Click OK.
Right-click the project and choose Run.
The output of the program looks like this:
Destination type is queue Reading message: This is message 1 |
To run the clients using the appclient command, follow these steps. You may want to use two terminal windows.
Go to the producer/dist directory.
Run the Producer program, sending one message to the queue:
appclient -client producer.jar queue |
The output of the program looks like this:
Destination type is queue Sending message: This is message 1 |
Go to the messagebrowser/dist directory.
Run the MessageBrowser program:
appclient -client messagebrowser.jar |
The output of the program looks like this:
Message: Text: This is message 1 Class: com.sun.messaging.jmq.jmsclient.TextMessageImpl getJMSMessageID(): ID:12-129.148.71.199(8c:34:4a:1a:1b:b8)-40883-1129062957611 getJMSTimestamp(): 1129062957611 getJMSCorrelationID(): null JMSReplyTo: null JMSDestination: PhysicalQueue getJMSDeliveryMode(): PERSISTENT getJMSRedelivered(): false getJMSType(): null getJMSExpiration(): 0 getJMSPriority(): 4 Properties: null Message: Class: com.sun.messaging.jmq.jmsclient.MessageImpl getJMSMessageID(): ID:13-129.148.71.199(8c:34:4a:1a:1b:b8)-40883-1129062957616 getJMSTimestamp(): 1129062957616 getJMSCorrelationID(): null JMSReplyTo: null JMSDestination: PhysicalQueue getJMSDeliveryMode(): PERSISTENT getJMSRedelivered(): false getJMSType(): null getJMSExpiration(): 0 getJMSPriority(): 4 Properties: null |
The first message is the TextMessage, and the second is the non-text control message.
Go to the synchconsumer/dist directory.
Run the SynchConsumer program to consume the messages:
appclient -client synchconsumer.jar queue |
The output of the program looks like this:
Destination type is queue Reading message: This is message 1 |