Interview Questions

How do I create a producer pool?

BEA WebLogic Questions and Answers


(Continued from previous question...)

How do I create a producer pool?

The following is some pseudo-code
 for a producer class.

    class ProducerPool {
      static Hashmap pSets = new Hashtable();
      static Hashmap inUse = new Hashtable();

    QueueSender get(String contextURL,
     String connectionFactoryName,
     String destinationName) {
     String lookup = contextURL+";
     "+connectionFactName+";"+destName;
     synchronized(pSets) {
      producer set = pSets.get(lookup);
      if (set != null && set not empty)
       qs = set.removeFirst();
     }
     if (producer == null) {
       create ctx
       get connect factory
       create connection
       create session
       look up destination
       qs = create queue sender
     }
     synchronized(inUse) {
      inUse.put(qs, lookup);
     }
     return qs;
    }

    void put(QueueSender qs) {
      String lookup;
      synchronized(inUse) {
       lookup = inUse.remove(p);
      }
      synchronzied(pSets) {
       producer set = pSets.get(lookup);
       if (set == null) {
        producer set = new producer set
        pSets.put(lookup, producer set);
       }
       producer set.add(qs);
      }
     }
    }

Note: Static classes may be garbage collected if there are no references to them, so make sure the application server has a permanent pointer to them in some manner. One way is to reference it permanently from within a servlet or EJB when they are initialized at startup.
Here is an example of using the producer pool within the onMessage method.

    onMessage() {
      QueueSender qs = ProducerPool.get(...);
      qs.send(...);
      ProducerPool.put(qs);
    }

You can pre-populate this pool by calling it from 
a startup class or a load-on-start servlet class.

(Continued on next question...)

Other Interview Questions