I have a multi-threaded server application that uses a JavaBean to service a
client request. The bean is responsible for publishing a message to a JMS
topic. I am considering two implementations. One implementation uses
synchronization to share a topic session among all threads. The other
implementation creates a separate topic session for each thread.

My question is, which option is more efficient. Are the topic session, topic
connection and message objects lightweight enough to create a new one for
each request that is being processed? Or am I better off sharing these
objects and having the threads potentially wait to enter the synchronization
block?

Here is an example of each case. I have omitted exception handling from the
code to make the examples clearer.

1. Share a single topic session using synchronization.

public class Publish {

  private TopicSession session; // NOT thread safe !
  private TopicPublisher publisher;
  private TopicConnection tc;
  private Topic topic;
  private MapMessage msg;

  // Constructor
  public Publish() {
     Hashtable env = new Hashtable();

env.put(Context.INITIAL_CONTEXT_FACTORY,"com.swiftmq.jndi.InitialContextFact
oryImpl");
     env.put(Context.PROVIDER_URL,"smqp://localhost:4001/timeout=10000");

     InitialContext jndi = new InitialContext(env);

     // Lookup JMS connection factory
     TopicConnectionFactory conFactory =
       (TopicConnectionFactory)jndi.lookup("TopicConnectionFactory");

     tc = conFactory.createTopicConnection();

     topic = (Topic)jndi.lookup("testtopic");
     session = tc.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
     publisher = session.createPublisher(topic);
     msg = session.createMapMessage();
     tc.start();

  }

  //
  // This method will be called by multiple threads
  //
  public void processRequest() {
    //
    // Synchronize access to the topic session
    //
    synchronized (session) {
      msg.clearBody();
      msg.setString("...");
      publisher.publish(msg);
   }
}

2. Use a separate topic session for each thread

public class Publish {
  private TopicConnection tc;
  private Topic topic;

  // Constructor
  public Publish() {

     Hashtable env = new Hashtable();

env.put(Context.INITIAL_CONTEXT_FACTORY,"com.swiftmq.jndi.InitialContextFact
oryImpl");
     env.put(Context.PROVIDER_URL,"smqp://localhost:4001/timeout=10000");

     InitialContext jndi = new InitialContext(env);

     // Lookup JMS connection factory
     TopicConnectionFactory conFactory =
       (TopicConnectionFactory)jndi.lookup("TopicConnectionFactory");

     tc = conFactory.createTopicConnection();
     Topic topic = (Topic)jndi.lookup("testtopic");
     tc.start();
  }

  //
  // This method will be called by multiple threads
  //
  public void processRequest() {
    //
    // Create a new topic session and publisher for each request
    //
    TopicSession session = tc.createTopicSession(false,
Session.AUTO_ACKNOWLEDGE);
    TopicPublisher publisher = session.createPublisher(topic);
    MapMessage msg = session.createMapMessage();

    msg.setString("...");
    publisher.publish(msg);
  }
}

Regards

Cliff Straw
Compaq Computer Corporation
Tru64 UNIX Internet Engineering


------------------------------------------------------
SwiftMQ developers mailing list * http://www.swiftmq.com
To unsubscribe from this list, send an eMail to 
[EMAIL PROTECTED] and write in the body of your message:
UNSUBSCRIBE developers <your-email-address>
Archive: http://www.mail-archive.com/developers@mail.iit.de/




Reply via email to