activemq-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jamie <ja...@mailarchiva.com>
Subject session & producer creation
Date Mon, 07 Jul 2014 04:59:25 GMT
Greetings

  A friend recommended that a new Producer and Session should be created 
for every message put on the queue. See below Is this correct?

-------------------- put message on queue ------------

         try {
             new ProducerTask<Void>() {

                 @Override
                 public Destination getDestination() { return destination; }

                 @Override
                 protected Void use(MessageProducer mc, Session s) 
throws Exception {
                     try {
                         TextMessage message = 
s.createTextMessage(fileQueueItem.getValue());
                         mc.send(message);
                     } catch (JMSException e) {
                         throw new FileQueueException("failed to send 
message:"+e.getMessage(),e,logger);
                     }
                     return null;
                 }

                 @Override
                 public Connection getConnection() {
                     return connection;
                 }

             }.call();
         } catch (Exception e) {
             logger.error("failed to handle message:"+e.getMessage(),e);
         }

-------------------- session task ------------

     private abstract class SessionTask<V> implements Callable<V> {

         @Override public final V call() throws Exception {
             final Session s = getConnection().createSession(false, 
Session.CLIENT_ACKNOWLEDGE);
             try {
                 return use(s);
             } finally {
                 s.close();
             }
         }

         protected abstract V use(Session s) throws Exception;

         public abstract Connection getConnection();
     }


--------------------  producer task ------------

  private abstract class ProducerTask<V> implements Callable<V> {

             public abstract Destination getDestination();

             @Override public V call() throws Exception {
                 return new SessionTask<V>() {
                     @Override public V use(final Session s) throws 
Exception {
                         final MessageProducer mp = 
s.createProducer(getDestination());
                         mp.setDeliveryMode(DeliveryMode.PERSISTENT);
                         try {
                             return ProducerTask.this.use(mp, s);
                         } finally {
                             mp.close();
                         }
                     }

                     public Connection getConnection() {
                         return ProducerTask.this.getConnection();
                     }

                 }.call();


             }

             public abstract Connection getConnection();

             protected abstract V use(MessageProducer mp, Session s) 
throws Exception;
         }





Mime
View raw message