Class ThreadChannelConnectionFactory
java.lang.Object
org.springframework.amqp.rabbit.connection.AbstractConnectionFactory
org.springframework.amqp.rabbit.connection.ThreadChannelConnectionFactory
- All Implemented Interfaces:
com.rabbitmq.client.ShutdownListener,EventListener,ConnectionFactory,Aware,BeanNameAware,DisposableBean,ApplicationContextAware,ApplicationEventPublisherAware,ApplicationListener<ContextClosedEvent>,Lifecycle,Phased,SmartLifecycle
public class ThreadChannelConnectionFactory
extends AbstractConnectionFactory
implements com.rabbitmq.client.ShutdownListener, SmartLifecycle
A very simple connection factory that caches a channel per thread. Users are
responsible for releasing the thread's channel by calling
closeThreadChannel().- Since:
- 2.3
- Author:
- Gary Russell, Leonardo Ferreira, Christian Tzolov
-
Nested Class Summary
Nested classes/interfaces inherited from class org.springframework.amqp.rabbit.connection.AbstractConnectionFactory
AbstractConnectionFactory.AddressShuffleMode, AbstractConnectionFactory.DefaultChannelCloseLogger -
Field Summary
Fields inherited from class org.springframework.amqp.rabbit.connection.AbstractConnectionFactory
DEFAULT_CLOSE_TIMEOUT, loggerFields inherited from interface org.springframework.context.SmartLifecycle
DEFAULT_PHASE -
Constructor Summary
ConstructorsConstructorDescriptionThreadChannelConnectionFactory(com.rabbitmq.client.ConnectionFactory rabbitConnectionFactory) Construct an instance. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddConnectionListener(ConnectionListener listener) voidClose the channel associated with this thread, if any.voiddestroy()intgetPhase()booleanbooleanReturn true if simple publisher confirms are enabled.Call to prepare to switch the channel(s) owned by this thread to another thread.voidClose the connection(s).voidsetPublisherConnectionFactory(AbstractConnectionFactory publisherConnectionFactory) Set a custom publisher connection factory; the type does not need to be the same as this factory.voidsetSimplePublisherConfirms(boolean simplePublisherConfirms) Enable simple publisher confirms.voidstart()voidstop()voidswitchContext(Object toSwitch) Acquire ownership of another thread's channel(s) after that thread calledprepareSwitchContext().Methods inherited from class org.springframework.amqp.rabbit.connection.AbstractConnectionFactory
addChannelListener, clearConnectionListeners, createBareConnection, doSetPublisherConnectionFactory, getAddresses, getApplicationContext, getApplicationEventPublisher, getBeanName, getChannelListener, getCloseTimeout, getConnectionListener, getConnectionNameStrategy, getContextStopped, getDefaultHostName, getExecutorService, getHost, getPort, getPublisherConnectionFactory, getRabbitConnectionFactory, getUsername, getVirtualHost, hasPublisherConnectionFactory, onApplicationEvent, removeConnectionListener, setAddresses, setAddressResolver, setAddressShuffleMode, setApplicationContext, setApplicationEventPublisher, setBeanName, setChannelListeners, setCloseExceptionLogger, setCloseTimeout, setConnectionCreatingBackOff, setConnectionListeners, setConnectionNameStrategy, setConnectionThreadFactory, setConnectionTimeout, setExecutor, setHost, setPassword, setPort, setRecoveryListener, setRequestedHeartBeat, setUri, setUri, setUsername, setVirtualHost, shutdownCompleted, toStringMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.springframework.context.ApplicationListener
supportsAsyncExecutionMethods inherited from interface org.springframework.amqp.rabbit.connection.ConnectionFactory
isPublisherConfirms, isPublisherReturnsMethods inherited from interface com.rabbitmq.client.ShutdownListener
shutdownCompletedMethods inherited from interface org.springframework.context.SmartLifecycle
isAutoStartup, stop
-
Constructor Details
-
ThreadChannelConnectionFactory
public ThreadChannelConnectionFactory(com.rabbitmq.client.ConnectionFactory rabbitConnectionFactory) Construct an instance.- Parameters:
rabbitConnectionFactory- the rabbitmq connection factory.
-
-
Method Details
-
setPublisherConnectionFactory
public void setPublisherConnectionFactory(@Nullable AbstractConnectionFactory publisherConnectionFactory) Description copied from class:AbstractConnectionFactorySet a custom publisher connection factory; the type does not need to be the same as this factory.- Overrides:
setPublisherConnectionFactoryin classAbstractConnectionFactory- Parameters:
publisherConnectionFactory- the factory.
-
isSimplePublisherConfirms
public boolean isSimplePublisherConfirms()Description copied from interface:ConnectionFactoryReturn true if simple publisher confirms are enabled.- Specified by:
isSimplePublisherConfirmsin interfaceConnectionFactory- Returns:
- simplePublisherConfirms
-
setSimplePublisherConfirms
public void setSimplePublisherConfirms(boolean simplePublisherConfirms) Enable simple publisher confirms.- Parameters:
simplePublisherConfirms- true to enable.
-
getPhase
public int getPhase()- Specified by:
getPhasein interfacePhased- Specified by:
getPhasein interfaceSmartLifecycle
-
start
-
stop
-
isRunning
-
addConnectionListener
- Specified by:
addConnectionListenerin interfaceConnectionFactory- Overrides:
addConnectionListenerin classAbstractConnectionFactory
-
createConnection
- Specified by:
createConnectionin interfaceConnectionFactory- Throws:
AmqpException
-
closeThreadChannel
public void closeThreadChannel()Close the channel associated with this thread, if any. -
resetConnection
public void resetConnection()Close the connection(s). This will impact any in-process operations. New connection(s) will be created on demand after this method returns. This might be used to force a reconnect to the primary broker after failing over to a secondary broker.- Specified by:
resetConnectionin interfaceConnectionFactory
-
destroy
public void destroy()- Specified by:
destroyin interfaceDisposableBean- Overrides:
destroyin classAbstractConnectionFactory
-
prepareSwitchContext
Call to prepare to switch the channel(s) owned by this thread to another thread.- Returns:
- an opaque object representing the context to switch. If there are no channels or no open channels assigned to this thread, null is returned.
- Since:
- 2.3.7
- See Also:
-
switchContext
Acquire ownership of another thread's channel(s) after that thread calledprepareSwitchContext().- Parameters:
toSwitch- the context returned byprepareSwitchContext().- Since:
- 2.3.7
- See Also:
-