Upgrade to JMS 2.0 and JCA 1.7

Issue: SPR-13793
This commit is contained in:
Juergen Hoeller
2016-07-05 22:18:19 +02:00
parent bc2c22d51e
commit 355c6f0715
23 changed files with 381 additions and 208 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 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.
@@ -16,10 +16,7 @@
package org.springframework.jms.connection;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import javax.jms.CompletionListener;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
@@ -29,9 +26,6 @@ import javax.jms.QueueSender;
import javax.jms.Topic;
import javax.jms.TopicPublisher;
import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
/**
* JMS MessageProducer decorator that adapts calls to a shared MessageProducer
* instance underneath, managing QoS settings locally within the decorator.
@@ -41,36 +35,6 @@ import org.springframework.util.ReflectionUtils;
*/
class CachedMessageProducer implements MessageProducer, QueueSender, TopicPublisher {
// Various JMS 2.0 MessageProducer methods, if available
private static final Method setDeliveryDelayMethod =
ClassUtils.getMethodIfAvailable(MessageProducer.class, "setDeliveryDelay", long.class);
private static final Method getDeliveryDelayMethod =
ClassUtils.getMethodIfAvailable(MessageProducer.class, "getDeliveryDelay");
private static Class<?> completionListenerClass;
private static Method sendWithCompletionListenerMethod;
private static Method sendWithDestinationAndCompletionListenerMethod;
static {
try {
completionListenerClass = ClassUtils.forName(
"javax.jms.CompletionListener", CachedMessageProducer.class.getClassLoader());
sendWithCompletionListenerMethod = MessageProducer.class.getMethod(
"send", Message.class, int.class, int.class, long.class, completionListenerClass);
sendWithDestinationAndCompletionListenerMethod = MessageProducer.class.getMethod(
"send", Destination.class, Message.class, int.class, int.class, long.class, completionListenerClass);
}
catch (Exception ex) {
// No JMS 2.0 API available
completionListenerClass = null;
}
}
private final MessageProducer target;
private Boolean originalDisableMessageID;
@@ -120,15 +84,15 @@ class CachedMessageProducer implements MessageProducer, QueueSender, TopicPublis
return this.target.getDisableMessageTimestamp();
}
public void setDeliveryDelay(long deliveryDelay) {
public void setDeliveryDelay(long deliveryDelay) throws JMSException {
if (this.originalDeliveryDelay == null) {
this.originalDeliveryDelay = (Long) ReflectionUtils.invokeMethod(getDeliveryDelayMethod, this.target);
this.originalDeliveryDelay = this.target.getDeliveryDelay();
}
ReflectionUtils.invokeMethod(setDeliveryDelayMethod, this.target, deliveryDelay);
this.target.setDeliveryDelay(deliveryDelay);
}
public long getDeliveryDelay() {
return (Long) ReflectionUtils.invokeMethod(getDeliveryDelayMethod, this.target);
public long getDeliveryDelay() throws JMSException {
return this.target.getDeliveryDelay();
}
@Override
@@ -196,6 +160,31 @@ class CachedMessageProducer implements MessageProducer, QueueSender, TopicPublis
this.target.send(destination, message, deliveryMode, priority, timeToLive);
}
@Override
public void send(Message message, CompletionListener completionListener) throws JMSException {
this.target.send(message, this.deliveryMode, this.priority, this.timeToLive, completionListener);
}
@Override
public void send(Message message, int deliveryMode, int priority, long timeToLive,
CompletionListener completionListener) throws JMSException {
this.target.send(message, deliveryMode, priority, timeToLive, completionListener);
}
@Override
public void send(Destination destination, Message message, CompletionListener completionListener) throws JMSException {
this.target.send(destination, message, this.deliveryMode, this.priority, this.timeToLive, completionListener);
}
@Override
public void send(Destination destination, Message message, int deliveryMode, int priority,
long timeToLive, CompletionListener completionListener) throws JMSException {
this.target.send(destination, message, deliveryMode, priority, timeToLive, completionListener);
}
@Override
public void send(Queue queue, Message message) throws JMSException {
this.target.send(queue, message, this.deliveryMode, this.priority, this.timeToLive);
@@ -238,7 +227,7 @@ class CachedMessageProducer implements MessageProducer, QueueSender, TopicPublis
this.originalDisableMessageTimestamp = null;
}
if (this.originalDeliveryDelay != null) {
ReflectionUtils.invokeMethod(setDeliveryDelayMethod, this.target, this.originalDeliveryDelay);
this.target.setDeliveryDelay(this.originalDeliveryDelay);
this.originalDeliveryDelay = null;
}
}
@@ -248,54 +237,4 @@ class CachedMessageProducer implements MessageProducer, QueueSender, TopicPublis
return "Cached JMS MessageProducer: " + this.target;
}
/**
* Build a dynamic proxy that reflectively adapts to JMS 2.0 API methods, if necessary.
* Otherwise simply return this CachedMessageProducer instance itself.
*/
public MessageProducer getProxyIfNecessary() {
if (completionListenerClass != null) {
return (MessageProducer) Proxy.newProxyInstance(CachedMessageProducer.class.getClassLoader(),
new Class<?>[] {MessageProducer.class, QueueSender.class, TopicPublisher.class},
new Jms2MessageProducerInvocationHandler());
}
else {
return this;
}
}
/**
* Reflective InvocationHandler which adapts to JMS 2.0 API methods that we
* cannot statically compile against while preserving JMS 1.1 compatibility
* (due to the new {@code javax.jms.CompletionListener} type in the signatures).
*/
private class Jms2MessageProducerInvocationHandler implements InvocationHandler {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
try {
if (method.getName().equals("send") && args != null &&
completionListenerClass == method.getParameterTypes()[args.length - 1]) {
switch (args.length) {
case 2: // send(message, completionListener)
return sendWithCompletionListenerMethod.invoke(
target, args[0], deliveryMode, priority, timeToLive, args[1]);
case 3: // send(destination, message, completionListener)
return sendWithDestinationAndCompletionListenerMethod.invoke(
target, args[0], args[1], deliveryMode, priority, timeToLive, args[2]);
case 5: // send(message, deliveryMode, priority, timeToLive, completionListener)
return sendWithCompletionListenerMethod.invoke(target, args);
case 6: // send(destination, message, deliveryMode, priority, timeToLive, completionListener)
return sendWithDestinationAndCompletionListenerMethod.invoke(target, args);
}
}
return method.invoke(CachedMessageProducer.this, args);
}
catch (InvocationTargetException ex) {
throw ex.getTargetException();
}
}
}
}

