提交 53a244d4 编写于 作者: J Juergen Hoeller

JmsInvokerClientInterceptor/FactoryBean always uses createConnection/createSession when on JMS 1.1

上级 8213d0cb
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -48,6 +48,7 @@ import org.springframework.remoting.support.DefaultRemoteInvocationFactory; ...@@ -48,6 +48,7 @@ import org.springframework.remoting.support.DefaultRemoteInvocationFactory;
import org.springframework.remoting.support.RemoteInvocation; import org.springframework.remoting.support.RemoteInvocation;
import org.springframework.remoting.support.RemoteInvocationFactory; import org.springframework.remoting.support.RemoteInvocationFactory;
import org.springframework.remoting.support.RemoteInvocationResult; import org.springframework.remoting.support.RemoteInvocationResult;
import org.springframework.util.ClassUtils;
/** /**
* {@link org.aopalliance.intercept.MethodInterceptor} for accessing a * {@link org.aopalliance.intercept.MethodInterceptor} for accessing a
...@@ -74,6 +75,8 @@ import org.springframework.remoting.support.RemoteInvocationResult; ...@@ -74,6 +75,8 @@ import org.springframework.remoting.support.RemoteInvocationResult;
*/ */
public class JmsInvokerClientInterceptor implements MethodInterceptor, InitializingBean { public class JmsInvokerClientInterceptor implements MethodInterceptor, InitializingBean {
private static final boolean jms11Available = ClassUtils.hasMethod(ConnectionFactory.class, "createConnection");
private ConnectionFactory connectionFactory; private ConnectionFactory connectionFactory;
private Object queue; private Object queue;
...@@ -193,7 +196,7 @@ public class JmsInvokerClientInterceptor implements MethodInterceptor, Initializ ...@@ -193,7 +196,7 @@ public class JmsInvokerClientInterceptor implements MethodInterceptor, Initializ
} }
RemoteInvocation invocation = createRemoteInvocation(methodInvocation); RemoteInvocation invocation = createRemoteInvocation(methodInvocation);
RemoteInvocationResult result = null; RemoteInvocationResult result;
try { try {
result = executeRequest(invocation); result = executeRequest(invocation);
} }
...@@ -255,39 +258,27 @@ public class JmsInvokerClientInterceptor implements MethodInterceptor, Initializ ...@@ -255,39 +258,27 @@ public class JmsInvokerClientInterceptor implements MethodInterceptor, Initializ
} }
/** /**
* Create a new JMS Connection for this JMS invoker, * 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>).
*/ */
protected Connection createConnection() throws JMSException { protected Connection createConnection() throws JMSException {
ConnectionFactory cf = getConnectionFactory(); ConnectionFactory cf = getConnectionFactory();
if (cf instanceof QueueConnectionFactory) { if (jms11Available) {
return ((QueueConnectionFactory) cf).createQueueConnection(); return cf.createConnection();
} }
else { else {
return cf.createConnection(); return ((QueueConnectionFactory) cf).createQueueConnection();
} }
} }
/** /**
* Create a new JMS Session for this JMS invoker, * 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>).
*/ */
protected Session createSession(Connection con) throws JMSException { protected Session createSession(Connection con) throws JMSException {
if (con instanceof QueueConnection) { if (jms11Available) {
return ((QueueConnection) con).createQueueSession(false, Session.AUTO_ACKNOWLEDGE); return con.createSession(false, Session.AUTO_ACKNOWLEDGE);
} }
else { 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 ...@@ -352,8 +343,17 @@ public class JmsInvokerClientInterceptor implements MethodInterceptor, Initializ
MessageProducer producer = null; MessageProducer producer = null;
MessageConsumer consumer = null; MessageConsumer consumer = null;
try { 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... // 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; QueueSession queueSession = (QueueSession) session;
responseQueue = queueSession.createTemporaryQueue(); responseQueue = queueSession.createTemporaryQueue();
QueueSender sender = queueSession.createSender(queue); QueueSender sender = queueSession.createSender(queue);
...@@ -362,14 +362,6 @@ public class JmsInvokerClientInterceptor implements MethodInterceptor, Initializ ...@@ -362,14 +362,6 @@ public class JmsInvokerClientInterceptor implements MethodInterceptor, Initializ
requestMessage.setJMSReplyTo(responseQueue); requestMessage.setJMSReplyTo(responseQueue);
sender.send(requestMessage); 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(); long timeout = getReceiveTimeout();
return (timeout > 0 ? consumer.receive(timeout) : consumer.receive()); return (timeout > 0 ? consumer.receive(timeout) : consumer.receive());
} }
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -19,7 +19,6 @@ package org.springframework.jms.remoting; ...@@ -19,7 +19,6 @@ package org.springframework.jms.remoting;
import java.io.Serializable; import java.io.Serializable;
import java.util.Arrays; import java.util.Arrays;
import java.util.Enumeration; import java.util.Enumeration;
import javax.jms.Destination; import javax.jms.Destination;
import javax.jms.JMSException; import javax.jms.JMSException;
import javax.jms.Message; import javax.jms.Message;
...@@ -70,10 +69,10 @@ public class JmsInvokerTests extends TestCase { ...@@ -70,10 +69,10 @@ public class JmsInvokerTests extends TestCase {
queueControl = MockControl.createControl(Queue.class); queueControl = MockControl.createControl(Queue.class);
mockQueue = (Queue) queueControl.getMock(); mockQueue = (Queue) queueControl.getMock();
mockConnectionFactory.createQueueConnection(); mockConnectionFactory.createConnection();
connectionFactoryControl.setReturnValue(mockConnection, 8); connectionFactoryControl.setReturnValue(mockConnection, 8);
mockConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); mockConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
connectionControl.setReturnValue(mockSession, 8); connectionControl.setReturnValue(mockSession, 8);
mockConnection.start(); mockConnection.start();
...@@ -409,6 +408,6 @@ public class JmsInvokerTests extends TestCase { ...@@ -409,6 +408,6 @@ public class JmsInvokerTests extends TestCase {
public Message toMessage(Object object, Session session) throws JMSException, MessageConversionException { public Message toMessage(Object object, Session session) throws JMSException, MessageConversionException {
return new MockObjectMessage((Serializable) object); return new MockObjectMessage((Serializable) object);
} }
}; }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册