qpid-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Praveen M <lefthandma...@gmail.com>
Subject JMS Client usage - best practices?
Date Tue, 10 Jan 2012 00:27:47 GMT
Hi,

   I'm writing a JMS Client and have a few best practices questions.

1) I understand that there are different ways ways to create a reference to
a destination to enqueue a message (using JMS createQueue, using JNDI
lookup).
    What would be the recommended way to use?

2) I create a connection to the broker on my application startup, and
create a session pool. on every enqueue, i checkout a session from my pool
use it and return to pool on enqueue.
    Does this sound reasonable?  (code sample below)

    What would be the pattern that is typically used/recommended for
session management/connection management in the client side?

    Or is this an overkill to do? Is CreateSession() and CreateProducer()
really cheap that it is just done on each enqueue?
    In my use case, I'd expect quite some concurrency (assume n (maybe
5-10) threads doing enqueues at the same time) and I'd prefer it to not
lock/block and go through asap.

    I do remember from reading the qpid client code that there is a lock
held on the connection object whenever a new session is created on that
connection. I'm assuming it is one of the places where my enqueue can slow
down
    when going in parallel if it has to create a session for every enqueue
of a message. There might be more such blocks, which I'm not aware of.

   Taking performance into mind, what would be the best design for
session/connection management?

    A snippet of what I have currently is below,

    // a thread safe queue with the available session producer pairs which
can be used to create a message and enqueue.
    ConcurrentLinkedQueue<Pair<Session, MessageProducer>>
availableSessionProducerPairs = new ConcurrentLinkedQueue<Pair<Session,
MessageProducer>> ();

   // initialize the sessionProducerPairs, create upto n pairs
    initializePool(Connection qpidConnection) {
         for (int i = 1 ; i <= SESSION_PRODUCER_PAIRS_COUNT; i ++ ) {
            Session session = qpidConnection.createSession(true,
Session.SESSION_TRANSACTED);
            MessageProducer producer = session.createProducer(null);
            availableSessionProducerPairs.add(Pair.newPair(session,
producer));
        }
    }

// on enqueue we checkout a session/producer pair use it for the enqueue
and then return it back to the session/produer pair pool.
   enqueueMessage(String queueName, byte[] message) {
            // remove the session producer pair
             Pair<Session, MessageProducer> sessionProducerPair =
availableSessionProducerPairs.poll();
        if(sessionProducerPair == null) {
            // gack here and return null.
        }
        Session session = sessionProducerPair.getFirst();
        MessageProducer producer = sessionProducerPair.getSecond();
        BytesMessage jmsMessage = session.createBytesMessage();
        jmsMessage.writeBytes(message);
        Queue queueRef = queueNameToReferenceMap.get(queueName);
        producer.send(queueRef, jmsMessage, DeliveryMode.PERSISTENT, 0, 0);
        session.commit();
        availableSessionProducersPair.add()
  }



Thank you,
-Praveen

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message