public class CachingConnectionFactory extends AbstractConnectionFactory implements org.springframework.beans.factory.InitializingBean, com.rabbitmq.client.ShutdownListener, org.springframework.context.ApplicationContextAware, org.springframework.context.ApplicationListener<org.springframework.context.event.ContextClosedEvent>, PublisherCallbackChannelConnectionFactory, org.springframework.context.SmartLifecycle
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.
|
CachingConnectionFactory(URI uri)
Create a new CachingConnectionFactory given a
URI. |
| 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()
Deprecated.
|
int |
getConnectionCacheSize() |
int |
getPhase() |
boolean |
isAutoStartup() |
boolean |
isPublisherConfirms() |
boolean |
isPublisherReturns() |
boolean |
isRunning() |
void |
onApplicationEvent(org.springframework.context.event.ContextClosedEvent event) |
protected void |
reset(List<ChannelProxy> channels,
List<ChannelProxy> txChannels) |
void |
resetConnection()
Close the connection(s).
|
void |
setApplicationContext(org.springframework.context.ApplicationContext applicationContext) |
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 |
setPhase(int phase)
Defaults to phase
- 1000 so the factory is
stopped in a very late phase, allowing other beans to use the connection
to clean up. |
void |
setPublisherConfirms(boolean publisherConfirms) |
void |
setPublisherReturns(boolean publisherReturns) |
void |
shutdownCompleted(com.rabbitmq.client.ShutdownSignalException cause) |
void |
start() |
void |
stop()
Stop the connection factory to prevent its connection from being used.
|
void |
stop(Runnable callback) |
String |
toString() |
addChannelListener, clearConnectionListeners, createBareConnection, getChannelListener, getCloseTimeout, getConnectionListener, getDefaultHostName, getExecutorService, getHost, getPort, getRabbitConnectionFactory, getVirtualHost, removeConnectionListener, setAddresses, setBeanName, setChannelListeners, setCloseTimeout, setConnectionThreadFactory, setConnectionTimeout, setExecutor, setHost, setPassword, setPort, setRequestedHeartBeat, setUri, setUri, setUsername, setVirtualHostpublic CachingConnectionFactory()
public CachingConnectionFactory(String hostname, int port)
hostname - the host name to connect toport - the port numberpublic CachingConnectionFactory(URI uri)
URI.uri - the amqp uri configuring the connectionpublic 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)
setChannelCheckoutTimeout(long).sessionCacheSize - the channel cache size.setChannelCheckoutTimeout(long)public int getChannelCacheSize()
public CachingConnectionFactory.CacheMode getCacheMode()
public void setCacheMode(CachingConnectionFactory.CacheMode cacheMode)
@Deprecated public int getConnectionCachesize()
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()
isPublisherConfirms in interface PublisherCallbackChannelConnectionFactorypublic boolean isPublisherReturns()
isPublisherReturns in interface PublisherCallbackChannelConnectionFactorypublic void setPublisherReturns(boolean publisherReturns)
public void setPublisherConfirms(boolean publisherConfirms)
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 afterPropertiesSet()
throws Exception
afterPropertiesSet in interface org.springframework.beans.factory.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 void setApplicationContext(org.springframework.context.ApplicationContext applicationContext)
throws org.springframework.beans.BeansException
setApplicationContext in interface org.springframework.context.ApplicationContextAwareorg.springframework.beans.BeansExceptionpublic void onApplicationEvent(org.springframework.context.event.ContextClosedEvent event)
onApplicationEvent in interface org.springframework.context.ApplicationListener<org.springframework.context.event.ContextClosedEvent>public 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 org.springframework.beans.factory.DisposableBeandestroy in class AbstractConnectionFactorypublic void resetConnection()
public void start()
start in interface org.springframework.context.Lifecyclepublic boolean isRunning()
isRunning in interface org.springframework.context.Lifecyclepublic int getPhase()
getPhase in interface org.springframework.context.Phasedpublic void setPhase(int phase)
- 1000 so the factory is
stopped in a very late phase, allowing other beans to use the connection
to clean up.phase - the phase.getPhase()public boolean isAutoStartup()
isAutoStartup in interface org.springframework.context.SmartLifecyclepublic void stop()
stop in interface org.springframework.context.Lifecyclepublic void stop(Runnable callback)
stop in interface org.springframework.context.SmartLifecycleprotected void reset(List<ChannelProxy> channels, List<ChannelProxy> txChannels)
public String toString()
toString in class AbstractConnectionFactory