You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by js...@apache.org on 2002/06/26 13:07:22 UTC

cvs commit: jakarta-commons-sandbox/messenger/src/java/org/apache/commons/messenger MessengerSupport.java

jstrachan    2002/06/26 04:07:22

  Modified:    messenger/src/java/org/apache/commons/messenger
                        MessengerSupport.java
  Log:
  Patched the code so that MessageConsumer instances are properly closed after they are used. Moving forward we should consider implementing pooling of MessageConsumer instances.
  
  Revision  Changes    Path
  1.23      +39 -12    jakarta-commons-sandbox/messenger/src/java/org/apache/commons/messenger/MessengerSupport.java
  
  Index: MessengerSupport.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/messenger/src/java/org/apache/commons/messenger/MessengerSupport.java,v
  retrieving revision 1.22
  retrieving revision 1.23
  diff -u -r1.22 -r1.23
  --- MessengerSupport.java	14 Jun 2002 17:50:13 -0000	1.22
  +++ MessengerSupport.java	26 Jun 2002 11:07:22 -0000	1.23
  @@ -178,7 +178,7 @@
                   message.setJMSReplyTo(replyTo);
       
                   MessageProducer producer = getMessageProducer( session, destination );
  -                MessageConsumer consumer = getMessageConsumer( session, replyTo );
  +                MessageConsumer consumer = borrowMessageConsumer( session, replyTo );
       
                   if ( isTopic( session ) ) {
                       ((TopicPublisher) producer).publish( message );
  @@ -189,6 +189,7 @@
                   return consumer.receive();
               }
               finally {
  +                returnMessageConsumer(consumer);
                   returnSession( session );
               }
           }
  @@ -206,7 +207,7 @@
               Destination replyTo = getReplyToDestination();
               message.setJMSReplyTo(replyTo);
               producer = getMessageProducer(session, destination);
  -            consumer = getMessageConsumer(session, replyTo);
  +            consumer = borrowMessageConsumer(session, replyTo);
               if (isTopic(session)) {
                   ((TopicPublisher) producer).publish(message);
               }
  @@ -216,6 +217,7 @@
               return consumer.receive(timeoutMillis);
           }
           finally {
  +            returnMessageConsumer(consumer);
               producer.close();
               returnSession(session);
           }
  @@ -223,11 +225,13 @@
       
       public Message receive(Destination destination) throws JMSException {
           Session session = borrowSession();
  +        MessageConsumer consumer = null;
           try {
  -            MessageConsumer consumer = getMessageConsumer(session, destination);
  +            consumer = borrowMessageConsumer(session, destination);
               return consumer.receive();
           }
           finally {
  +            returnMessageConsumer(consumer);
               returnSession(session);
           }
       }
  @@ -235,11 +239,13 @@
       public Message receive(Destination destination, String selector)
           throws JMSException {
           Session session = borrowSession();
  +        MessageConsumer consumer = null;
           try {
  -            MessageConsumer consumer = getMessageConsumer(session, destination, selector);
  +            consumer = borrowMessageConsumer(session, destination, selector);
               return consumer.receive();
           }
           finally {
  +            returnMessageConsumer(consumer);
               returnSession(session);
           }
       }
  @@ -247,11 +253,13 @@
       public Message receive(Destination destination, long timeoutMillis)
           throws JMSException {
           Session session = borrowSession();
  +        MessageConsumer consumer = null;
           try {
  -            MessageConsumer consumer = getMessageConsumer(session, destination);
  +            consumer = borrowMessageConsumer(session, destination);
               return consumer.receive(timeoutMillis);
           }
           finally {
  +            returnMessageConsumer(consumer);
               returnSession(session);
           }
       }
  @@ -262,22 +270,26 @@
           long timeoutMillis)
           throws JMSException {
           Session session = borrowSession();
  +        MessageConsumer consumer = null;
           try {
  -            MessageConsumer consumer = getMessageConsumer(session, destination, selector);
  +            consumer = borrowMessageConsumer(session, destination, selector);
               return consumer.receive(timeoutMillis);
           }
           finally {
  +            returnMessageConsumer(consumer);
               returnSession(session);
           }
       }
   
       public Message receiveNoWait(Destination destination) throws JMSException {
           Session session = borrowSession();
  +        MessageConsumer consumer = null;
           try {
  -            MessageConsumer consumer = getMessageConsumer(session, destination);
  +            consumer = borrowMessageConsumer(session, destination);
               return consumer.receiveNoWait();
           }
           finally {
  +            returnMessageConsumer(consumer);
               returnSession(session);
           }
       }
  @@ -285,11 +297,13 @@
       public Message receiveNoWait(Destination destination, String selector)
           throws JMSException {
           Session session = borrowSession();
  +        MessageConsumer consumer = null;
           try {
  -            MessageConsumer consumer = getMessageConsumer(session, destination, selector);
  +            consumer = borrowMessageConsumer(session, destination, selector);
               return consumer.receiveNoWait();
           }
           finally {
  +            returnMessageConsumer(consumer);
               returnSession(session);
           }
       }
  @@ -873,7 +887,7 @@
       }
       
       /** @return a MessageConsumer for the given session and destination */
  -    protected MessageConsumer getMessageConsumer(
  +    protected MessageConsumer borrowMessageConsumer(
           Session session,
           Destination destination)
           throws JMSException {
  @@ -888,7 +902,7 @@
       }
       
       /** @return a MessageConsumer for the given session, destination and selector */
  -    protected MessageConsumer getMessageConsumer(
  +    protected MessageConsumer borrowMessageConsumer(
           Session session,
           Destination destination,
           String selector)
  @@ -897,6 +911,18 @@
           return createMessageConsumer(session, destination, selector);
       }
       
  +    /** 
  +     * Returns a message consumer back to the pool. 
  +     * By default this method will close message consumers though we should
  +     * be able to cache then
  +     */
  +    protected void returnMessageConsumer(MessageConsumer messageConsumer) throws JMSException {
  +        // XXXX: could do caching one day
  +        if ( messageConsumer != null ) {
  +            messageConsumer.close();
  +        }
  +    }
  +    
       /** @return a new MessageConsumer for the given session and destination */
       protected MessageConsumer createMessageConsumer(
           Session session,
  @@ -918,6 +944,7 @@
               return queueSession.createReceiver((Queue) destination);
           }
       }
  +    
       
       /** @return a new MessageConsumer for the given session, destination and selector */
       protected MessageConsumer createMessageConsumer(
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>