Announcement Announcement Module
Collapse
No announcement yet.
JmsTemplate.receive()- Connection hangs after program runs Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • JmsTemplate.receive()- Connection hangs after program runs

    Hi all,

    I am using the receive() method on the JmsTemplate. The program runs at a set interval and counts the number of messages currently on the queue and pulls them off one at a time. I am using CLIENT_ACKNOWLEDGE mode so I have to wrap the JmsTemplate code in a transaction to prevent the template from closing the session before I can acknowledge the message. So... when the program completes, a JMS Connection is left hanging open.

    How to I force the JmsTemplate to close the connection? This is causing the java.exe to keep running and not complete. I am using Spring 3.1.0

    Code:
     //This transaction is needed to prevent the JmsTempate from closing the session after the receive method is called.
            TransactionStatus status = txManager.getTransaction(new DefaultTransactionDefinition());
    
            //Get the number of messages on the queue and process them.
            int messageCount = JmsUtil.countMessagesOnQueue(jmsTemplate, subscribeQueue);
            LOG.info("Found " + messageCount + " messages(s) on the queue.");
             for (int i = 0; i < messageCount; i++) {
    
                Message message = jmsTemplate.receive(subscribeQueue);
                //..more code here
             }
    
             message.acknowledge();
             txManager.commit(status);
    HTML Code:
    <bean name="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"
              p:connectionFactory-ref="cacheJmsConnectionFactory"
              p:sessionAcknowledgeMode="2" <!-- CLIENT_ACKNOWLEDGE -->
              p:defaultDestination-ref="publishQueue"
              p:pubSubDomain="false"
              p:receiveTimeout="-1"/> <!-- Don't wait if a message isn't there -->
    </beans>
    Thanks!
    Nathan

  • #2
    You may want to consider using JmsTemplate.execute(SessionCallback callback) instead. That manages the Session for you (including transactions) while still allowing you to use that Session to create a Consumer and then explicitly acknowledge all within the scope of the callback method you implement (after which the template will commit the TX and close the connection).

    Comment


    • #3
      Hi Mark,

      Here's what I did. Wasn't sure if I needed to close the consumer and the session, but I tried with just closing the consumer, and closing both the consumer and the session, and the connection is still hanging. I wonder what I am doing wrong? I took the transaction code out. I figured I didn't need that anymore.

      Here's the new code.
      Code:
       jmsTemplate.execute(new SessionCallback() {
                  public Object doInJms(Session session) throws JMSException {
      
                      //Get the number of messages on the queue and process them.
                      int messageCount = JmsUtil.countMessagesOnQueue(jmsTemplate, subscribeQueue);
                      LOG.info("Found " + messageCount + " messages(s) on the queue.");
                      MessageConsumer consumer = session.createConsumer(subscribeQueue);
      
                      for (int i = 0; i < messageCount; i++) {
      
                          Message message = consumer.receiveNoWait();
                          //... bunch of processing code ....
                          message.acknowledge();
                          consumer.close();                   
                          session.close();
                      return null;
                  }
              }, true);

      Comment

      Working...
      X