类 SingleConnectionFactory
- java.lang.Object
- org.springframework.jms.connection.SingleConnectionFactory
- 所有已实现的接口:
ConnectionFactory
,ExceptionListener
,QueueConnectionFactory
,TopicConnectionFactory
,DisposableBean
,InitializingBean
- 直接已知子类:
CachingConnectionFactory
public class SingleConnectionFactory extends Object implements ConnectionFactory, QueueConnectionFactory, TopicConnectionFactory, ExceptionListener, InitializingBean, DisposableBean
A JMS ConnectionFactory adapter that returns the same Connection from allcreateConnection()
calls, and ignores calls toConnection.close()
. According to the JMS Connection model, this is perfectly thread-safe (in contrast to e.g. JDBC). The shared Connection can be automatically recovered in case of an Exception.You can either pass in a specific JMS Connection directly or let this factory lazily create a Connection via a given target ConnectionFactory. This factory generally works with JMS 1.1 as well as the JMS 1.0.2 API.
Note that when using the JMS 1.0.2 API, this ConnectionFactory will switch into queue/topic mode according to the JMS API methods used at runtime:
createQueueConnection
andcreateTopicConnection
will lead to queue/topic mode, respectively; genericcreateConnection
calls will lead to a JMS 1.1 connection which is able to serve both modes.As of Spring Framework 5, this class supports JMS 2.0
JMSContext
calls and therefore requires the JMS 2.0 API to be present at runtime. It may nevertheless run against a JMS 1.1 driver (bound to the JMS 2.0 API) as long as no actual JMS 2.0 calls are triggered by the application's setup.Useful for testing and standalone environments in order to keep using the same Connection for multiple
JmsTemplate
calls, without having a pooling ConnectionFactory underneath. This may span any number of transactions, even concurrently executing transactions.Note that Spring's message listener containers support the use of a shared Connection within each listener container instance. Using SingleConnectionFactory in combination only really makes sense for sharing a single JMS Connection across multiple listener containers.
- 从以下版本开始:
- 1.1
- 作者:
- Juergen Hoeller, Mark Pollack
- 另请参阅:
JmsTemplate
,SimpleMessageListenerContainer
,DefaultMessageListenerContainer.setCacheLevel(int)
构造器概要
构造器 构造器 说明 SingleConnectionFactory()
Create a new SingleConnectionFactory for bean-style usage.SingleConnectionFactory(Connection targetConnection)
Create a new SingleConnectionFactory that always returns the given Connection.SingleConnectionFactory(ConnectionFactory targetConnectionFactory)
Create a new SingleConnectionFactory that always returns a single Connection that it will lazily create via the given target ConnectionFactory.
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 void
afterPropertiesSet()
Make sure a Connection or ConnectionFactory has been set.protected void
closeConnection(Connection con)
Close the given Connection.Connection
createConnection()
Connection
createConnection(String username, String password)
JMSContext
createContext()
JMSContext
createContext(int sessionMode)
JMSContext
createContext(String userName, String password)
JMSContext
createContext(String userName, String password, int sessionMode)
QueueConnection
createQueueConnection()
QueueConnection
createQueueConnection(String username, String password)
protected Session
createSession(Connection con, Integer mode)
Create a default Session for this ConnectionFactory, adapting to JMS 1.0.2 style queue/topic mode if necessary.TopicConnection
createTopicConnection()
TopicConnection
createTopicConnection(String username, String password)
void
destroy()
Close the underlying shared connection.protected Connection
doCreateConnection()
Create a JMS Connection via this template's ConnectionFactory.protected String
getClientId()
Return a JMS client ID for the single Connection created and exposed by this ConnectionFactory, if any.protected Connection
getConnection()
Obtain an initialized shared Connection.protected ExceptionListener
getExceptionListener()
Return the JMS ExceptionListener implementation that should be registered with the single Connection created by this factory, if any.protected Session
getSession(Connection con, Integer mode)
Template method for obtaining a (potentially cached) Session.protected Connection
getSharedConnectionProxy(Connection target)
Wrap the given Connection with a proxy that delegates every method call to it but suppresses close calls.ConnectionFactory
getTargetConnectionFactory()
Return the target ConnectionFactory which will be used to lazily create a single Connection, if any.void
initConnection()
Initialize the underlying shared Connection.protected boolean
isReconnectOnException()
Return whether the single Connection should be renewed when a JMSException is reported by the underlying Connection.void
onException(JMSException ex)
Exception listener callback that renews the underlying single Connection.protected void
prepareConnection(Connection con)
Prepare the given Connection before it is exposed.void
resetConnection()
Reset the underlying shared Connection, to be reinitialized on next access.void
setClientId(String clientId)
Specify a JMS client ID for the single Connection created and exposed by this ConnectionFactory.void
setExceptionListener(ExceptionListener exceptionListener)
Specify an JMS ExceptionListener implementation that should be registered with the single Connection created by this factory.void
setReconnectOnException(boolean reconnectOnException)
Specify whether the single Connection should be reset (to be subsequently renewed) when a JMSException is reported by the underlying Connection.void
setTargetConnectionFactory(ConnectionFactory targetConnectionFactory)
Set the target ConnectionFactory which will be used to lazily create a single Connection.
构造器详细资料
SingleConnectionFactory
public SingleConnectionFactory()
Create a new SingleConnectionFactory for bean-style usage.
SingleConnectionFactory
public SingleConnectionFactory(Connection targetConnection)
Create a new SingleConnectionFactory that always returns the given Connection.- 参数:
targetConnection
- the single Connection
SingleConnectionFactory
public SingleConnectionFactory(ConnectionFactory targetConnectionFactory)
Create a new SingleConnectionFactory that always returns a single Connection that it will lazily create via the given target ConnectionFactory.- 参数:
targetConnectionFactory
- the target ConnectionFactory
方法详细资料
setTargetConnectionFactory
public void setTargetConnectionFactory(@Nullable ConnectionFactory targetConnectionFactory)
Set the target ConnectionFactory which will be used to lazily create a single Connection.
getTargetConnectionFactory
@Nullable public ConnectionFactory getTargetConnectionFactory()
Return the target ConnectionFactory which will be used to lazily create a single Connection, if any.
setClientId
public void setClientId(@Nullable String clientId)
Specify a JMS client ID for the single Connection created and exposed by this ConnectionFactory.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.
getClientId
@Nullable protected String getClientId()
Return a JMS client ID for the single Connection created and exposed by this ConnectionFactory, if any.
setExceptionListener
public void setExceptionListener(@Nullable ExceptionListener exceptionListener)
Specify an JMS ExceptionListener implementation that should be registered with the single Connection created by this factory.
getExceptionListener
@Nullable protected ExceptionListener getExceptionListener()
Return the JMS ExceptionListener implementation that should be registered with the single Connection created by this factory, if any.
setReconnectOnException
public void setReconnectOnException(boolean reconnectOnException)
Specify whether the single Connection should be reset (to be subsequently renewed) when a JMSException is reported by the underlying Connection.Default is "false". Switch this to "true" to automatically trigger recovery based on your JMS provider's exception notifications.
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.
isReconnectOnException
protected boolean isReconnectOnException()
Return whether the single Connection should be renewed when a JMSException is reported by the underlying Connection.
afterPropertiesSet
public void afterPropertiesSet()
Make sure a Connection or ConnectionFactory has been set.- 指定者:
afterPropertiesSet
在接口中InitializingBean
createConnection
public Connection createConnection() throws JMSException
- 指定者:
createConnection
在接口中ConnectionFactory
- 抛出:
JMSException
createConnection
public Connection createConnection(String username, String password) throws JMSException
- 指定者:
createConnection
在接口中ConnectionFactory
- 抛出:
JMSException
createQueueConnection
public QueueConnection createQueueConnection() throws JMSException
createQueueConnection
public QueueConnection createQueueConnection(String username, String password) throws JMSException
createTopicConnection
public TopicConnection createTopicConnection() throws JMSException
createTopicConnection
public TopicConnection createTopicConnection(String username, String password) throws JMSException
createContext
public JMSContext createContext()
- 指定者:
createContext
在接口中ConnectionFactory
createContext
public JMSContext createContext(String userName, String password)
- 指定者:
createContext
在接口中ConnectionFactory
createContext
public JMSContext createContext(String userName, String password, int sessionMode)
- 指定者:
createContext
在接口中ConnectionFactory
createContext
public JMSContext createContext(int sessionMode)
- 指定者:
createContext
在接口中ConnectionFactory
getConnection
protected Connection getConnection() throws JMSException
Obtain an initialized shared Connection.- 返回:
- the Connection (never
null
) - 抛出:
JMSException
- if thrown by JMS API methods- 另请参阅:
initConnection()
initConnection
public void initConnection() throws JMSException
Initialize the underlying shared Connection.Closes and reinitializes the Connection if an underlying Connection is present already.
- 抛出:
JMSException
- if thrown by JMS API methods- 另请参阅:
prepareConnection(javax.jms.Connection)
onException
public void onException(JMSException ex)
Exception listener callback that renews the underlying single Connection.- 指定者:
onException
在接口中ExceptionListener
- 另请参阅:
resetConnection()
destroy
public void destroy()
Close the underlying shared connection. The provider of this ConnectionFactory needs to care for proper shutdown.As this bean implements DisposableBean, a bean factory will automatically invoke this on destruction of its cached singletons.
- 指定者:
destroy
在接口中DisposableBean
- 另请参阅:
resetConnection()
resetConnection
public void resetConnection()
Reset the underlying shared Connection, to be reinitialized on next access.
doCreateConnection
protected Connection doCreateConnection() throws JMSException
Create a JMS Connection via this template's ConnectionFactory.- 返回:
- the new JMS Connection
- 抛出:
JMSException
- if thrown by JMS API methods
prepareConnection
protected void prepareConnection(Connection con) throws JMSException
Prepare the given Connection before it is exposed.The default implementation applies ExceptionListener and client id. Can be overridden in subclasses.
- 参数:
con
- the Connection to prepare- 抛出:
JMSException
- if thrown by JMS API methods- 另请参阅:
setExceptionListener(javax.jms.ExceptionListener)
,setReconnectOnException(boolean)
getSession
@Nullable protected Session getSession(Connection con, Integer mode) throws JMSException
Template method for obtaining a (potentially cached) Session.The default implementation always returns
null
. Subclasses may override this for exposing specific Session handles, possibly delegating tocreateSession(javax.jms.Connection, java.lang.Integer)
for the creation of raw Session objects that will then get wrapped and returned from here.- 参数:
con
- the JMS Connection to operate onmode
- the Session acknowledgement mode (Session.TRANSACTED
or one of the common modes)- 返回:
- the Session to use, or
null
to indicate creation of a raw standard Session - 抛出:
JMSException
- if thrown by the JMS API
createSession
protected Session createSession(Connection con, Integer mode) throws JMSException
Create a default Session for this ConnectionFactory, adapting to JMS 1.0.2 style queue/topic mode if necessary.- 参数:
con
- the JMS Connection to operate onmode
- the Session acknowledgement mode (Session.TRANSACTED
or one of the common modes)- 返回:
- the newly created Session
- 抛出:
JMSException
- if thrown by the JMS API
closeConnection
protected void closeConnection(Connection con)
Close the given Connection.- 参数:
con
- the Connection to close
getSharedConnectionProxy
protected Connection getSharedConnectionProxy(Connection target)
Wrap the given Connection with a proxy that delegates every method call to it but suppresses close calls. This is useful for allowing application code to handle a special framework Connection just like an ordinary Connection from a JMS ConnectionFactory.- 参数:
target
- the original Connection to wrap- 返回:
- the wrapped Connection