Skip to content

Commit 53a244d

Browse files
committed
JmsInvokerClientInterceptor/FactoryBean always uses createConnection/createSession when on JMS 1.1
1 parent 8213d0c commit 53a244d

File tree

2 files changed

+27
-36
lines changed

2 files changed

+27
-36
lines changed

org.springframework.jms/src/main/java/org/springframework/jms/remoting/JmsInvokerClientInterceptor.java

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2007 the original author or authors.
2+
* Copyright 2002-2011 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -48,6 +48,7 @@
4848
import org.springframework.remoting.support.RemoteInvocation;
4949
import org.springframework.remoting.support.RemoteInvocationFactory;
5050
import org.springframework.remoting.support.RemoteInvocationResult;
51+
import org.springframework.util.ClassUtils;
5152

5253
/**
5354
* {@link org.aopalliance.intercept.MethodInterceptor} for accessing a
@@ -74,6 +75,8 @@
7475
*/
7576
public class JmsInvokerClientInterceptor implements MethodInterceptor, InitializingBean {
7677

78+
private static final boolean jms11Available = ClassUtils.hasMethod(ConnectionFactory.class, "createConnection");
79+
7780
private ConnectionFactory connectionFactory;
7881

7982
private Object queue;
@@ -193,7 +196,7 @@ public Object invoke(MethodInvocation methodInvocation) throws Throwable {
193196
}
194197

195198
RemoteInvocation invocation = createRemoteInvocation(methodInvocation);
196-
RemoteInvocationResult result = null;
199+
RemoteInvocationResult result;
197200
try {
198201
result = executeRequest(invocation);
199202
}
@@ -255,39 +258,27 @@ protected RemoteInvocationResult executeRequest(RemoteInvocation invocation) thr
255258
}
256259

257260
/**
258-
* Create a new JMS Connection for this JMS invoker,
259-
* ideally a <code>javax.jms.QueueConnection</code>.
260-
* <p>The default implementation uses the
261-
* <code>javax.jms.QueueConnectionFactory</code> API if available,
262-
* falling back to a standard JMS 1.1 ConnectionFactory otherwise.
263-
* This is necessary for working with generic JMS 1.1 connection pools
264-
* (such as ActiveMQ's <code>org.apache.activemq.pool.PooledConnectionFactory</code>).
261+
* Create a new JMS Connection for this JMS invoker.
265262
*/
266263
protected Connection createConnection() throws JMSException {
267264
ConnectionFactory cf = getConnectionFactory();
268-
if (cf instanceof QueueConnectionFactory) {
269-
return ((QueueConnectionFactory) cf).createQueueConnection();
265+
if (jms11Available) {
266+
return cf.createConnection();
270267
}
271268
else {
272-
return cf.createConnection();
269+
return ((QueueConnectionFactory) cf).createQueueConnection();
273270
}
274271
}
275272

276273
/**
277-
* Create a new JMS Session for this JMS invoker,
278-
* ideally a <code>javax.jms.QueueSession</code>.
279-
* <p>The default implementation uses the
280-
* <code>javax.jms.QueueConnection</code> API if available,
281-
* falling back to a standard JMS 1.1 Connection otherwise.
282-
* This is necessary for working with generic JMS 1.1 connection pools
283-
* (such as ActiveMQ's <code>org.apache.activemq.pool.PooledConnectionFactory</code>).
274+
* Create a new JMS Session for this JMS invoker.
284275
*/
285276
protected Session createSession(Connection con) throws JMSException {
286-
if (con instanceof QueueConnection) {
287-
return ((QueueConnection) con).createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
277+
if (jms11Available) {
278+
return con.createSession(false, Session.AUTO_ACKNOWLEDGE);
288279
}
289280
else {
290-
return con.createSession(false, Session.AUTO_ACKNOWLEDGE);
281+
return ((QueueConnection) con).createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
291282
}
292283
}
293284

@@ -352,8 +343,17 @@ protected Message doExecuteRequest(Session session, Queue queue, Message request
352343
MessageProducer producer = null;
353344
MessageConsumer consumer = null;
354345
try {
355-
if (session instanceof QueueSession) {
346+
if (jms11Available) {
347+
// Standard JMS 1.1 API usage...
348+
responseQueue = session.createTemporaryQueue();
349+
producer = session.createProducer(queue);
350+
consumer = session.createConsumer(responseQueue);
351+
requestMessage.setJMSReplyTo(responseQueue);
352+
producer.send(requestMessage);
353+
}
354+
else {
356355
// Perform all calls on QueueSession reference for JMS 1.0.2 compatibility...
356+
// DEPRECATED but kept around with the deprecated JmsTemplate102 etc classes for the time being.
357357
QueueSession queueSession = (QueueSession) session;
358358
responseQueue = queueSession.createTemporaryQueue();
359359
QueueSender sender = queueSession.createSender(queue);
@@ -362,14 +362,6 @@ protected Message doExecuteRequest(Session session, Queue queue, Message request
362362
requestMessage.setJMSReplyTo(responseQueue);
363363
sender.send(requestMessage);
364364
}
365-
else {
366-
// Standard JMS 1.1 API usage...
367-
responseQueue = session.createTemporaryQueue();
368-
producer = session.createProducer(queue);
369-
consumer = session.createConsumer(responseQueue);
370-
requestMessage.setJMSReplyTo(responseQueue);
371-
producer.send(requestMessage);
372-
}
373365
long timeout = getReceiveTimeout();
374366
return (timeout > 0 ? consumer.receive(timeout) : consumer.receive());
375367
}

org.springframework.jms/src/test/java/org/springframework/jms/remoting/JmsInvokerTests.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2007 the original author or authors.
2+
* Copyright 2002-2011 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,7 +19,6 @@
1919
import java.io.Serializable;
2020
import java.util.Arrays;
2121
import java.util.Enumeration;
22-
2322
import javax.jms.Destination;
2423
import javax.jms.JMSException;
2524
import javax.jms.Message;
@@ -70,10 +69,10 @@ protected void setUp() throws Exception {
7069
queueControl = MockControl.createControl(Queue.class);
7170
mockQueue = (Queue) queueControl.getMock();
7271

73-
mockConnectionFactory.createQueueConnection();
72+
mockConnectionFactory.createConnection();
7473
connectionFactoryControl.setReturnValue(mockConnection, 8);
7574

76-
mockConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
75+
mockConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
7776
connectionControl.setReturnValue(mockSession, 8);
7877

7978
mockConnection.start();
@@ -409,6 +408,6 @@ private static class MockSimpleMessageConverter extends SimpleMessageConverter {
409408
public Message toMessage(Object object, Session session) throws JMSException, MessageConversionException {
410409
return new MockObjectMessage((Serializable) object);
411410
}
412-
};
411+
}
413412

414413
}

0 commit comments

Comments
 (0)