New Session created every time message is sent using JMSTemplate

Posted on 2011-10-11
Last Modified: 2012-05-12
I am using JMSTemplate to send message to the external system. There is limit on number of connections allowed to the external system.
I am sending the message one by one though loop.
I have seen the JMSTemplate class definition and the method doSend close the connection every time. Though my each operation to send message take individual connection and number of connection reached to the upper limit set by the external system.

I am using SingleConnectionFactory to get the connection. Below are spinet of my configuration file and code.

Spring Version: 2.0.2

Spring Configuration File:
<!-- connection factory for local queue -->
      <bean id="jmsConnectionFactory"
            <property name="targetConnectionFactory">                                          
                        <ref bean="queueConnectionFactory"/>
            <!--  <property name="username" value="" />
            <property name="password" value="" />-->

      <!-- Actual connection factory setting. Need to load the information from -->
      <bean id="queueConnectionFactory" class="">
            <property name="transportType" value="${jms.transportType}" />
            <property name="hostName" value="${jms.hostName}" />
            <property name="port" value="${jms.port}" />
            <property name="channel" value="${}" />
            <property name="SSLCipherSuite" value="${jms.ssl.cipherSuite}" />
            <property name="SSLSocketFactory" ref="sslSocketFactoryBean" />            

<!-- JMS Templates -->
    <bean id="JmsTemplate" class="org.springframework.jms.core.JmsTemplate"  abstract="true">
        <property name="connectionFactory" ref="jmsConnectionFactory" />
        <property name="sessionTransacted" value="true" />

Java Code:
jmsTemplate.convertAndSend((Object) msg, new MessagePostProcessor() {

                  public Message postProcessMessage(Message message)
                              throws JMSException {
                        m[0] = message;
                        return message;

Question by:pvasha
    LVL 5

    Expert Comment

    whats the application server you are using if use web logic. this can be done as per below,

    Creating a JMS Template
    To define the JMS template configuration attributes for destinations, use the JMS —> Templates node.
    1.      Expand the JMS —> Templates node. The JMS Templates table displays in the right pane showing all the templates defined in the domain.
    2.      Click the Configure a new JMS Template text link. A dialog displays in the right pane showing the tabs associated with configuring a new template.
    3.      On the Configuration General tab, define the general configuration attributes for the JMS template.
    •      Name of the JMS template. This name must be unique within a WebLogic Server instance, cluster, or domain. For more information, see JMS Resource Naming Rules for Domain Interoperability.
    •      Destination keys used to define the sort order for messages arriving on destinations created from this JMS template.
    For more information about the topic general attributes, see JMS Template --> Configuration --> General.
    4.      Click Create to create a template instance with the name you specified in the Name field. The new instance is added under the Templates node in the left pane.
    5.      On the Configuration Thresholds & Quotas tab, define the following upper and lower message/byte threshold and maximum quota attributes for the destinations created from this JMS template:
    •      Specify the maximum bytes or message quota that can be stored in a destination.
    •      Specify the upper threshold value that trigger events based on the number of bytes or messages stored in a
    •      destination. Events include message paging, message flow control, and system log messages.
    •      Specify the lower threshold value that trigger events based on the number of bytes or messages stored in a destination. Events include message paging, message flow control, and system log messages.
    •      Indicate whether bytes or messages paging is enabled on a destination for temporarily swapping messages out from memory to a paging store when a destination's message load reaches a specified bytes/messages threshold.
    •      Specify the maximum size of a message that will be accepted from message producers on a destination. The size includes the message body, any user-defined properties, and the user-defined JMS header fields: JMSCorrelationID and JMSType.
    For more information about the JMS template Thresholds & Quota attributes, see JMS Template --> Configuration --> Thresholds & Quotas.
    6.      On the Configuration Override tab, define the message attributes that can override those specified by a message producer for destinations created from this JMS template, including the priority, time-to-live, time-to-deliver, and delivery mode.
    For more information about the JMS template override attributes, see JMS Template --> Configuration --> Override.
    7.       On the Configuration Redelivery tab, define the message redelivery attributes for destinations created from this JMS template, including redelivery delay override, redelivery limit, and error destination.
    For more information about the JMS template redelivery attributes, see JMS Template --> Configuration --> Redelivery.
    8.        On the Configuration Expiration Policy tab, define the message expiration policy logging properties to use when expired messages are encountered on destinations created from this JMS template.
    For more information about the JMS template expiration policy attributes, see JMS Template --> Configuration --> Expiration Policy.
    •  Click Apply to save any changes you made on these tabs.

    Author Comment

    We are using Apache Tomcat container.

    Connection factory is created through the configuration file. That connection factory we are using for the sending/receiving messages.

    I forgot to paste the listener configuration in first comment. We are using DefaultMessageListener for receiving the messages.

    <!-- Abstract Message Listener Container -->
        <bean id="abstractMessageListenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer" abstract="true">          
            <property name="connectionFactory" ref="jmsConnectionFactory" />      

        <!-- Incoming Swift Message -->
          <bean id="InQueueMessageListenerContainer" parent="abstractMessageListenerContainer">
                <property name="destination" ref="swiftInQueue" />
                <property name="messageListener">
                      <bean class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
                            <constructor-arg index="0">
                                  <bean class="comm.agent.InProcessor" />
    LVL 6

    Expert Comment

    The queueConnectionFactory indicates you are sending JMS messages to IBM WebSphere MQ. It is not clear which MQ JMS client version you are using.

    I recommend to enable MQ JMS tracing and attach the trace output for our investigation -
    You may also refer to IBM Redbook on WebSphere MQ V7.0 features and enhancements (section 7.7) :

    Use the following Java properties when running your client application:

    Author Comment

    We are using a shared service, kind of an ESB, which in turn uses IBM Websphere MQ v 7. Out here on one queue manager multiple applications have their own Queues with their dedicated channels.
    The problem I am facing is that I have two listeners which listen to different queues, Queue A and Queue B, which share the same connection factory. For messages received in Queue A, i need to post the incoming message to another queue, Queue C, and then do some processing. When my application starts, 2 connections are created for the two listeners. When a message is received in Queue A, the same needs to be posted in Queue C and then do some processing. The problem we are facing is that when we post the message to Queue C, the connection still remains open and does not get close. So after some time it reaches the max number of open connections permitted and then no further messages are allowed to be put to Queue C. I want to know how can I share the connection or session between the two queues. If I cannot do that then how can I ensure that the connection which is created for posting the message to Queue C is closed after the posting is complete. I will try to get the logs as mentioned by regevha.

    Accepted Solution

    We resolved the issue using the org.springframework.jms.listener.SessionAwareMessageListener for the incoming queues. For outgoing messages, we are ensuring that we are closing the session (returning it to the pool) after every run as the messages are sent out in a Batch.

    Author Comment

    I thank all the experts who tried to help us out for finding the solution.

    Author Closing Comment

    Experts pointed out to resources which could help us out. Also we were unable to provide more details to experts for them to provide us with concrete solution. In the meanwhile we came up with the solution within our own team.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Connect to IP Camera using Java 4 62
    copyEvens challenge 6 47
    mapBully challenge 6 55
    Java Message handling in Service Layer 3 23
    For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
    Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
    Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
    Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now