Announcement Announcement Module
Collapse
No announcement yet.
Reconnecting JmsTemplate after JMS Server failure Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Reconnecting JmsTemplate after JMS Server failure

    All,
    I would like to use JmsTemplate to publish messages to a JMS server but I am not sure how the JmsTemplate will behave when the JMS server goes down for 5 minutes and then comes back up again.
    Do I need to restart the application to get the JmsTemplate to reconnect to the JMS Server?

    Thanks in advance,
    -Guillermo

  • #2
    The actual behavior depends on your JMS ConnectionFactory. JmsTemplate will fetch a Connection from the ConnectionFactory for each operation, putting the onus of Connection management onto the ConnectionFactory.

    In a J2EE environment, the ConnectionFactory will usually return transactional Connections, i.e. the same Connection for an entire transaction. As a further example (unrelated to your use case), you could define Spring's SingleConnectionFactory, holding a single JMS Connection and exposing it to all ConnectionFactory callers.

    In terms of reconnect behavior, concrete JMS providers will behave differently. If you're not happy with the raw behavior of your JMS provider / J2EE server, consider writing a ConnectionFactory proxy where you first check whether the Connection returned by the target ConnectionFactory is still valid, making sure to return an active Connection to the caller (i.e. JmsTemplate). This would be similar to how JDBC connection pools behave.

    Juergen

    Comment


    • #3
      Re: Reconnecting JmsTemplate after JMS Server failure

      Originally posted by gt1601
      All,
      I would like to use JmsTemplate to publish messages to a JMS server but I am not sure how the JmsTemplate will behave when the JMS server goes down for 5 minutes and then comes back up again.
      Do I need to restart the application to get the JmsTemplate to reconnect to the JMS Server?
      -Guillermo
      If you use a decent JMS provider like ActiveMQ then you can enable auto-reconnection using the reliable: protocol. e.g. connect via the URL

      reliable:tcp://hostort

      and the JMS client will automatically reconnect for you under the covers without having to change your JMS code or fiddle with the JmsTemplate

      Comment


      • #4
        Thanks for your response guys.

        I didn't know I could use the reliable protocol for only 1 server. What version of ActiveMQ do I need to do this?

        Is there an example of a Spring and ActiveMQ that uses the reliable protocol?.

        -Guillermo

        Comment


        • #5
          Originally posted by gt1601
          Thanks for your response guys.

          I didn't know I could use the reliable protocol for only 1 server. What version of ActiveMQ do I need to do this?

          Is there an example of a Spring and ActiveMQ that uses the reliable protocol?.

          -Guillermo
          Reliable connections have been in ActiveMQ from around 2.0 I think (my memories a bit hazy, its been there for a long long time). Certainly if you use a recent version, like 3.1-M6 its all there working fine.

          The only requirement for the use of reliable protocol is prefixing your connection URL string with 'reliable:' on the JMS ConnectionFactory side of things, so any of the Spring / ActiveMQ examples should do the trick.

          e.g.

          <bean id="jmsFactory" class="org.activemq.ActiveMQConnectionFactory">
          <property name="brokerURL" value="reliable:tcp://localhost:61616"/>
          </bean>

          Comment


          • #6
            JmsTemplate and JBoss clustering

            Hello,

            I have a web application here that uses Spring to send JMS messages to JBossMQ queues. Works fine.

            Now I set up JBoss clustering and "configured" Spring to use JBoss HA-JMS/HA-JNDI:

            java.naming.provider.url=node1:1100,node2:1100

            Another requirement for the JMS client to use clustering is that it must listen for server exceptions and reconnect. As far as I know, this is usually done by registering an ExceptionListener on the JMS Connection which will take care of reconnecting.

            I have done that with a litte test JMS client and failover to another node works fine.

            But how can I do this with Spring/JmsTemplate? I am completely new to Spring and I have no idea yet. Is it possible at all or should it be done with another approach (ConnectionFactory proxy as mentioned here)?

            Thanks in advance,
            Torsten

            Comment


            • #7
              JMS reconnect not happening m using a cachinconnectionfactory to connect to a MQ(IBM)

              Hi Juergen,

              m fairly new to spring and have two questions , please oblige me...

              1) in my scenario where m trying to connect to an MQ , when the QManager goes down , while the defaultmessagelistner container works fine like after the MQ QMGR is restarted it continues to recieve message by automatically reconnecting...while sending the same message fails coz the jmstemplate does'nt reconnect automatically. m using a cachingconnectionfactory for both container and jmstemplate , and the setreconnect is set to true. why does this happen??

              2)what will the connectionproxy look like if i have to write one for this problem? like wat do i do in the code?

              m in a big fix......highly appreciate any prompt help on this.....thanks in advance

              regards
              sunit

              Originally posted by Juergen Hoeller View Post
              The actual behavior depends on your JMS ConnectionFactory. JmsTemplate will fetch a Connection from the ConnectionFactory for each operation, putting the onus of Connection management onto the ConnectionFactory.

              In a J2EE environment, the ConnectionFactory will usually return transactional Connections, i.e. the same Connection for an entire transaction. As a further example (unrelated to your use case), you could define Spring's SingleConnectionFactory, holding a single JMS Connection and exposing it to all ConnectionFactory callers.

              In terms of reconnect behavior, concrete JMS providers will behave differently. If you're not happy with the raw behavior of your JMS provider / J2EE server, consider writing a ConnectionFactory proxy where you first check whether the Connection returned by the target ConnectionFactory is still valid, making sure to return an active Connection to the caller (i.e. JmsTemplate). This would be similar to how JDBC connection pools behave.

              Juergen

              Comment

              Working...
              X