Spring AMQP

org.springframework.amqp.rabbit.connection
Class CachingConnectionFactory

java.lang.Object
  extended by org.springframework.amqp.rabbit.connection.AbstractConnectionFactory
      extended by org.springframework.amqp.rabbit.connection.CachingConnectionFactory
All Implemented Interfaces:
ConnectionFactory, DisposableBean

public class CachingConnectionFactory
extends AbstractConnectionFactory

A ConnectionFactory implementation that returns the same Connections from all createConnection() calls, and ignores calls to Connection.close() and caches Channel.

By default, only one Channel will be cached, with further requested Channels being created and disposed on demand. Consider raising the "channelCacheSize" value in case of a high-concurrency environment.

NOTE: This ConnectionFactory requires explicit closing of all Channels obtained form its shared Connection. This is the usual recommendation for native Rabbit access code anyway. However, with this ConnectionFactory, its use is mandatory in order to actually allow for Channel reuse.

Author:
Mark Pollack, Mark Fisher, Dave Syer

Field Summary
 
Fields inherited from class org.springframework.amqp.rabbit.connection.AbstractConnectionFactory
logger
 
Constructor Summary
CachingConnectionFactory()
          Create a new CachingConnectionFactory initializing the hostname to be the value returned from InetAddress.getLocalHost(), or "localhost" if getLocalHost() throws an exception.
CachingConnectionFactory(com.rabbitmq.client.ConnectionFactory rabbitConnectionFactory)
          Create a new CachingConnectionFactory for the given target ConnectionFactory.
CachingConnectionFactory(int port)
          Create a new CachingConnectionFactory given a host name.
CachingConnectionFactory(String hostname)
          Create a new CachingConnectionFactory given a host name.
CachingConnectionFactory(String hostname, int port)
          Create a new CachingConnectionFactory given a host name.
 
Method Summary
 void addConnectionListener(ConnectionListener listener)
           
 Connection createConnection()
           
 void destroy()
          Close the underlying shared connection.
 int getChannelCacheSize()
           
protected  void reset()
          Reset the Channel cache and underlying shared Connection, to be reinitialized on next access.
 void setChannelCacheSize(int sessionCacheSize)
           
 void setConnectionListeners(List<? extends ConnectionListener> listeners)
           
 String toString()
           
 
Methods inherited from class org.springframework.amqp.rabbit.connection.AbstractConnectionFactory
addChannelListener, createBareConnection, getChannelListener, getConnectionListener, getDefaultHostName, getHost, getPort, getVirtualHost, setChannelListeners, setHost, setPassword, setPort, setUsername, setVirtualHost
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

CachingConnectionFactory

public CachingConnectionFactory()
Create a new CachingConnectionFactory initializing the hostname to be the value returned from InetAddress.getLocalHost(), or "localhost" if getLocalHost() throws an exception.


CachingConnectionFactory

public CachingConnectionFactory(String hostname,
                                int port)
Create a new CachingConnectionFactory given a host name.

Parameters:
hostname - the host name to connect to

CachingConnectionFactory

public CachingConnectionFactory(int port)
Create a new CachingConnectionFactory given a host name.

Parameters:
hostName - the host name to connect to

CachingConnectionFactory

public CachingConnectionFactory(String hostname)
Create a new CachingConnectionFactory given a host name.

Parameters:
hostname - the host name to connect to

CachingConnectionFactory

public CachingConnectionFactory(com.rabbitmq.client.ConnectionFactory rabbitConnectionFactory)
Create a new CachingConnectionFactory for the given target ConnectionFactory.

Parameters:
rabbitConnectionFactory - the target ConnectionFactory
Method Detail

setChannelCacheSize

public void setChannelCacheSize(int sessionCacheSize)

getChannelCacheSize

public int getChannelCacheSize()

setConnectionListeners

public void setConnectionListeners(List<? extends ConnectionListener> listeners)
Overrides:
setConnectionListeners in class AbstractConnectionFactory

addConnectionListener

public void addConnectionListener(ConnectionListener listener)
Specified by:
addConnectionListener in interface ConnectionFactory
Overrides:
addConnectionListener in class AbstractConnectionFactory

createConnection

public final Connection createConnection()
                                  throws AmqpException
Throws:
AmqpException

destroy

public final 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.

Specified by:
destroy in interface DisposableBean
Overrides:
destroy in class AbstractConnectionFactory

reset

protected void reset()
Reset the Channel cache and underlying shared Connection, to be reinitialized on next access.


toString

public String toString()
Overrides:
toString in class Object

Spring AMQP

Copyright © 2011. All Rights Reserved.