public class CachingConnectionFactory extends AbstractConnectionFactory implements InitializingBean, com.rabbitmq.client.ShutdownListener
ConnectionFactory implementation that (when the cache mode is CachingConnectionFactory.CacheMode.CHANNEL (default)
returns the same Connection 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.
When the cache mode is CachingConnectionFactory.CacheMode.CONNECTION, a new (or cached) connection is used for each createConnection();
connections are cached according to the "connectionCacheSize" value.
Both connections and channels are cached in this mode.
CachingConnectionFactory.CacheMode.CONNECTION is not compatible with a Rabbit Admin that auto-declares queues etc.
NOTE: This ConnectionFactory requires explicit closing of all Channels obtained form its Connection(s).
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. Channel.close() returns the channel to the
cache, if there is room, or physically closes the channel otherwise.
| Modifier and Type | Class and Description |
|---|---|
static class |
CachingConnectionFactory.CacheMode |
DEFAULT_CLOSE_TIMEOUT, logger| Constructor and Description |
|---|
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 port on the hostname returned from
InetAddress.getLocalHost(), or "localhost" if getLocalHost() throws an exception.
|
CachingConnectionFactory(String hostname)
Create a new CachingConnectionFactory given a host name.
|
CachingConnectionFactory(String hostname,
int port)
Create a new CachingConnectionFactory given a host name
and port.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addConnectionListener(ConnectionListener listener) |
void |
afterPropertiesSet() |
Connection |
createConnection() |
void |
destroy()
Close the underlying shared connection.
|
CachingConnectionFactory.CacheMode |
getCacheMode() |
int |
getChannelCacheSize() |
int |
getConnectionCachesize() |
boolean |
isPublisherConfirms() |
boolean |
isPublisherReturns() |
protected void |
reset()
Reset the Channel cache and underlying shared Connection, to be reinitialized on next access.
|
void |
setCacheMode(CachingConnectionFactory.CacheMode cacheMode) |
void |
setChannelCacheSize(int sessionCacheSize) |
void |
setConnectionCacheSize(int connectionCacheSize) |
void |
setConnectionListeners(List<? extends ConnectionListener> listeners) |
void |
setPublisherConfirms(boolean publisherConfirms) |
void |
setPublisherReturns(boolean publisherReturns) |
void |
shutdownCompleted(com.rabbitmq.client.ShutdownSignalException cause) |
String |
toString() |
addChannelListener, clearConnectionListeners, createBareConnection, getChannelListener, getConnectionListener, getDefaultHostName, getHost, getPort, getVirtualHost, removeConnectionListener, setAddresses, setChannelListeners, setCloseTimeout, setExecutor, setHost, setPassword, setPort, setRequestedHeartBeat, setUsername, setVirtualHostpublic CachingConnectionFactory()
public CachingConnectionFactory(String hostname, int port)
hostname - the host name to connect toport - the port numberpublic CachingConnectionFactory(int port)
port - the port numberpublic CachingConnectionFactory(String hostname)
hostname - the host name to connect topublic CachingConnectionFactory(com.rabbitmq.client.ConnectionFactory rabbitConnectionFactory)
rabbitConnectionFactory - the target ConnectionFactorypublic void setChannelCacheSize(int sessionCacheSize)
public int getChannelCacheSize()
public CachingConnectionFactory.CacheMode getCacheMode()
public void setCacheMode(CachingConnectionFactory.CacheMode cacheMode)
public int getConnectionCachesize()
public void setConnectionCacheSize(int connectionCacheSize)
public boolean isPublisherConfirms()
public boolean isPublisherReturns()
public void setPublisherReturns(boolean publisherReturns)
public void setPublisherConfirms(boolean publisherConfirms)
public void afterPropertiesSet()
throws Exception
afterPropertiesSet in interface InitializingBeanExceptionpublic void setConnectionListeners(List<? extends ConnectionListener> listeners)
setConnectionListeners in class AbstractConnectionFactorypublic void addConnectionListener(ConnectionListener listener)
addConnectionListener in interface ConnectionFactoryaddConnectionListener in class AbstractConnectionFactorypublic void shutdownCompleted(com.rabbitmq.client.ShutdownSignalException cause)
shutdownCompleted in interface com.rabbitmq.client.ShutdownListenerpublic final Connection createConnection() throws AmqpException
createConnection in interface ConnectionFactoryAmqpExceptionpublic final void destroy()
As this bean implements DisposableBean, a bean factory will automatically invoke this on destruction of its cached singletons.
destroy in interface DisposableBeandestroy in class AbstractConnectionFactoryprotected void reset()