Class AbstractConnectionFactory
- java.lang.Object
-
- org.springframework.amqp.rabbit.connection.AbstractConnectionFactory
-
- All Implemented Interfaces:
com.rabbitmq.client.ShutdownListener,java.util.EventListener,ConnectionFactory,Aware,BeanNameAware,DisposableBean,ApplicationContextAware,ApplicationEventPublisherAware,ApplicationListener<ContextClosedEvent>
- Direct Known Subclasses:
CachingConnectionFactory,PooledChannelConnectionFactory,ThreadChannelConnectionFactory
public abstract class AbstractConnectionFactory extends java.lang.Object implements ConnectionFactory, DisposableBean, BeanNameAware, ApplicationContextAware, ApplicationEventPublisherAware, ApplicationListener<ContextClosedEvent>, com.rabbitmq.client.ShutdownListener
- Author:
- Dave Syer, Gary Russell, Steve Powell, Artem Bilan, Will Droste
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classAbstractConnectionFactory.AddressShuffleModeThe mode used to shuffle the addresses.
-
Field Summary
Fields Modifier and Type Field Description static intDEFAULT_CLOSE_TIMEOUTprotected Loglogger
-
Constructor Summary
Constructors Constructor Description AbstractConnectionFactory(com.rabbitmq.client.ConnectionFactory rabbitConnectionFactory)Create a new AbstractConnectionFactory for the given target ConnectionFactory, with no publisher connection factory.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidaddChannelListener(ChannelListener listener)voidaddConnectionListener(ConnectionListener listener)voidclearConnectionListeners()protected ConnectioncreateBareConnection()voiddestroy()protected voiddoSetPublisherConnectionFactory(AbstractConnectionFactory publisherConnectionFactory)protected ApplicationContextgetApplicationContext()protected ApplicationEventPublishergetApplicationEventPublisher()protected java.lang.StringgetBeanName()Return a bean name of the component or null if not a bean.protected ChannelListenergetChannelListener()A composite channel listener to be used by subclasses when creating and closing channels.intgetCloseTimeout()protected ConnectionListenergetConnectionListener()A composite connection listener to be used by subclasses when creating and closing connections.protected ConnectionNameStrategygetConnectionNameStrategy()protected booleangetContextStopped()protected java.lang.StringgetDefaultHostName()protected java.util.concurrent.ExecutorServicegetExecutorService()java.lang.StringgetHost()intgetPort()ConnectionFactorygetPublisherConnectionFactory()Return a separate connection factory for publishers (if implemented).com.rabbitmq.client.ConnectionFactorygetRabbitConnectionFactory()Return a reference to the underlying Rabbit Connection factory.java.lang.StringgetUsername()Return the user name from the underlying rabbit connection factory.java.lang.StringgetVirtualHost()booleanhasPublisherConnectionFactory()voidonApplicationEvent(ContextClosedEvent event)booleanremoveConnectionListener(ConnectionListener listener)voidsetAddresses(java.lang.String addresses)Set addresses for clustering.voidsetAddressResolver(com.rabbitmq.client.AddressResolver addressResolver)Set anAddressResolverto use when creating connections; overridessetAddresses(String),setHost(String), andsetPort(int).voidsetAddressShuffleMode(AbstractConnectionFactory.AddressShuffleMode addressShuffleMode)Set the mode for shuffling addresses.voidsetApplicationContext(ApplicationContext applicationContext)voidsetApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher)voidsetBeanName(java.lang.String name)voidsetChannelListeners(java.util.List<? extends ChannelListener> listeners)voidsetCloseExceptionLogger(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.voidsetCloseTimeout(int closeTimeout)How long to wait (milliseconds) for a response to a connection close operation from the broker; default 30000 (30 seconds).voidsetConnectionListeners(java.util.List<? extends ConnectionListener> listeners)voidsetConnectionNameStrategy(ConnectionNameStrategy connectionNameStrategy)Provide aConnectionNameStrategyto build the name for the target RabbitMQ connection.voidsetConnectionThreadFactory(java.util.concurrent.ThreadFactory threadFactory)Set theThreadFactoryon the underlying rabbit connection factory.voidsetConnectionTimeout(int connectionTimeout)voidsetExecutor(java.util.concurrent.Executor executor)Provide an Executor for use by the Rabbit ConnectionFactory when creating connections.voidsetHost(java.lang.String host)voidsetPassword(java.lang.String password)voidsetPort(int port)voidsetPublisherConnectionFactory(AbstractConnectionFactory publisherConnectionFactory)Set a custom publisher connection factory; the type does not need to be the same as this factory.voidsetRecoveryListener(com.rabbitmq.client.RecoveryListener recoveryListener)Set aRecoveryListenerthat will be added to each connection created.voidsetRequestedHeartBeat(int requestedHeartBeat)voidsetShuffleAddresses(boolean shuffleAddresses)Deprecated.since 2.3 in favor ofvoidsetUri(java.lang.String uri)voidsetUri(java.net.URI uri)voidsetUsername(java.lang.String username)voidsetVirtualHost(java.lang.String virtualHost)voidshutdownCompleted(com.rabbitmq.client.ShutdownSignalException cause)java.lang.StringtoString()-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.springframework.amqp.rabbit.connection.ConnectionFactory
createConnection, isPublisherConfirms, isPublisherReturns, isSimplePublisherConfirms, resetConnection
-
-
-
-
Field Detail
-
DEFAULT_CLOSE_TIMEOUT
public static final int DEFAULT_CLOSE_TIMEOUT
- See Also:
- Constant Field Values
-
logger
protected final Log logger
-
-
Constructor Detail
-
AbstractConnectionFactory
public AbstractConnectionFactory(com.rabbitmq.client.ConnectionFactory rabbitConnectionFactory)
Create a new AbstractConnectionFactory for the given target ConnectionFactory, with no publisher connection factory.- Parameters:
rabbitConnectionFactory- the target ConnectionFactory
-
-
Method Detail
-
setPublisherConnectionFactory
public void setPublisherConnectionFactory(@Nullable AbstractConnectionFactory publisherConnectionFactory)
Set a custom publisher connection factory; the type does not need to be the same as this factory.- Parameters:
publisherConnectionFactory- the factory.- Since:
- 2.3.2
-
doSetPublisherConnectionFactory
protected final void doSetPublisherConnectionFactory(@Nullable AbstractConnectionFactory publisherConnectionFactory)
-
setApplicationContext
public void setApplicationContext(ApplicationContext applicationContext)
- Specified by:
setApplicationContextin interfaceApplicationContextAware
-
getApplicationContext
protected ApplicationContext getApplicationContext()
-
setApplicationEventPublisher
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher)
- Specified by:
setApplicationEventPublisherin interfaceApplicationEventPublisherAware
-
getApplicationEventPublisher
protected ApplicationEventPublisher getApplicationEventPublisher()
-
onApplicationEvent
public void onApplicationEvent(ContextClosedEvent event)
- Specified by:
onApplicationEventin interfaceApplicationListener<ContextClosedEvent>
-
getContextStopped
protected boolean getContextStopped()
-
getRabbitConnectionFactory
public com.rabbitmq.client.ConnectionFactory getRabbitConnectionFactory()
Return a reference to the underlying Rabbit Connection factory.- Returns:
- the connection factory.
- Since:
- 1.5.6
-
getUsername
public java.lang.String getUsername()
Return the user name from the underlying rabbit connection factory.- Specified by:
getUsernamein interfaceConnectionFactory- Returns:
- the user name.
- Since:
- 1.6
-
setUsername
public void setUsername(java.lang.String username)
-
setPassword
public void setPassword(java.lang.String password)
-
setHost
public void setHost(java.lang.String host)
-
setConnectionThreadFactory
public void setConnectionThreadFactory(java.util.concurrent.ThreadFactory threadFactory)
Set theThreadFactoryon the underlying rabbit connection factory.- Parameters:
threadFactory- the thread factory.- Since:
- 1.5.3
-
setAddressResolver
public void setAddressResolver(com.rabbitmq.client.AddressResolver addressResolver)
Set anAddressResolverto use when creating connections; overridessetAddresses(String),setHost(String), andsetPort(int).- Parameters:
addressResolver- the resolver.- Since:
- 2.1.15
-
setUri
public void setUri(java.net.URI uri)
- Parameters:
uri- the URI- Since:
- 1.5
- See Also:
ConnectionFactory.setUri(URI)
-
setUri
public void setUri(java.lang.String uri)
- Parameters:
uri- the URI- Since:
- 1.5
- See Also:
ConnectionFactory.setUri(String)
-
getHost
public java.lang.String getHost()
- Specified by:
getHostin interfaceConnectionFactory
-
setVirtualHost
public void setVirtualHost(java.lang.String virtualHost)
-
getVirtualHost
public java.lang.String getVirtualHost()
- Specified by:
getVirtualHostin interfaceConnectionFactory
-
setPort
public void setPort(int port)
-
setRequestedHeartBeat
public void setRequestedHeartBeat(int requestedHeartBeat)
-
setConnectionTimeout
public void setConnectionTimeout(int connectionTimeout)
-
getPort
public int getPort()
- Specified by:
getPortin interfaceConnectionFactory
-
setAddresses
public void setAddresses(java.lang.String addresses)
Set addresses for clustering. This property overrides the host+port properties if not empty.- Parameters:
addresses- list of addresses with form "host[:port],..."
-
getConnectionListener
protected ConnectionListener getConnectionListener()
A composite connection listener to be used by subclasses when creating and closing connections.- Returns:
- the connection listener
-
getChannelListener
protected ChannelListener getChannelListener()
A composite channel listener to be used by subclasses when creating and closing channels.- Returns:
- the channel listener
-
setConnectionListeners
public void setConnectionListeners(java.util.List<? extends ConnectionListener> listeners)
-
addConnectionListener
public void addConnectionListener(ConnectionListener listener)
- Specified by:
addConnectionListenerin interfaceConnectionFactory
-
removeConnectionListener
public boolean removeConnectionListener(ConnectionListener listener)
- Specified by:
removeConnectionListenerin interfaceConnectionFactory
-
clearConnectionListeners
public void clearConnectionListeners()
- Specified by:
clearConnectionListenersin interfaceConnectionFactory
-
setChannelListeners
public void setChannelListeners(java.util.List<? extends ChannelListener> listeners)
-
setRecoveryListener
public void setRecoveryListener(com.rabbitmq.client.RecoveryListener recoveryListener)
Set aRecoveryListenerthat will be added to each connection created.- Parameters:
recoveryListener- the listener.- Since:
- 2.0
-
addChannelListener
public void addChannelListener(ChannelListener listener)
-
setExecutor
public void setExecutor(java.util.concurrent.Executor executor)
Provide an Executor for use by the Rabbit ConnectionFactory when creating connections. Can either be an ExecutorService or a Spring ThreadPoolTaskExecutor, as defined by a <task:executor/> element.- Parameters:
executor- The executor.
-
getExecutorService
@Nullable protected java.util.concurrent.ExecutorService getExecutorService()
-
setCloseTimeout
public void setCloseTimeout(int closeTimeout)
How long to wait (milliseconds) for a response to a connection close operation from the broker; default 30000 (30 seconds).- Parameters:
closeTimeout- the closeTimeout to set.
-
getCloseTimeout
public int getCloseTimeout()
-
setConnectionNameStrategy
public void setConnectionNameStrategy(ConnectionNameStrategy connectionNameStrategy)
Provide aConnectionNameStrategyto build the name for the target RabbitMQ connection. ThebeanNametogether with a counter is used by default.- Parameters:
connectionNameStrategy- theConnectionNameStrategyto use.- Since:
- 2.0
-
setCloseExceptionLogger
public 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. Normal channel closes (200 OK) are not logged. All others are logged at ERROR level (unless access is refused due to an exclusive consumer condition, in which case, it is logged at INFO level).- Parameters:
closeExceptionLogger- theConditionalExceptionLogger.- Since:
- 1.5
-
getConnectionNameStrategy
protected ConnectionNameStrategy getConnectionNameStrategy()
-
setBeanName
public void setBeanName(java.lang.String name)
- Specified by:
setBeanNamein interfaceBeanNameAware
-
getBeanName
@Nullable protected java.lang.String getBeanName()
Return a bean name of the component or null if not a bean.- Returns:
- the bean name or null.
- Since:
- 1.7.9
-
setShuffleAddresses
@Deprecated public void setShuffleAddresses(boolean shuffleAddresses)
Deprecated.since 2.3 in favor ofWhenaddressesare provided and there is more than one, set to true to shuffle the list before opening a new connection so that the connection to the broker will be attempted in random order.- Parameters:
shuffleAddresses- true to shuffle the list.- Since:
- 2.1.8
- See Also:
.
-
setAddressShuffleMode
public void setAddressShuffleMode(AbstractConnectionFactory.AddressShuffleMode addressShuffleMode)
Set the mode for shuffling addresses.- Parameters:
addressShuffleMode- the address shuffle mode.- Since:
- 2.3
- See Also:
Collections.shuffle(List)
-
hasPublisherConnectionFactory
public boolean hasPublisherConnectionFactory()
-
getPublisherConnectionFactory
public ConnectionFactory getPublisherConnectionFactory()
Description copied from interface:ConnectionFactoryReturn a separate connection factory for publishers (if implemented).- Specified by:
getPublisherConnectionFactoryin interfaceConnectionFactory- Returns:
- the publisher connection factory, or null.
-
createBareConnection
protected final Connection createBareConnection()
-
getDefaultHostName
protected final java.lang.String getDefaultHostName()
-
shutdownCompleted
public void shutdownCompleted(com.rabbitmq.client.ShutdownSignalException cause)
- Specified by:
shutdownCompletedin interfacecom.rabbitmq.client.ShutdownListener
-
destroy
public void destroy()
- Specified by:
destroyin interfaceDisposableBean
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-