Creating a temporary queue or topic

In this post under JMS I will explain how to create temporary queue or topic.

Note: I will be using JMS 2.0 api for this and future examples

Non temporary queue or topics are created by administrator and we developers connect to those topic or queue using the below method

Destination destination = (Destination)initialContext.lookup("MyQueue");

Whereas temporary queue or topics are created at runtime by the developers and not by administrators.

The Non temporary destination (queue or topic) instance represents a physical location where data is persisted and will be available even after restart of JMS provider.

Whereas the data send to the temporary destination (queue or topic) will not be persisted permanently. The data will be present as long as the connection that created the temporary destination is alive. Once the connection is closed, the data is gone.

Below is the complete main class code

Main Class

1  package package4;
3  import java.util.Properties;
5  import javax.jms.ConnectionFactory;
6  import javax.jms.Destination;
7  import javax.jms.JMSConsumer;
8  import javax.jms.JMSContext;
9  import javax.jms.JMSException;
10 import javax.jms.JMSProducer;
11 import javax.jms.Message;
12 import javax.jms.TextMessage;
13 import javax.naming.Context;
14 import javax.naming.InitialContext;
15 import javax.naming.NamingException;
17 public class Example4 {
18  public static void main(String[] args) throws NamingException, JMSException {
19      Properties env = new Properties();
20      env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
21      env.put(Context.PROVIDER_URL, "file:///C:/openmq5_1_1/temp");
22      InitialContext ctx = new InitialContext(env);
23      ConnectionFactory conFactory = (ConnectionFactory)ctx.lookup("MyConnectionFactory");
25      JMSContext jmsContext = conFactory.createContext();
26      Destination destination = jmsContext.createTemporaryQueue();
27      JMSProducer jmsProducer = jmsContext.createProducer();
28      JMSConsumer jmsConsumer = jmsContext.createConsumer(destination);
29      jmsProducer.send(destination, "Hello World");
30      Message message = jmsConsumer.receive();
31      System.out.println(((TextMessage)message).getText());
32      jmsConsumer.close();
33      jmsContext.close();
34      ctx.close();
35  }
36 }

At line 19 I have created Properties object to populate it with INITIAL_CONTEXT_FACTORY and PROVIDER_URL.

The value of these keys vary based on the JMS provider. So please consult your specific JMS provider for more details on this.

For the example I am using OpenMessageQueue 5.1 JMS provider.

At line 22 I created an InitialContext instance and at line 23 I created a ConnectionFactory instance.

At line 25 I create an instance of JMSContext named “jmsContext” using the connectionFactory.

At line 26 we create a temporary queue by calling “createTemporaryQueue” method on jmsContext. To create a temporary topic we have method “createTemporaryTopic”.

In my examples from previous posts, we used to create Destination instance and share it between Producer and Consumer running on different threads.

In the case of temporary queue or topic we cannot do that. We have to create and use it within the same thread. In the above example we are creating the Destination object and using it in same thread. The temporary queue or topic will be alive as long as the connection that created it will be active. The moment connection is closed the temporary queue or topic is gone. Since an instance of JMSContext represents both the connection and session. The Destination object will be alive as long as instance of JMSContext is alive. The moment instance of JMSContext is closed, the temporary queue or topic is gone.

At line 27 We create a Producer object and at line 28 we create a Consumer object.

At line 29 we send the message using the Producer object and at line 30 the consumer receives the message and prints it at line 31.

In this way we can create a temporary destination (queue or topic).

Check out these products on Amazon

Leave a Reply