Reputation: 1028
I am trying to use Spring's SingleConnectionFactory, where there are 2 connection factories, and only one of them should be able to connect at a time.
<bean id="connectionFactory1" class="org.springframework.jms.connection.SingleConnectionFactory102">
<property name="targetConnectionFactory">
<bean class="org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter">
<property name="username" value="LOGIN" />
<property name="password" value="PASS" />
<property name="targetConnectionFactory">
<bean class="com.tibco.tibjms.TibjmsQueueConnectionFactory">
<constructor-arg value="URL" />
<constructor-arg value="CLIENT_ID" />
</bean>
</property>
</bean>
</property>
<property name="reconnectOnException" value="true" />
</bean>
<bean id="connectionFactory2" class="org.springframework.jms.connection.SingleConnectionFactory102">
<property name="targetConnectionFactory">
<bean class="org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter">
<property name="username" value="LOGIN" />
<property name="password" value="PASS" />
<property name="targetConnectionFactory">
<bean class="com.tibco.tibjms.TibjmsQueueConnectionFactory">
<constructor-arg value="URL" />
<constructor-arg value="CLIENT_ID" />
</bean>
</property>
</bean>
</property>
<property name="reconnectOnException" value="true" />
</bean>
By using the same client id in both factories(CLIENT_ID), I made sure that when I create connection from one factory, I can't create another connection from the other factory (throws InvalidClientIDException, so this is as expected). But the issue is, when I close the connection I got from first factory, and try to get connection from second factory, I'm still getting the exception.
Using the following Java code:
Connection connection1 = connectionFactory1.createConnection();
connection1.close();
Connection connection2 = connectionFactory2.createConnection();
What is the proper way of closing first connection, so that it allows me to create connection using the second factory?
Also, note that I am not using Queues here. I have to achieve this without queues.
Upvotes: 1
Views: 6204
Reputation: 174564
Use factory.resetConnection()
to actually close the single connection.
Upvotes: 2
Reputation: 13
I would not recommend the accepted answer. Let me address this and several points raised by other answers to this question.
A better approach would look something like this example (JavaEE6 JMS style) which sends an ObjectMessage to a Queue from within a stateless EJB:
@Stateless
public class SendEventsBean {
private static final Logger log = Logger.getLogger(SendEventsBean.class);
@Resource(mappedName = "jms/MyConnectionFactory")
private ConnectionFactory jmsConnectionFactory;
@Resource(mappedName = "jms/myApp/MyQueue"
private Queue queue;
public void sendEvent() {
Connection jmsConnection = null;
try {
connection = jmsConnectionFactory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer producer = session.createProducer(queue);
MyObj obj = new MyObj(1, "Foo");
ObjectMessage myObjMsg = session.createObjectMessage(obj);
producer.send(myObjMsg);
} catch (JMSException jmxEx) {
log.error("Couldn't send JMS message: ", jmsEx);
}finally{
if (jmsConnection != null) {
try {
jmsConnection.close();
}catch(JMSException ex) {
log.warn("Couldn't close JMSConnection: ", ex);
}
}
}
}
}
Upvotes: 1