From 0f1affe93148dc7b5b16bdd39a20b59ee9a232ad Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Fri, 9 Dec 2011 12:06:50 +0000 Subject: [PATCH] JmsInvokerClientInterceptor/FactoryBean always uses createConnection/createSession when on JMS 1.1 --- .../remoting/JmsInvokerClientInterceptor.java | 54 ++++++++----------- .../jms/remoting/JmsInvokerTests.java | 9 ++-- 2 files changed, 27 insertions(+), 36 deletions(-) diff --git a/org.springframework.jms/src/main/java/org/springframework/jms/remoting/JmsInvokerClientInterceptor.java b/org.springframework.jms/src/main/java/org/springframework/jms/remoting/JmsInvokerClientInterceptor.java index 6989ea28e92..7dd6ed748f0 100644 --- a/org.springframework.jms/src/main/java/org/springframework/jms/remoting/JmsInvokerClientInterceptor.java +++ b/org.springframework.jms/src/main/java/org/springframework/jms/remoting/JmsInvokerClientInterceptor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2007 the original author or authors. + * Copyright 2002-2011 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -48,6 +48,7 @@ import org.springframework.remoting.support.DefaultRemoteInvocationFactory; import org.springframework.remoting.support.RemoteInvocation; import org.springframework.remoting.support.RemoteInvocationFactory; import org.springframework.remoting.support.RemoteInvocationResult; +import org.springframework.util.ClassUtils; /** * {@link org.aopalliance.intercept.MethodInterceptor} for accessing a @@ -74,6 +75,8 @@ import org.springframework.remoting.support.RemoteInvocationResult; */ public class JmsInvokerClientInterceptor implements MethodInterceptor, InitializingBean { + private static final boolean jms11Available = ClassUtils.hasMethod(ConnectionFactory.class, "createConnection"); + private ConnectionFactory connectionFactory; private Object queue; @@ -193,7 +196,7 @@ public class JmsInvokerClientInterceptor implements MethodInterceptor, Initializ } RemoteInvocation invocation = createRemoteInvocation(methodInvocation); - RemoteInvocationResult result = null; + RemoteInvocationResult result; try { result = executeRequest(invocation); } @@ -255,39 +258,27 @@ public class JmsInvokerClientInterceptor implements MethodInterceptor, Initializ } /** - * Create a new JMS Connection for this JMS invoker, - * ideally a javax.jms.QueueConnection. - *

The default implementation uses the - * javax.jms.QueueConnectionFactory API if available, - * falling back to a standard JMS 1.1 ConnectionFactory otherwise. - * This is necessary for working with generic JMS 1.1 connection pools - * (such as ActiveMQ's org.apache.activemq.pool.PooledConnectionFactory). + * Create a new JMS Connection for this JMS invoker. */ protected Connection createConnection() throws JMSException { ConnectionFactory cf = getConnectionFactory(); - if (cf instanceof QueueConnectionFactory) { - return ((QueueConnectionFactory) cf).createQueueConnection(); + if (jms11Available) { + return cf.createConnection(); } else { - return cf.createConnection(); + return ((QueueConnectionFactory) cf).createQueueConnection(); } } /** - * Create a new JMS Session for this JMS invoker, - * ideally a javax.jms.QueueSession. - *

The default implementation uses the - * javax.jms.QueueConnection API if available, - * falling back to a standard JMS 1.1 Connection otherwise. - * This is necessary for working with generic JMS 1.1 connection pools - * (such as ActiveMQ's org.apache.activemq.pool.PooledConnectionFactory). + * Create a new JMS Session for this JMS invoker. */ protected Session createSession(Connection con) throws JMSException { - if (con instanceof QueueConnection) { - return ((QueueConnection) con).createQueueSession(false, Session.AUTO_ACKNOWLEDGE); + if (jms11Available) { + return con.createSession(false, Session.AUTO_ACKNOWLEDGE); } else { - return con.createSession(false, Session.AUTO_ACKNOWLEDGE); + return ((QueueConnection) con).createQueueSession(false, Session.AUTO_ACKNOWLEDGE); } } @@ -352,8 +343,17 @@ public class JmsInvokerClientInterceptor implements MethodInterceptor, Initializ MessageProducer producer = null; MessageConsumer consumer = null; try { - if (session instanceof QueueSession) { + if (jms11Available) { + // Standard JMS 1.1 API usage... + responseQueue = session.createTemporaryQueue(); + producer = session.createProducer(queue); + consumer = session.createConsumer(responseQueue); + requestMessage.setJMSReplyTo(responseQueue); + producer.send(requestMessage); + } + else { // Perform all calls on QueueSession reference for JMS 1.0.2 compatibility... + // DEPRECATED but kept around with the deprecated JmsTemplate102 etc classes for the time being. QueueSession queueSession = (QueueSession) session; responseQueue = queueSession.createTemporaryQueue(); QueueSender sender = queueSession.createSender(queue); @@ -362,14 +362,6 @@ public class JmsInvokerClientInterceptor implements MethodInterceptor, Initializ requestMessage.setJMSReplyTo(responseQueue); sender.send(requestMessage); } - else { - // Standard JMS 1.1 API usage... - responseQueue = session.createTemporaryQueue(); - producer = session.createProducer(queue); - consumer = session.createConsumer(responseQueue); - requestMessage.setJMSReplyTo(responseQueue); - producer.send(requestMessage); - } long timeout = getReceiveTimeout(); return (timeout > 0 ? consumer.receive(timeout) : consumer.receive()); } diff --git a/org.springframework.jms/src/test/java/org/springframework/jms/remoting/JmsInvokerTests.java b/org.springframework.jms/src/test/java/org/springframework/jms/remoting/JmsInvokerTests.java index 8c307cd603e..97c33f03077 100644 --- a/org.springframework.jms/src/test/java/org/springframework/jms/remoting/JmsInvokerTests.java +++ b/org.springframework.jms/src/test/java/org/springframework/jms/remoting/JmsInvokerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2007 the original author or authors. + * Copyright 2002-2011 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,7 +19,6 @@ package org.springframework.jms.remoting; import java.io.Serializable; import java.util.Arrays; import java.util.Enumeration; - import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.Message; @@ -70,10 +69,10 @@ public class JmsInvokerTests extends TestCase { queueControl = MockControl.createControl(Queue.class); mockQueue = (Queue) queueControl.getMock(); - mockConnectionFactory.createQueueConnection(); + mockConnectionFactory.createConnection(); connectionFactoryControl.setReturnValue(mockConnection, 8); - mockConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); + mockConnection.createSession(false, Session.AUTO_ACKNOWLEDGE); connectionControl.setReturnValue(mockSession, 8); mockConnection.start(); @@ -409,6 +408,6 @@ public class JmsInvokerTests extends TestCase { public Message toMessage(Object object, Session session) throws JMSException, MessageConversionException { return new MockObjectMessage((Serializable) object); } - }; + } }