Question

I am just starting to gain knowledge and use JMS(activemq). The pseudo code looks like below

// 1. TopicConnectionFactory tcf = (TopicConnectionFactory) ctx.lookup("XXConnFactory");

// 2. TopicConnection connection = tcf.createTopicConnection();

// 3. TopicSession topicSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);

then using the topicSession i create publisher and call publish() method.

Question is, when do I initialize TopicConnection/TopicSession. Is it like I can have TopicConnection initialized once and use the same connection to get a session for each calls?

Basically I want to know which can be instantiated once and which should be instantiated for every call.

Was it helpful?

Solution

  • The TopicConnection normally manages the TCP connection to the JMS provider. So normally one connection is enough.
  • The TopicSession is a single-threaded context (to manage transactions), so it's required on a per thread basis. It's lightweight.

You can find more details in the Javadoc:

A quote from the Javadoc of Connection:

A JMS client typically creates a connection, one or more sessions, and a number of message producers and consumers.

OTHER TIPS

Basically I want to know which can be instantiated once and which should be instantiated for every call.

To answer this question, you could do the initialization as many time as you want. As long as you are releasing each of the session properly and closing the connection properly it is not an issue.

But from performance perspective, what should be the way?

Well, the answer depends on how frequently you are interacting with the queue. Say you want to perform once in a while read / write to the queue/topic, go for putting all the initialization code in a method, which would initialize the session , read/write data and close it.

But say, you have to perform the read/write very often, it is a good idea to initialize the session and keep the reference in an instance variable and then you method would simply use this session to put data.

Keep the operation in try-catch to catch any exception (in all the cases). And use a finally block to close the session/connection.

After all read-write is over, close the session and connection through a finally block.

It is enough to call the below comment once if you want to create only one connection.

       Context ctx  = new InitialContext();

        // lookup the topic connection factory
        TopicConnectionFactory connFactory = (TopicConnectionFactory) ctx
                .lookup("/RemoteConnectionFactory");

        // lookup the topic object
        Topic topic = (Topic) ctx.lookup("java:/"+prop.TOPIC);

        // create a topic connection
        TopicConnection topicConn = connFactory.createTopicConnection(prop.USERPUB,prop.PASSPUB);

          //create topic session
         TopicSession topicSession = topicConn.createTopicSession(true, Session.AUTO_ACKNOWLEDGE);

         //create topic publisher
         MessageProducer topicPublisher = topicSession.createPublisher(topic);
         topicPublisher.setDeliveryMode(DeliveryMode.PERSISTENT);

Call each time you want to publish a new message to the topic. // create the message message = topicSession.createTextMessage(); message.setText(this.publishMsg);

        // publish the messages
        Long timeLng = System.currentTimeMillis();
        message.setJMSCorrelationID(timeLng.toString());
        topicPublisher.send(message);
        topicSession.commit();

If you want to create a new topic, call following with the topic name

       MessageProducer topicPublisher = topicSession.createPublisher(topic);
       topicPublisher.setDeliveryMode(DeliveryMode.PERSISTENT);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top