org.springframework.jms.connection
Class SingleConnectionFactory

java.lang.Object
  extended by org.springframework.jms.connection.SingleConnectionFactory
All Implemented Interfaces:
ConnectionFactory, QueueConnectionFactory, TopicConnectionFactory, DisposableBean, InitializingBean
Direct Known Subclasses:
SingleConnectionFactory102

public class SingleConnectionFactory
extends Object
implements ConnectionFactory, QueueConnectionFactory, TopicConnectionFactory, InitializingBean, DisposableBean

A JMS ConnectionFactory adapter that returns the same Connection on all createConnection calls, and ignores calls to Connection.close(). According to the JMS Connection model, this is even thread-safe.

Useful for testing and standalone environemtns, to keep using the same Connection for multiple JmsTemplate calls, without having a pooling ConnectionFactory, also spanning any number of transactions.

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 SingleConnectionFactory102 for JMS 1.0.2.

Since:
1.1
Author:
Mark Pollack, Juergen Hoeller
See Also:
createConnection(), Connection.close(), JmsTemplate, SingleConnectionFactory102

Field Summary
protected  org.apache.commons.logging.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 connection factory has been set.
 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 connection.
protected  Connection doCreateConnection()
          Create a JMS Connection via this template's ConnectionFactory.
protected  Connection getCloseSuppressingConnectionProxy(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.
protected  void init()
          Initialize the single 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

logger

protected final org.apache.commons.logging.Log logger
Constructor Detail

SingleConnectionFactory

public SingleConnectionFactory()
Create a new SingleConnectionFactory for bean-style usage.

See Also:
setTargetConnectionFactory(javax.jms.ConnectionFactory)

SingleConnectionFactory

public SingleConnectionFactory(Connection target)
Create a new SingleConnectionFactory that always returns the given Connection. Works with both JMS 1.1 and 1.0.2.

Parameters:
target - 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.

Parameters:
targetConnectionFactory - the target ConnectionFactory
Method Detail

setTargetConnectionFactory

public void setTargetConnectionFactory(ConnectionFactory targetConnectionFactory)
Set the target ConnectionFactory which will be used to lazily create a single Connection.


getTargetConnectionFactory

public ConnectionFactory getTargetConnectionFactory()
Return the target ConnectionFactory which will be used to lazily create a single Connection, if any.


afterPropertiesSet

public void afterPropertiesSet()
Make sure a connection or connection factory has been set.

Specified by:
afterPropertiesSet in interface InitializingBean

init

protected void init()
             throws JMSException
Initialize the single Connection.

Throws:
JMSException - if thrown by JMS API methods

doCreateConnection

protected Connection doCreateConnection()
                                 throws JMSException
Create a JMS Connection via this template's ConnectionFactory.

This implementation uses JMS 1.1 API.

Returns:
the new JMS Connection
Throws:
JMSException - if thrown by JMS API methods

destroy

public void destroy()
             throws JMSException
Close the underlying 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.

Specified by:
destroy in interface DisposableBean
Throws:
JMSException

createConnection

public Connection createConnection()
                            throws JMSException
Specified by:
createConnection in interface ConnectionFactory
Throws:
JMSException

createConnection

public Connection createConnection(String username,
                                   String password)
                            throws JMSException
Specified by:
createConnection in interface ConnectionFactory
Throws:
JMSException

createQueueConnection

public QueueConnection createQueueConnection()
                                      throws JMSException
Specified by:
createQueueConnection in interface QueueConnectionFactory
Throws:
JMSException

createQueueConnection

public QueueConnection createQueueConnection(String username,
                                             String password)
                                      throws JMSException
Specified by:
createQueueConnection in interface QueueConnectionFactory
Throws:
JMSException

createTopicConnection

public TopicConnection createTopicConnection()
                                      throws JMSException
Specified by:
createTopicConnection in interface TopicConnectionFactory
Throws:
JMSException

createTopicConnection

public TopicConnection createTopicConnection(String username,
                                             String password)
                                      throws JMSException
Specified by:
createTopicConnection in interface TopicConnectionFactory
Throws:
JMSException

getCloseSuppressingConnectionProxy

protected Connection getCloseSuppressingConnectionProxy(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.

Parameters:
target - the original Connection to wrap
Returns:
the wrapped Connection


Copyright (c) 2002-2006 The Spring Framework Project.