View Full Version : Reconnecting JmsTemplate after JMS Server failure
gt1601
Aug 4th, 2005, 10:21 PM
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
Juergen Hoeller
Aug 6th, 2005, 03:51 PM
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
jstrachan
Aug 7th, 2005, 04:18 AM
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 (http://activemq.org) then you can enable auto-reconnection using the reliable: protocol. e.g. connect via the URL
reliable:tcp://host:port
and the JMS client will automatically reconnect for you under the covers without having to change your JMS code or fiddle with the JmsTemplate
gt1601
Aug 8th, 2005, 05:44 PM
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
jstrachan
Aug 15th, 2005, 11:08 AM
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>
dode
Jan 17th, 2006, 07:26 AM
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
sunit singh
Oct 10th, 2012, 03:15 PM
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
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
Powered by vBulletin® Version 4.2.1 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.