public class CachingConnectionFactory extends AbstractConnectionFactory
ConnectionFactory
implementation that returns the same Connections 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.
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.
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) |
Connection |
createConnection() |
void |
destroy()
Close the underlying shared connection.
|
int |
getChannelCacheSize() |
boolean |
isPublisherConfirms() |
boolean |
isPublisherReturns() |
protected void |
reset()
Reset the Channel cache and underlying shared Connection, to be reinitialized on next access.
|
void |
setChannelCacheSize(int sessionCacheSize) |
void |
setConnectionListeners(List<? extends ConnectionListener> listeners) |
void |
setPublisherConfirms(boolean publisherConfirms) |
void |
setPublisherReturns(boolean publisherReturns) |
String |
toString() |
addChannelListener, createBareConnection, getChannelListener, getConnectionListener, getDefaultHostName, getHost, getPort, getVirtualHost, 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 boolean isPublisherConfirms()
public boolean isPublisherReturns()
public void setPublisherReturns(boolean publisherReturns)
public void setPublisherConfirms(boolean publisherConfirms)
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 final Connection createConnection() throws AmqpException
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 DisposableBean
destroy
in class AbstractConnectionFactory
protected void reset()