@ManagedResource 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 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
The cache mode.
|
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(java.lang.String hostname)
Create a new CachingConnectionFactory given a host name.
|
CachingConnectionFactory(java.lang.String hostname,
int port)
Create a new CachingConnectionFactory given a host name
and port.
|
CachingConnectionFactory(java.net.URI uri)
Create a new CachingConnectionFactory given a
URI. |
| Modifier and Type | Method and Description |
|---|---|
void |
addConnectionListener(ConnectionListener listener) |
void |
afterPropertiesSet() |
protected void |
closeAndClear(java.util.Collection<ChannelProxy> theChannels) |
protected void |
closeChannels(java.util.Collection<ChannelProxy> theChannels) |
Connection |
createConnection() |
void |
destroy()
Close the underlying shared connection.
|
CachingConnectionFactory.CacheMode |
getCacheMode() |
java.util.Properties |
getCacheProperties() |
int |
getChannelCacheSize() |
int |
getConnectionCacheSize() |
protected java.util.concurrent.ExecutorService |
getDeferredCloseExecutor()
Determine the executor service used to close connections.
|
java.util.Properties |
getPublisherConnectionFactoryCacheProperties()
Return the cache properties from the underlying publisher sub-factory.
|
boolean |
isPublisherConfirms()
Return true if publisher confirms are enabled.
|
boolean |
isPublisherReturns()
Return true if publisher returns are enabled.
|
boolean |
isSimplePublisherConfirms()
Return true if simple publisher confirms are enabled.
|
protected void |
reset(java.util.List<ChannelProxy> channels,
java.util.List<ChannelProxy> txChannels,
java.util.Map<com.rabbitmq.client.Channel,ChannelProxy> channelsAwaitingAcks) |
void |
resetConnection()
Close the connection(s).
|
void |
setCacheMode(CachingConnectionFactory.CacheMode cacheMode) |
void |
setChannelCacheSize(int sessionCacheSize)
The number of channels to maintain in the cache.
|
void |
setChannelCheckoutTimeout(long channelCheckoutTimeout)
Sets the channel checkout timeout.
|
void |
setCloseExceptionLogger(ConditionalExceptionLogger closeExceptionLogger)
Set the strategy for logging close exceptions; by default, if a channel is closed due to a failed
passive queue declaration, it is logged at debug level.
|
void |
setConnectionCacheSize(int connectionCacheSize) |
void |
setConnectionLimit(int connectionLimit)
Set the connection limit when using cache mode CONNECTION.
|
void |
setConnectionListeners(java.util.List<? extends ConnectionListener> listeners) |
void |
setPublisherConfirms(boolean publisherConfirms)
Use full publisher confirms, with correlation data and a callback for each message.
|
void |
setPublisherReturns(boolean publisherReturns) |
void |
setSimplePublisherConfirms(boolean simplePublisherConfirms)
Use simple publisher confirms where the template simply waits for completion.
|
void |
shutdownCompleted(com.rabbitmq.client.ShutdownSignalException cause) |
java.lang.String |
toString() |
addChannelListener, clearConnectionListeners, createBareConnection, getApplicationContext, getApplicationEventPublisher, getBeanName, getChannelListener, getCloseTimeout, getConnectionListener, getContextStopped, getDefaultHostName, getExecutorService, getHost, getPort, getPublisherConnectionFactory, getRabbitConnectionFactory, getUsername, getVirtualHost, hasPublisherConnectionFactory, onApplicationEvent, removeConnectionListener, setAddresses, setApplicationContext, setApplicationEventPublisher, setBeanName, setChannelListeners, setCloseTimeout, setConnectionNameStrategy, setConnectionThreadFactory, setConnectionTimeout, setExecutor, setHost, setPassword, setPort, setPublisherConnectionFactory, setRecoveryListener, setRequestedHeartBeat, setUri, setUri, setUsername, setVirtualHostpublic CachingConnectionFactory()
public CachingConnectionFactory(java.lang.String hostname)
hostname - the host name to connect topublic CachingConnectionFactory(int port)
port - the port numberpublic CachingConnectionFactory(java.lang.String hostname,
int port)
hostname - the host name to connect toport - the port numberpublic CachingConnectionFactory(java.net.URI uri)
URI.uri - the amqp uri configuring the connectionpublic CachingConnectionFactory(com.rabbitmq.client.ConnectionFactory rabbitConnectionFactory)
rabbitConnectionFactory - the target ConnectionFactorypublic void setChannelCacheSize(int sessionCacheSize)
setChannelCheckoutTimeout(long).sessionCacheSize - the channel cache size.setChannelCheckoutTimeout(long)public int getChannelCacheSize()
public CachingConnectionFactory.CacheMode getCacheMode()
public void setCacheMode(CachingConnectionFactory.CacheMode cacheMode)
public int getConnectionCacheSize()
public void setConnectionCacheSize(int connectionCacheSize)
public void setConnectionLimit(int connectionLimit)
channelCheckoutTimeLimit is used to wait
for a connection to become idle.connectionLimit - the limit.public boolean isPublisherConfirms()
ConnectionFactoryisPublisherConfirms in interface ConnectionFactorypublic boolean isPublisherReturns()
ConnectionFactoryisPublisherReturns in interface ConnectionFactorypublic void setPublisherReturns(boolean publisherReturns)
public void setPublisherConfirms(boolean publisherConfirms)
publisherConfirms - true for full publisher returns,setSimplePublisherConfirms(boolean)public void setSimplePublisherConfirms(boolean simplePublisherConfirms)
simplePublisherConfirms - true for confirms.setPublisherConfirms(boolean)public boolean isSimplePublisherConfirms()
ConnectionFactoryisSimplePublisherConfirms in interface ConnectionFactorypublic void setChannelCheckoutTimeout(long channelCheckoutTimeout)
channelCacheSize becomes the total number of available channels per
connection rather than a simple cache size. Note that changing the channelCacheSize
does not affect the limit on existing connection(s), invoke destroy() to cause a
new connection to be created with the new limit.
Since 1.5.5, also applies to getting a connection when the cache mode is CONNECTION.
channelCheckoutTimeout - the timeout in milliseconds; default 0 (channel limiting not enabled).setConnectionLimit(int)public void setCloseExceptionLogger(ConditionalExceptionLogger closeExceptionLogger)
closeExceptionLogger - the ConditionalExceptionLogger.public void afterPropertiesSet()
throws java.lang.Exception
afterPropertiesSet in interface org.springframework.beans.factory.InitializingBeanjava.lang.Exceptionpublic void setConnectionListeners(java.util.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()
resetConnection() to close the
connection while the application is still running.
As this bean implements DisposableBean, a bean factory will automatically invoke this on destruction of its cached singletons.
If called after the context is closed, the connection factory can no longer server up connections.
destroy in interface org.springframework.beans.factory.DisposableBeandestroy in class AbstractConnectionFactorypublic void resetConnection()
protected void reset(java.util.List<ChannelProxy> channels, java.util.List<ChannelProxy> txChannels, java.util.Map<com.rabbitmq.client.Channel,ChannelProxy> channelsAwaitingAcks)
protected void closeAndClear(java.util.Collection<ChannelProxy> theChannels)
protected void closeChannels(java.util.Collection<ChannelProxy> theChannels)
@ManagedAttribute public java.util.Properties getCacheProperties()
@ManagedAttribute public java.util.Properties getPublisherConnectionFactoryCacheProperties()
protected java.util.concurrent.ExecutorService getDeferredCloseExecutor()
public java.lang.String toString()
toString in class AbstractConnectionFactory