public class CachingConnectionFactory extends AbstractConnectionFactory implements org.springframework.beans.factory.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 request. In this case,
no channels are cached, just connections. The intended use case is a dedicated connection for long-lived
channels, such as those used in listener container threads. In those cases, the channel must be closed
anyway in order to re-queue any un-acked messages.
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 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.
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, setVirtualHost
public 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 org.springframework.beans.factory.InitializingBean
Exception
public void setConnectionListeners(List<? extends ConnectionListener> listeners)
setConnectionListeners
in class AbstractConnectionFactory
public void addConnectionListener(ConnectionListener listener)
addConnectionListener
in interface ConnectionFactory
addConnectionListener
in class AbstractConnectionFactory
public void shutdownCompleted(com.rabbitmq.client.ShutdownSignalException cause)
shutdownCompleted
in interface com.rabbitmq.client.ShutdownListener
public final Connection createConnection() throws AmqpException
createConnection
in interface ConnectionFactory
AmqpException
public final void destroy()
As this bean implements DisposableBean, a bean factory will automatically invoke this on destruction of its cached singletons.
destroy
in interface org.springframework.beans.factory.DisposableBean
destroy
in class AbstractConnectionFactory
protected void reset()