In this post under JMS, I will explain with an example what is QueueBrowser and how to use it.
Note: I will be using JMS 2.0 api for this and future examples.
The name “QueueBrowser” implies two things
1) It can be used only for queue that means in point to point messaging model.
2) It is used to just look the queue contents and not to consume them.
When QueueBrowser is created, the queue which has to be scanned is given to the QueueBrowser as an argument.
The QueueBrowser makes a copy (i.e., snapshot) of the queue contents and lets us look through the queue contents.
Since QueueBrowser makes a copy of the queue, any update to the actual queue (i.e., addition of new JMS message to the queue) is not reflected in the QueueBrowser.
QueueBrowser instance is created by calling “createBrowser” method on JMSContext instance and passing the queue destination as the argument as shown below
QueueBrowser queueBrowser = jmsContext.createBrowser((Queue)destination);
Once the QueueBrowser instance is created we can browse the contents as shown below
1 Enumeration enumeration = queueBrowser.getEnumeration();
2 while(enumeration.hasMoreElements()) {
3 TextMessage textMessage = (TextMessage)enumeration.nextElement();
4 System.out.println(textMessage.getText());
5 }
In the above code, I am assuming each message in QueueBrowser will be of type text messages, so casting each message to TextMessage at line 3.
We use combination of “hasMoreElements” and “nextElement” methods to browse the QueueBrowser instance.
Below is the complete code for your reference.
Main Class
1 package package7;
2
3 import java.util.Properties;
4
5 import javax.jms.ConnectionFactory;
6 import javax.jms.Destination;
7 import javax.jms.JMSException;
8 import javax.naming.Context;
9 import javax.naming.InitialContext;
10 import javax.naming.NamingException;
11
12 public class Example7 {
13 public static void main(String[] args) throws NamingException, JMSException, InterruptedException {
14 Properties env = new Properties();
15 env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
16 env.put(Context.PROVIDER_URL, "file:///C:/openmq5_1_1/temp");
17 InitialContext initialContext = new InitialContext(env);
18 ConnectionFactory connectionFactory = (ConnectionFactory)initialContext.lookup("MyConnectionFactory");
19 Destination destination = (Destination)initialContext.lookup("MyQueue");
20
21 Producer producer = new Producer(destination, connectionFactory);
22 Consumer consumer = new Consumer(destination, connectionFactory);
23
24 Thread producerThread = new Thread(producer);
25 Thread consumerThread = new Thread(consumer);
26
27 producerThread.start();
28 consumerThread.start();
29
30 initialContext.close();
31 }
32 }
Below is the Producer code
Producer
1 package package7;
2
3 import javax.jms.ConnectionFactory;
4 import javax.jms.Destination;
5 import javax.jms.JMSContext;
6 import javax.jms.JMSProducer;
7
8 public class Producer implements Runnable {
9 private Destination destination;
10 private ConnectionFactory connectionFactory;
11
12 public Producer(Destination destination, ConnectionFactory connectionFactory) {
13 this.destination = destination;
14 this.connectionFactory = connectionFactory;
15 }
16
17 @Override
18 public void run() {
19 JMSContext jmsContext = null;
20
21 try {
22 jmsContext = connectionFactory.createContext();
23 JMSProducer jmsProducer = jmsContext.createProducer();
24 jmsProducer.send(destination, "Hello World 1");
25 jmsProducer.send(destination, "Hello World 2");
26 jmsProducer.send(destination, "Hello World 3");
27 jmsProducer.send(destination, "Hello World 4");
28 jmsProducer.send(destination, "Hello World 5");
29 jmsProducer.send(destination, "Hello World 6");
30 jmsProducer.send(destination, "Hello World 7");
31 } catch(Exception excep) {
32 excep.printStackTrace();
33 } finally {
34 if(jmsContext != null) {
35 jmsContext.close();
36 }
37 }
38 }
39 }
Below is the Consumer code
Consumer
1 package package7;
2
3 import java.util.Enumeration;
4
5 import javax.jms.ConnectionFactory;
6 import javax.jms.Destination;
7 import javax.jms.JMSConsumer;
8 import javax.jms.JMSContext;
9 import javax.jms.Queue;
10 import javax.jms.QueueBrowser;
11 import javax.jms.TextMessage;
12
13 public class Consumer implements Runnable {
14 private Destination destination;
15 private ConnectionFactory connectionFactory;
16
17 public Consumer(Destination destination, ConnectionFactory connectionFactory) {
18 this.destination = destination;
19 this.connectionFactory = connectionFactory;
20 }
21
22 @Override
23 public void run() {
24 JMSContext jmsContext = null;
25 try {
26 jmsContext = connectionFactory.createContext();
27 JMSConsumer jmsConsumer = jmsContext.createConsumer(destination);
28 QueueBrowser queueBrowser = jmsContext.createBrowser((Queue)destination);
29 Enumeration enumeration = queueBrowser.getEnumeration();
30 System.out.println("Output from QueueBrowser");
31 while(enumeration.hasMoreElements()) {
32 TextMessage textMessage = (TextMessage)enumeration.nextElement();
33 System.out.println(textMessage.getText());
34 }
35 System.out.println("Output from the actual Queue");
36 for(int i = 0; i < 7; i++) {
37 System.out.println("Removing message: " + (i + 1));
38 jmsConsumer.receive();
39 }
40 jmsConsumer.close();
41 } catch(Exception excep) {
42 excep.printStackTrace();
43 } finally {
44 if(jmsContext != null) {
45 jmsContext.close();
46 }
47 }
48 }
49 }
Below is the output
Output
Output from QueueBrowser
Hello World 1
Output from the actual Queue
Removing message: 1
Removing message: 2
Removing message: 3
Removing message: 4
Removing message: 5
Removing message: 6
Removing message: 7
As you can see from the output, When the QueueBrowser was created there was only one message in the queue. When the actual queue is processed we find all the 7 messages sent.