|
The Spring Framework | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.springframework.jms.connection.SingleConnectionFactory
public class SingleConnectionFactory
A JMS ConnectionFactory adapter that returns the same Connection
from all createConnection()
calls, and ignores calls to
Connection.close()
. According to the JMS Connection
model, this is perfectly thread-safe (in contrast to e.g. JDBC).
You can either pass in a specific 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
SingleConnectionFactory102
for JMS 1.0.2.
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.
JmsTemplate
,
SimpleMessageListenerContainer
,
DefaultMessageListenerContainer.setCacheLevel(int)
Field Summary | |
---|---|
protected Log |
logger
|
Constructor Summary | |
---|---|
SingleConnectionFactory()
Create a new SingleConnectionFactory for bean-style usage. |
|
SingleConnectionFactory(Connection target)
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. |
Method Summary | |
---|---|
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)
|
QueueConnection |
createQueueConnection()
|
QueueConnection |
createQueueConnection(String username,
String password)
|
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 ExceptionListener |
getExceptionListener()
Return the JMS ExceptionListener implementation that should be registered with 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 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. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected final Log logger
Constructor Detail |
---|
public SingleConnectionFactory()
setTargetConnectionFactory(javax.jms.ConnectionFactory)
public SingleConnectionFactory(Connection target)
target
- the single Connectionpublic SingleConnectionFactory(ConnectionFactory targetConnectionFactory)
targetConnectionFactory
- the target ConnectionFactoryMethod Detail |
---|
public void setTargetConnectionFactory(ConnectionFactory targetConnectionFactory)
public ConnectionFactory getTargetConnectionFactory()
public void setClientId(String clientId)
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.
Connection.setClientID(java.lang.String)
,
setTargetConnectionFactory(javax.jms.ConnectionFactory)
protected String getClientId()
public void setExceptionListener(ExceptionListener exceptionListener)
setReconnectOnException(boolean)
protected ExceptionListener getExceptionListener()
public void setReconnectOnException(boolean reconnectOnException)
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.
setExceptionListener(javax.jms.ExceptionListener)
protected boolean isReconnectOnException()
public void afterPropertiesSet()
afterPropertiesSet
in interface InitializingBean
public Connection createConnection() throws JMSException
createConnection
in interface ConnectionFactory
JMSException
public Connection createConnection(String username, String password) throws JMSException
createConnection
in interface ConnectionFactory
JMSException
public QueueConnection createQueueConnection() throws JMSException
createQueueConnection
in interface QueueConnectionFactory
JMSException
public QueueConnection createQueueConnection(String username, String password) throws JMSException
createQueueConnection
in interface QueueConnectionFactory
JMSException
public TopicConnection createTopicConnection() throws JMSException
createTopicConnection
in interface TopicConnectionFactory
JMSException
public TopicConnection createTopicConnection(String username, String password) throws JMSException
createTopicConnection
in interface TopicConnectionFactory
JMSException
public void initConnection() throws JMSException
Closes and reinitializes the Connection if an underlying Connection is present already.
JMSException
- if thrown by JMS API methodspublic void onException(JMSException ex)
onException
in interface ExceptionListener
public void destroy()
As this bean implements DisposableBean, a bean factory will automatically invoke this on destruction of its cached singletons.
destroy
in interface DisposableBean
public void resetConnection()
protected Connection doCreateConnection() throws JMSException
This implementation uses JMS 1.1 API.
JMSException
- if thrown by JMS API methodsprotected void prepareConnection(Connection con) throws JMSException
The default implementation applies ExceptionListener and client id. Can be overridden in subclasses.
con
- the Connection to prepare
JMSException
- if thrown by JMS API methodssetExceptionListener(javax.jms.ExceptionListener)
,
setReconnectOnException(boolean)
protected Session getSession(Connection con, Integer mode) throws JMSException
con
- the JMS Connection to operate onmode
- the Session acknowledgement mode
(Session.TRANSACTED
or one of the common modes)
null
to indicate
creation of a default Session
JMSException
- if thrown by the JMS APIprotected void closeConnection(Connection con)
con
- the Connection to closeprotected Connection getSharedConnectionProxy(Connection target)
target
- the original Connection to wrap
|
The Spring Framework | |||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |