Java Message Service: Browsing Queue

JMS also supports inspecting messages in a queue without receiving them. The following program prints all messages in a queue AQueue, else prints a message if the queue is empty.

import javax.jms.*; public class Browser {

public static void main(String[] args) {

try {

ConnectionFactory cf = new com.sun.messaging.ConnectionFactory();

((com.sun.messaging.ConnectionFactory)cf).setProperty(com.sun.messaging.Connect ionConfiguration.imqAddressList,””);

Connection con = cf.createConnection();

Session sn = con.createSession(false, Session.AUTO_ACKNOWLEDGE);

Destination dest = sn.createQueue(”AQueue”);

QueueBrowser browser = sn.createBrowser((Queue)dest);

java.util.Enumeration msgs = browser.getEnumeration();

if ( !msgs.hasMoreElements() )

System.out.println(”No messages in queue”);

else {

while (msgs.hasMoreElements()) {

Message tempMsg = (Message)msgs.nextElement();

System.out.println(”Message: ” + tempMsg);





} catch (Exception e) { e.printStackTrace(); }



Make sure that a queue with name AQueue exists before running the program.

Source: Uttam Kumar Roy (2015), Advanced Java programming, Oxford University Press.

Leave a Reply

Your email address will not be published. Required fields are marked *