Browsing messages in a message queue

The JMS queue can contain a number of messages that have not been processed. It is possible to use the QueueBrowser class to examine the contents of a queue. This recipe will build upon the MessageSelectorApplication to illustrate how the QueueBrowser is used.

Getting ready

We will reuse the MessageSelectorApplication as found in the Specifying which types of messages to receive using the message selector recipe.

How to do it...

In the MessageSelectorApplication, modify the PostingServlet to send a "protected" message and to incorporate the QueueBrowser as shown below:

public class PostingServlet extends HttpServlet {
@Resource(mappedName="jms/PostingsQueueFactory")
private QueueConnectionFactory queueConnectionFactory;
@Resource(mappedName="jms/PostingsQueue")
private Queue queue;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
Connection connection;
try {
connection = queueConnectionFactory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer messageProducer = (MessageProducer) session.createProducer(queue);
TextMessage textMessage = session.createTextMessage();
textMessage.setText("For your eyes only");
textMessage.setStringProperty("PostingType", "private");
messageProducer.send(textMessage);
System.out.println("---> Public textMessage sent");
textMessage.setText("Distribute freely");
textMessage.setStringProperty("PostingType", "public");
messageProducer.send(textMessage);
System.out.println("--->Private textMessage sent");
// Used by Browsing Messages in a Message Queue recipe
textMessage.setText("Distribute in house only");
textMessage.setStringProperty("PostingType", "protected");
messageProducer.send(textMessage);
QueueBrowser queueBrowser = session.createBrowser(queue);
Enumeration messages = queueBrowser.getEnumeration();
while(messages.hasMoreElements()) {
TextMessage message = (TextMessage) messages.nextElement();
System.out.println("Message: " + message.getText());
}
} catch (JMSException ex) {
Logger.getLogger(PostingServlet.class.getName()). log(Level.SEVERE, null, ex);
}
message queuemessages, browsing inout.println("<html>");
out.println("<head>");
out.println("<title>Servlet PostingServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet PostingServlet at " + request.getContextPath () + "</h1>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}

Execute the PostingServlet. Repeated execution of the servlet will result in a number of protected messages being sent to the queue as shown below. They will remain in the queue until they expire.

INFO: Message: Distribute in house only

INFO: Message: Distribute in house only

INFO: Message: Distribute in house only

INFO: Message: Distribute in house only

INFO: Message: Distribute in house only

INFO: Message: Distribute in house only

INFO: Message: Distribute in house only

How it works...

Code was added to send a "Distribute in house only" message to the queue so the QueueBrowser has something to display. Since there are no MDBs to process this type of message, they will sit there. The QueueBrowser object was obtained using the Session object's createBrowser method. The getEnumeration method returns a java.util.Enumeration object. This object was then used to list or otherwise process the messages in the queue.

There's more...

The createBrowser method also accepts a second argument specifying a message selector. The message selector will narrow down the messages returned by the QueueBrowser.

Messages will stay in a queue indefinitely. However, if the useful lifetime of a message is limited, the MessageProducer object's setTimeToLive method takes a long argument to specify when the message will expire. In addition, the overloaded send method has a version that specifies the expiration time.

producer.send(message, DeliveryMode.NON_PERSISTENT, 3, 10000);

The second argument specifies the delivery mode. The third specifies the message's priority and

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.234.28