📄 singleconnectionfactory.java
字号:
/*
* Copyright 2002-2007 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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 java.util.ArrayList;
import java.util.List;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.ExceptionListener;
import javax.jms.JMSException;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;
/**
* A JMS ConnectionFactory adapter that returns the same Connection on all
* <code>createConnection</code> calls, and ignores calls to
* <code>Connection.close()</code>. According to the JMS Connection model,
* this is even thread-safe.
*
* <p>Useful for testing and standalone environments, to keep using the same
* Connection for multiple JmsTemplate calls, without having a pooling
* ConnectionFactory, also spanning any number of transactions.
*
* <p>You can either pass in a JMS Connection directly, or let this
* factory lazily create a Connection via a given target ConnectionFactory.
* In the latter case, this factory just works with JMS 1.1; use
* {@link SingleConnectionFactory102} for JMS 1.0.2.
*
* @author Mark Pollack
* @author Juergen Hoeller
* @since 1.1
* @see #createConnection()
* @see javax.jms.Connection#close()
* @see SingleConnectionFactory102
* @see org.springframework.jms.core.JmsTemplate
*/
public class SingleConnectionFactory
implements ConnectionFactory, QueueConnectionFactory, TopicConnectionFactory, ExceptionListener,
InitializingBean, DisposableBean {
protected final Log logger = LogFactory.getLog(getClass());
private ConnectionFactory targetConnectionFactory;
private String clientId;
private ExceptionListener exceptionListener;
private boolean reconnectOnException = false;
/** Wrapped Connection */
private Connection target;
/** Proxy Connection */
private Connection connection;
/** Synchronization monitor for the shared Connection */
private final Object connectionMonitor = new Object();
/**
* Create a new SingleConnectionFactory for bean-style usage.
* @see #setTargetConnectionFactory
*/
public SingleConnectionFactory() {
}
/**
* Create a new SingleConnectionFactory that always returns the
* given Connection. Works with both JMS 1.1 and 1.0.2.
* @param target the single Connection
*/
public SingleConnectionFactory(Connection target) {
Assert.notNull(target, "Target Connection must not be null");
this.target = target;
this.connection = getSharedConnectionProxy(target);
}
/**
* Create a new SingleConnectionFactory that always returns a single
* Connection that it will lazily create via the given target
* ConnectionFactory.
* @param targetConnectionFactory the target ConnectionFactory
*/
public SingleConnectionFactory(ConnectionFactory targetConnectionFactory) {
Assert.notNull(targetConnectionFactory, "Target ConnectionFactory must not be null");
this.targetConnectionFactory = targetConnectionFactory;
}
/**
* Set the target ConnectionFactory which will be used to lazily
* create a single Connection.
*/
public void setTargetConnectionFactory(ConnectionFactory targetConnectionFactory) {
this.targetConnectionFactory = targetConnectionFactory;
}
/**
* Return the target ConnectionFactory which will be used to lazily
* create a single Connection, if any.
*/
public ConnectionFactory getTargetConnectionFactory() {
return this.targetConnectionFactory;
}
/**
* Specify a JMS client ID for the single Connection created and exposed
* by this ConnectionFactory.
* <p>Note that client IDs need to be unique among all active Connections
* of the underlying JMS provider. Furthermore, a client ID can only be
* assigned if the original ConnectionFactory hasn't already assigned one.
* @see javax.jms.Connection#setClientID
* @see #setTargetConnectionFactory
*/
public void setClientId(String clientId) {
this.clientId = clientId;
}
/**
* Return a JMS client ID for the single Connection created and exposed
* by this ConnectionFactory, if any.
*/
protected String getClientId() {
return this.clientId;
}
/**
* Specify an JMS ExceptionListener implementation that should be
* registered with with the single Connection created by this factory.
* @see #setReconnectOnException
*/
public void setExceptionListener(ExceptionListener exceptionListener) {
this.exceptionListener = exceptionListener;
}
/**
* Return the JMS ExceptionListener implementation that should be registered
* with with the single Connection created by this factory, if any.
*/
protected ExceptionListener getExceptionListener() {
return this.exceptionListener;
}
/**
* Specify whether the single Connection should be reset (to be subsequently renewed)
* when a JMSException is reported by the underlying Connection.
* <p>Default is "false". Switch this to "true" to automatically trigger
* recovery based on your JMS provider's exception notifications.
* <p>Internally, this will lead to a special JMS ExceptionListener
* (this SingleConnectionFactory itself) being registered with the
* underlying Connection. This can also be combined with a
* user-specified ExceptionListener, if desired.
* @see #setExceptionListener
*/
public void setReconnectOnException(boolean reconnectOnException) {
this.reconnectOnException = reconnectOnException;
}
/**
* Return whether the single Connection should be renewed when
* a JMSException is reported by the underlying Connection.
*/
protected boolean isReconnectOnException() {
return this.reconnectOnException;
}
/**
* Make sure a Connection or ConnectionFactory has been set.
*/
public void afterPropertiesSet() {
if (this.connection == null && getTargetConnectionFactory() == null) {
throw new IllegalArgumentException("Connection or 'targetConnectionFactory' is required");
}
}
public Connection createConnection() throws JMSException {
synchronized (this.connectionMonitor) {
if (this.connection == null) {
initConnection();
}
return this.connection;
}
}
public Connection createConnection(String username, String password) throws JMSException {
throw new javax.jms.IllegalStateException(
"SingleConnectionFactory does not support custom username and password");
}
public QueueConnection createQueueConnection() throws JMSException {
Connection con = createConnection();
if (!(con instanceof QueueConnection)) {
throw new javax.jms.IllegalStateException(
"This SingleConnectionFactory does not hold a QueueConnection but rather: " + con);
}
return ((QueueConnection) con);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -