@ManagedResource 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
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(String hostname)
Create a new CachingConnectionFactory given a host name.
|
CachingConnectionFactory(String hostNameArg,
int port)
Create a new CachingConnectionFactory given a host name
and port.
|
CachingConnectionFactory(URI uri)
Create a new CachingConnectionFactory given a
URI . |
Modifier and Type | Method and Description |
---|---|
void |
addConnectionListener(ConnectionListener listener) |
void |
afterPropertiesSet() |
protected void |
closeAndClear(Collection<ChannelProxy> theChannels) |
protected void |
closeChannels(Collection<ChannelProxy> theChannels) |
Connection |
createConnection() |
void |
destroy()
Close the underlying shared connection.
|
CachingConnectionFactory.CacheMode |
getCacheMode() |
Properties |
getCacheProperties() |
int |
getChannelCacheSize() |
protected ExecutorService |
getChannelsExecutor()
Determine the executor service used for target channels.
|
int |
getConnectionCacheSize() |
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(List<ChannelProxy> channels,
List<ChannelProxy> txChannels,
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(List<? extends ConnectionListener> listeners) |
void |
setPublisherChannelFactory(PublisherCallbackChannelFactory publisherChannelFactory)
Set the factory to use to create
PublisherCallbackChannel instances. |
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) |
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, setShuffleAddresses, setUri, setUri, setUsername, setVirtualHost
public CachingConnectionFactory()
public CachingConnectionFactory(@Nullable String hostname)
hostname
- the host name to connect topublic CachingConnectionFactory(int port)
port
- the port numberpublic CachingConnectionFactory(@Nullable String hostNameArg, int port)
hostNameArg
- the host name to connect toport
- the port numberpublic CachingConnectionFactory(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()
ConnectionFactory
isPublisherConfirms
in interface ConnectionFactory
public boolean isPublisherReturns()
ConnectionFactory
isPublisherReturns
in interface ConnectionFactory
public 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()
ConnectionFactory
isSimplePublisherConfirms
in interface ConnectionFactory
public 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 setPublisherChannelFactory(PublisherCallbackChannelFactory publisherChannelFactory)
PublisherCallbackChannel
instances.publisherChannelFactory
- the factory.public void afterPropertiesSet()
afterPropertiesSet
in interface InitializingBean
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()
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 DisposableBean
destroy
in class AbstractConnectionFactory
public void resetConnection()
protected void reset(List<ChannelProxy> channels, List<ChannelProxy> txChannels, Map<com.rabbitmq.client.Channel,ChannelProxy> channelsAwaitingAcks)
protected void closeAndClear(Collection<ChannelProxy> theChannels)
protected void closeChannels(Collection<ChannelProxy> theChannels)
@ManagedAttribute public Properties getCacheProperties()
@ManagedAttribute public Properties getPublisherConnectionFactoryCacheProperties()
protected ExecutorService getChannelsExecutor()
public String toString()
toString
in class AbstractConnectionFactory