This example shows you how can use shared consumers to share a subscription on a topic. In JMS 1.1 this was not allowed and so caused a scalability issue. In JMS 2 this restriction has been lifted so you can share the load across different threads and connections.
To run the example, simply type mvn verify -Pexample
from this directory
client-jndi.properties
file in the directory ../common/config
InitialContext initialContext = getContext();
Topic topic = (Topic) initialContext.lookup("/topic/exampleTopic");
ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("/ConnectionFactory");
jmsContext = cf.createContext();
JMSProducer producer = jmsContext.createProducer();
JMSConsumer jmsConsumer = jmsContext.createSharedConsumer(topic, "sc1");
jmsContext2 = cf.createContext();
JMSConsumer jmsConsumer2 = jmsContext2.createSharedConsumer(topic, "sc1");
producer.send(topic, "this is a String!");
producer.send(topic, "this is a second String!") ;
String body = jmsConsumer.receiveBody(String.class, 5000);
body = jmsConsumer2.receiveBody(String.class, 5000);
finally
block. Closing a JMS connection will automatically close all of its sessions, consumers, producer and browser objects
finally
{
if (initialContext != null)
{
initialContext.close();
}
if (jmsContext != null)
{
jmsContext.close();
}
if (jmsContext2 != null)
{
jmsContext2.close();
}
}