org.springframework.jms.connection
Class SingleConnectionFactory

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

public class SingleConnectionFactory
extends java.lang.Object
implements javax.jms.ConnectionFactory, javax.jms.QueueConnectionFactory, javax.jms.TopicConnectionFactory, InitializingBean, DisposableBean

A JMS ConnectionFactory adapter that returns the same Connection on all createConnection calls, and ignores calls to close.

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:
SingleConnectionFactory102, JmsTemplate

Field Summary
protected  org.apache.commons.logging.Log logger
           
 
Constructor Summary
SingleConnectionFactory()
          Create a new SingleConnectionFactory for bean-style usage.
SingleConnectionFactory(javax.jms.Connection target)
          Create a new SingleConnectionFactory that always returns the given Connection.
SingleConnectionFactory(javax.jms.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.
 javax.jms.Connection createConnection()
           
 javax.jms.Connection createConnection(java.lang.String username, java.lang.String password)
           
 javax.jms.QueueConnection createQueueConnection()
           
 javax.jms.QueueConnection createQueueConnection(java.lang.String username, java.lang.String password)
           
 javax.jms.TopicConnection createTopicConnection()
           
 javax.jms.TopicConnection createTopicConnection(java.lang.String username, java.lang.String password)
           
 void destroy()
          Close the underlying connection.
protected  javax.jms.Connection doCreateConnection()
          Create a JMS Connection via this template's ConnectionFactory.
protected  javax.jms.Connection getCloseSuppressingConnectionProxy(javax.jms.Connection target)
          Wrap the given Connection with a proxy that delegates every method call to it but suppresses close calls.
 javax.jms.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(javax.jms.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(javax.jms.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(javax.jms.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(javax.jms.ConnectionFactory targetConnectionFactory)
Set the target ConnectionFactory which will be used to lazily create a single Connection.


getTargetConnectionFactory

public javax.jms.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 javax.jms.JMSException
Initialize the single Connection.

Throws:
javax.jms.JMSException - if thrown by JMS API methods

doCreateConnection

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

This implementation uses JMS 1.1 API.

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

destroy

public void destroy()
             throws javax.jms.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:
javax.jms.JMSException

createConnection

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

createConnection

public javax.jms.Connection createConnection(java.lang.String username,
                                             java.lang.String password)
                                      throws javax.jms.JMSException
Specified by:
createConnection in interface javax.jms.ConnectionFactory
Throws:
javax.jms.JMSException

createQueueConnection

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

createQueueConnection

public javax.jms.QueueConnection createQueueConnection(java.lang.String username,
                                                       java.lang.String password)
                                                throws javax.jms.JMSException
Specified by:
createQueueConnection in interface javax.jms.QueueConnectionFactory
Throws:
javax.jms.JMSException

createTopicConnection

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

createTopicConnection

public javax.jms.TopicConnection createTopicConnection(java.lang.String username,
                                                       java.lang.String password)
                                                throws javax.jms.JMSException
Specified by:
createTopicConnection in interface javax.jms.TopicConnectionFactory
Throws:
javax.jms.JMSException

getCloseSuppressingConnectionProxy

protected javax.jms.Connection getCloseSuppressingConnectionProxy(javax.jms.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) 2003-2004 The Spring Framework Project.