View File

@@ -40,9 +40,7 @@ import javax.jms.Topic;
import javax.jms.TopicSession;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.ReflectionUtils;
/**
* {@link SingleConnectionFactory} subclass that adds {@link javax.jms.Session}
@@ -78,15 +76,6 @@ import org.springframework.util.ReflectionUtils;
*/
public class CachingConnectionFactory extends SingleConnectionFactory {
/** The JMS 2.0 Session.createSharedConsumer method, if available */
private static final Method createSharedConsumerMethod = ClassUtils.getMethodIfAvailable(
Session.class, "createSharedConsumer", Topic.class, String.class, String.class);
/** The JMS 2.0 Session.createSharedDurableConsumer method, if available */
private static final Method createSharedDurableConsumerMethod = ClassUtils.getMethodIfAvailable(
Session.class, "createSharedDurableConsumer", Topic.class, String.class, String.class);
private int sessionCacheSize = 1;
private boolean cacheProducers = true;
@@ -405,7 +394,7 @@ public class CachingConnectionFactory extends SingleConnectionFactory {
}
this.cachedProducers.put(cacheKey, producer);
}
return new CachedMessageProducer(producer).getProxyIfNecessary();
return new CachedMessageProducer(producer);
}
private MessageConsumer getCachedConsumer(
@@ -421,21 +410,9 @@ public class CachingConnectionFactory extends SingleConnectionFactory {
else {
if (dest instanceof Topic) {
if (noLocal == null) {
// createSharedConsumer((Topic) dest, subscription, selector);
// createSharedDurableConsumer((Topic) dest, subscription, selector);
Method method = (durable ? createSharedDurableConsumerMethod : createSharedConsumerMethod);
try {
consumer = (MessageConsumer) method.invoke(this.target, dest, subscription, selector);
}
catch (InvocationTargetException ex) {
if (ex.getTargetException() instanceof JMSException) {
throw (JMSException) ex.getTargetException();
}
ReflectionUtils.handleInvocationTargetException(ex);
}
catch (IllegalAccessException ex) {
throw new IllegalStateException("Could not access JMS 2.0 API method: " + ex.getMessage());
}
consumer = (durable ?
this.target.createSharedDurableConsumer((Topic) dest, subscription, selector) :
this.target.createSharedConsumer((Topic) dest, subscription, selector));
}
else {
consumer = (durable ?

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2016 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.
@@ -18,6 +18,7 @@ package org.springframework.jms.connection;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSContext;
import javax.jms.JMSException;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
@@ -95,22 +96,22 @@ public class DelegatingConnectionFactory
@Override
public Connection createConnection() throws JMSException {
return getTargetConnectionFactory().createConnection();
return obtainTargetConnectionFactory().createConnection();
}
@Override
public Connection createConnection(String username, String password) throws JMSException {
return getTargetConnectionFactory().createConnection(username, password);
return obtainTargetConnectionFactory().createConnection(username, password);
}
@Override
public QueueConnection createQueueConnection() throws JMSException {
ConnectionFactory cf = getTargetConnectionFactory();
if (cf instanceof QueueConnectionFactory) {
return ((QueueConnectionFactory) cf).createQueueConnection();
ConnectionFactory target = obtainTargetConnectionFactory();
if (target instanceof QueueConnectionFactory) {
return ((QueueConnectionFactory) target).createQueueConnection();
}
else {
Connection con = cf.createConnection();
Connection con = target.createConnection();
if (!(con instanceof QueueConnection)) {
throw new javax.jms.IllegalStateException("'targetConnectionFactory' is not a QueueConnectionFactory");
}
@@ -120,12 +121,12 @@ public class DelegatingConnectionFactory
@Override
public QueueConnection createQueueConnection(String username, String password) throws JMSException {
ConnectionFactory cf = getTargetConnectionFactory();
if (cf instanceof QueueConnectionFactory) {
return ((QueueConnectionFactory) cf).createQueueConnection(username, password);
ConnectionFactory target = obtainTargetConnectionFactory();
if (target instanceof QueueConnectionFactory) {
return ((QueueConnectionFactory) target).createQueueConnection(username, password);
}
else {
Connection con = cf.createConnection(username, password);
Connection con = target.createConnection(username, password);
if (!(con instanceof QueueConnection)) {
throw new javax.jms.IllegalStateException("'targetConnectionFactory' is not a QueueConnectionFactory");
}
@@ -135,12 +136,12 @@ public class DelegatingConnectionFactory
@Override
public TopicConnection createTopicConnection() throws JMSException {
ConnectionFactory cf = getTargetConnectionFactory();
if (cf instanceof TopicConnectionFactory) {
return ((TopicConnectionFactory) cf).createTopicConnection();
ConnectionFactory target = obtainTargetConnectionFactory();
if (target instanceof TopicConnectionFactory) {
return ((TopicConnectionFactory) target).createTopicConnection();
}
else {
Connection con = cf.createConnection();
Connection con = target.createConnection();
if (!(con instanceof TopicConnection)) {
throw new javax.jms.IllegalStateException("'targetConnectionFactory' is not a TopicConnectionFactory");
}
@@ -150,12 +151,12 @@ public class DelegatingConnectionFactory
@Override
public TopicConnection createTopicConnection(String username, String password) throws JMSException {
ConnectionFactory cf = getTargetConnectionFactory();
if (cf instanceof TopicConnectionFactory) {
return ((TopicConnectionFactory) cf).createTopicConnection(username, password);
ConnectionFactory target = obtainTargetConnectionFactory();
if (target instanceof TopicConnectionFactory) {
return ((TopicConnectionFactory) target).createTopicConnection(username, password);
}
else {
Connection con = cf.createConnection(username, password);
Connection con = target.createConnection(username, password);
if (!(con instanceof TopicConnection)) {
throw new javax.jms.IllegalStateException("'targetConnectionFactory' is not a TopicConnectionFactory");
}
@@ -163,6 +164,32 @@ public class DelegatingConnectionFactory
}
}
@Override
public JMSContext createContext() {
return obtainTargetConnectionFactory().createContext();
}
@Override
public JMSContext createContext(String userName, String password) {
return obtainTargetConnectionFactory().createContext(userName, password);
}
@Override
public JMSContext createContext(String userName, String password, int sessionMode) {
return obtainTargetConnectionFactory().createContext(userName, password, sessionMode);
}
@Override
public JMSContext createContext(int sessionMode) {
return obtainTargetConnectionFactory().createContext(sessionMode);
}
private ConnectionFactory obtainTargetConnectionFactory() {
ConnectionFactory target = getTargetConnectionFactory();
Assert.state(target != null, "'targetConnectionFactory' is required");
return target;
}
@Override
public boolean shouldStop(Connection con) {
return this.shouldStopConnections;

View File

@@ -27,6 +27,7 @@ import java.util.Set;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.ExceptionListener;
import javax.jms.JMSContext;
import javax.jms.JMSException;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
@@ -270,6 +271,32 @@ public class SingleConnectionFactory implements ConnectionFactory, QueueConnecti
"SingleConnectionFactory does not support custom username and password");
}
@Override
public JMSContext createContext() {
return obtainTargetConnectionFactory().createContext();
}
@Override
public JMSContext createContext(String userName, String password) {
return obtainTargetConnectionFactory().createContext(userName, password);
}
@Override
public JMSContext createContext(String userName, String password, int sessionMode) {
return obtainTargetConnectionFactory().createContext(userName, password, sessionMode);
}
@Override
public JMSContext createContext(int sessionMode) {
return obtainTargetConnectionFactory().createContext(sessionMode);
}
private ConnectionFactory obtainTargetConnectionFactory() {
ConnectionFactory target = getTargetConnectionFactory();
Assert.state(target != null, "'targetConnectionFactory' is required");
return target;
}
/**
* Obtain an initialized shared Connection.

View File

@@ -24,6 +24,7 @@ import java.util.ArrayList;
import java.util.List;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSContext;
import javax.jms.JMSException;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
@@ -101,7 +102,7 @@ public class TransactionAwareConnectionFactoryProxy
* Set the target ConnectionFactory that this ConnectionFactory should delegate to.
*/
public final void setTargetConnectionFactory(ConnectionFactory targetConnectionFactory) {
Assert.notNull(targetConnectionFactory, "targetConnectionFactory must not be nul");
Assert.notNull(targetConnectionFactory, "'targetConnectionFactory' must not be null");
this.targetConnectionFactory = targetConnectionFactory;
}
@@ -144,50 +145,76 @@ public class TransactionAwareConnectionFactoryProxy
@Override
public Connection createConnection(String username, String password) throws JMSException {
Connection targetConnection = this.targetConnectionFactory.createConnection(username, password);
Connection targetConnection = obtainTargetConnectionFactory().createConnection(username, password);
return getTransactionAwareConnectionProxy(targetConnection);
}
@Override
public QueueConnection createQueueConnection() throws JMSException {
if (!(this.targetConnectionFactory instanceof QueueConnectionFactory)) {
ConnectionFactory target = obtainTargetConnectionFactory();
if (!(target instanceof QueueConnectionFactory)) {
throw new javax.jms.IllegalStateException("'targetConnectionFactory' is no QueueConnectionFactory");
}
QueueConnection targetConnection =
((QueueConnectionFactory) this.targetConnectionFactory).createQueueConnection();
QueueConnection targetConnection = ((QueueConnectionFactory) target).createQueueConnection();
return (QueueConnection) getTransactionAwareConnectionProxy(targetConnection);
}
@Override
public QueueConnection createQueueConnection(String username, String password) throws JMSException {
if (!(this.targetConnectionFactory instanceof QueueConnectionFactory)) {
ConnectionFactory target = obtainTargetConnectionFactory();
if (!(target instanceof QueueConnectionFactory)) {
throw new javax.jms.IllegalStateException("'targetConnectionFactory' is no QueueConnectionFactory");
}
QueueConnection targetConnection =
((QueueConnectionFactory) this.targetConnectionFactory).createQueueConnection(username, password);
QueueConnection targetConnection = ((QueueConnectionFactory) target).createQueueConnection(username, password);
return (QueueConnection) getTransactionAwareConnectionProxy(targetConnection);
}
@Override
public TopicConnection createTopicConnection() throws JMSException {
if (!(this.targetConnectionFactory instanceof TopicConnectionFactory)) {
ConnectionFactory target = obtainTargetConnectionFactory();
if (!(target instanceof TopicConnectionFactory)) {
throw new javax.jms.IllegalStateException("'targetConnectionFactory' is no TopicConnectionFactory");
}
TopicConnection targetConnection =
((TopicConnectionFactory) this.targetConnectionFactory).createTopicConnection();
TopicConnection targetConnection = ((TopicConnectionFactory) target).createTopicConnection();
return (TopicConnection) getTransactionAwareConnectionProxy(targetConnection);
}
@Override
public TopicConnection createTopicConnection(String username, String password) throws JMSException {
if (!(this.targetConnectionFactory instanceof TopicConnectionFactory)) {
ConnectionFactory target = obtainTargetConnectionFactory();
if (!(target instanceof TopicConnectionFactory)) {
throw new javax.jms.IllegalStateException("'targetConnectionFactory' is no TopicConnectionFactory");
}
TopicConnection targetConnection =
((TopicConnectionFactory) this.targetConnectionFactory).createTopicConnection(username, password);
TopicConnection targetConnection = ((TopicConnectionFactory) target).createTopicConnection(username, password);
return (TopicConnection) getTransactionAwareConnectionProxy(targetConnection);
}
@Override
public JMSContext createContext() {
return obtainTargetConnectionFactory().createContext();
}
@Override
public JMSContext createContext(String userName, String password) {
return obtainTargetConnectionFactory().createContext(userName, password);
}
@Override
public JMSContext createContext(String userName, String password, int sessionMode) {
return obtainTargetConnectionFactory().createContext(userName, password, sessionMode);
}
@Override
public JMSContext createContext(int sessionMode) {
return obtainTargetConnectionFactory().createContext(sessionMode);
}
private ConnectionFactory obtainTargetConnectionFactory() {
ConnectionFactory target = getTargetConnectionFactory();
Assert.state(target != null, "'targetConnectionFactory' is required");
return target;
}
/**
* Wrap the given Connection with a proxy that delegates every method call to it

View File

@@ -18,6 +18,7 @@ package org.springframework.jms.connection;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSContext;
import javax.jms.JMSException;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
@@ -173,16 +174,15 @@ public class UserCredentialsConnectionFactoryAdapter
* @see javax.jms.ConnectionFactory#createConnection()
*/
protected Connection doCreateConnection(String username, String password) throws JMSException {
Assert.state(this.targetConnectionFactory != null, "'targetConnectionFactory' is required");
ConnectionFactory target = obtainTargetConnectionFactory();
if (StringUtils.hasLength(username)) {
return this.targetConnectionFactory.createConnection(username, password);
return target.createConnection(username, password);
}
else {
return this.targetConnectionFactory.createConnection();
return target.createConnection();
}
}
/**
* Determine whether there are currently thread-bound credentials,
* using them if available, falling back to the statically specified
@@ -220,11 +220,11 @@ public class UserCredentialsConnectionFactoryAdapter
* @see javax.jms.QueueConnectionFactory#createQueueConnection()
*/
protected QueueConnection doCreateQueueConnection(String username, String password) throws JMSException {
Assert.state(this.targetConnectionFactory != null, "'targetConnectionFactory' is required");
if (!(this.targetConnectionFactory instanceof QueueConnectionFactory)) {
ConnectionFactory target = obtainTargetConnectionFactory();
if (!(target instanceof QueueConnectionFactory)) {
throw new javax.jms.IllegalStateException("'targetConnectionFactory' is not a QueueConnectionFactory");
}
QueueConnectionFactory queueFactory = (QueueConnectionFactory) this.targetConnectionFactory;
QueueConnectionFactory queueFactory = (QueueConnectionFactory) target;
if (StringUtils.hasLength(username)) {
return queueFactory.createQueueConnection(username, password);
}
@@ -233,7 +233,6 @@ public class UserCredentialsConnectionFactoryAdapter
}
}
/**
* Determine whether there are currently thread-bound credentials,
* using them if available, falling back to the statically specified
@@ -271,11 +270,11 @@ public class UserCredentialsConnectionFactoryAdapter
* @see javax.jms.TopicConnectionFactory#createTopicConnection()
*/
protected TopicConnection doCreateTopicConnection(String username, String password) throws JMSException {
Assert.state(this.targetConnectionFactory != null, "'targetConnectionFactory' is required");
if (!(this.targetConnectionFactory instanceof TopicConnectionFactory)) {
ConnectionFactory target = obtainTargetConnectionFactory();
if (!(target instanceof TopicConnectionFactory)) {
throw new javax.jms.IllegalStateException("'targetConnectionFactory' is not a TopicConnectionFactory");
}
TopicConnectionFactory queueFactory = (TopicConnectionFactory) this.targetConnectionFactory;
TopicConnectionFactory queueFactory = (TopicConnectionFactory) target;
if (StringUtils.hasLength(username)) {
return queueFactory.createTopicConnection(username, password);
}
@@ -284,6 +283,31 @@ public class UserCredentialsConnectionFactoryAdapter
}
}
@Override
public JMSContext createContext() {
return obtainTargetConnectionFactory().createContext();
}
@Override
public JMSContext createContext(String userName, String password) {
return obtainTargetConnectionFactory().createContext(userName, password);
}
@Override
public JMSContext createContext(String userName, String password, int sessionMode) {
return obtainTargetConnectionFactory().createContext(userName, password, sessionMode);
}
@Override
public JMSContext createContext(int sessionMode) {
return obtainTargetConnectionFactory().createContext(sessionMode);
}
private ConnectionFactory obtainTargetConnectionFactory() {
Assert.state(this.targetConnectionFactory != null, "'targetConnectionFactory' is required");
return this.targetConnectionFactory;
}
/**
* Inner class used as ThreadLocal value.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2016 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.
@@ -24,7 +24,7 @@ import javax.resource.spi.UnavailableException;
import org.springframework.jca.endpoint.AbstractMessageEndpointFactory;
/**
* JMS-specific implementation of the JCA 1.5
* JMS-specific implementation of the JCA 1.7
* {@link javax.resource.spi.endpoint.MessageEndpointFactory} interface,
* providing transaction management capabilities for a JMS listener object
* (e.g. a {@link javax.jms.MessageListener} object).
@@ -61,7 +61,7 @@ public class JmsMessageEndpointFactory extends AbstractMessageEndpointFactory {
* Return the JMS MessageListener for this endpoint.
*/
protected MessageListener getMessageListener() {
return messageListener;
return this.messageListener;
}
/**