JmsInvokerClientInterceptor/FactoryBean always uses createConnection/createSession when on JMS 1.1
This commit is contained in:
parent
1adf82503b
commit
0f1affe931
|
@ -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 <code>javax.jms.QueueConnection</code>.
|
||||
* <p>The default implementation uses the
|
||||
* <code>javax.jms.QueueConnectionFactory</code> 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 <code>org.apache.activemq.pool.PooledConnectionFactory</code>).
|
||||
* 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 <code>javax.jms.QueueSession</code>.
|
||||
* <p>The default implementation uses the
|
||||
* <code>javax.jms.QueueConnection</code> 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 <code>org.apache.activemq.pool.PooledConnectionFactory</code>).
|
||||
* 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());
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue