Class AbstractRoutingConnectionFactory
- java.lang.Object
-
- org.springframework.amqp.rabbit.connection.AbstractRoutingConnectionFactory
-
- All Implemented Interfaces:
ConnectionFactory
,RoutingConnectionFactory
,DisposableBean
,InitializingBean
- Direct Known Subclasses:
SimpleRoutingConnectionFactory
public abstract class AbstractRoutingConnectionFactory extends java.lang.Object implements ConnectionFactory, RoutingConnectionFactory, InitializingBean, DisposableBean
AbstractConnectionFactory
implementation that routescreateConnection()
calls to one of various target ConnectionFactories based on a lookup key. The latter is usually (but not necessarily) determined through some thread-bound context.- Since:
- 1.3
- Author:
- Artem Bilan, Josh Chappelle, Gary Russell, Leonardo Ferreira
-
-
Constructor Summary
Constructors Constructor Description AbstractRoutingConnectionFactory()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
addConnectionListener(ConnectionListener listener)
protected void
addTargetConnectionFactory(java.lang.Object key, ConnectionFactory connectionFactory)
Adds the givenConnectionFactory
and associates it with the given lookup key.void
afterPropertiesSet()
void
clearConnectionListeners()
Connection
createConnection()
void
destroy()
protected abstract java.lang.Object
determineCurrentLookupKey()
Determine the current lookup key.protected ConnectionFactory
determineTargetConnectionFactory()
Retrieve the current targetConnectionFactory
.java.lang.String
getHost()
int
getPort()
ConnectionFactory
getTargetConnectionFactory(java.lang.Object key)
Returns theConnectionFactory
bound to given lookup key, or null if one does not exist.java.lang.String
getUsername()
java.lang.String
getVirtualHost()
boolean
isLenientFallback()
boolean
isPublisherConfirms()
Return true if publisher confirms are enabled.boolean
isPublisherReturns()
Return true if publisher returns are enabled.boolean
removeConnectionListener(ConnectionListener listener)
protected ConnectionFactory
removeTargetConnectionFactory(java.lang.Object key)
Removes theConnectionFactory
associated with the given lookup key and returns it.void
resetConnection()
Close any connection(s) that might be cached by this factory.void
setConsistentConfirmsReturns(boolean consistentConfirmsReturns)
Specify whether to apply a validation enforcing allConnectionFactory.isPublisherConfirms()
andConnectionFactory.isPublisherReturns()
have a consistent value.void
setDefaultTargetConnectionFactory(ConnectionFactory defaultTargetConnectionFactory)
Specify the default targetConnectionFactory
, if any.void
setLenientFallback(boolean lenientFallback)
Specify whether to apply a lenient fallback to the defaultConnectionFactory
if no specificConnectionFactory
could be found for the current lookup key.void
setTargetConnectionFactories(java.util.Map<java.lang.Object,ConnectionFactory> targetConnectionFactories)
Specify the map of target ConnectionFactories, with the lookup key as key.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.springframework.amqp.rabbit.connection.ConnectionFactory
getPublisherConnectionFactory, isSimplePublisherConfirms
-
-
-
-
Method Detail
-
setTargetConnectionFactories
public void setTargetConnectionFactories(java.util.Map<java.lang.Object,ConnectionFactory> targetConnectionFactories)
Specify the map of target ConnectionFactories, with the lookup key as key.The key can be of arbitrary type; this class implements the generic lookup process only. The concrete key representation will be handled by
determineCurrentLookupKey()
.- Parameters:
targetConnectionFactories
- The target connection factories and lookup keys.
-
setDefaultTargetConnectionFactory
public void setDefaultTargetConnectionFactory(ConnectionFactory defaultTargetConnectionFactory)
Specify the default targetConnectionFactory
, if any.This
ConnectionFactory
will be used as target if none of the keyedtargetConnectionFactories
match thedetermineCurrentLookupKey()
current lookup key.- Parameters:
defaultTargetConnectionFactory
- The default target connection factory.
-
setLenientFallback
public void setLenientFallback(boolean lenientFallback)
Specify whether to apply a lenient fallback to the defaultConnectionFactory
if no specificConnectionFactory
could be found for the current lookup key.Default is "true", accepting lookup keys without a corresponding entry in the
targetConnectionFactories
- simply falling back to the defaultConnectionFactory
in that case.Switch this flag to "false" if you would prefer the fallback to only apply if the lookup key was
null
. Lookup keys without aConnectionFactory
entry will then lead to anIllegalStateException
.- Parameters:
lenientFallback
- true to fall back to the default, if specified.- See Also:
setTargetConnectionFactories(java.util.Map<java.lang.Object, org.springframework.amqp.rabbit.connection.ConnectionFactory>)
,setDefaultTargetConnectionFactory(org.springframework.amqp.rabbit.connection.ConnectionFactory)
,determineCurrentLookupKey()
-
isLenientFallback
public boolean isLenientFallback()
-
isPublisherConfirms
public boolean isPublisherConfirms()
Description copied from interface:ConnectionFactory
Return true if publisher confirms are enabled.- Specified by:
isPublisherConfirms
in interfaceConnectionFactory
- Returns:
- publisherConfirms.
-
isPublisherReturns
public boolean isPublisherReturns()
Description copied from interface:ConnectionFactory
Return true if publisher returns are enabled.- Specified by:
isPublisherReturns
in interfaceConnectionFactory
- Returns:
- publisherReturns.
-
afterPropertiesSet
public void afterPropertiesSet() throws java.lang.Exception
- Specified by:
afterPropertiesSet
in interfaceInitializingBean
- Throws:
java.lang.Exception
-
createConnection
public Connection createConnection() throws AmqpException
- Specified by:
createConnection
in interfaceConnectionFactory
- Throws:
AmqpException
-
determineTargetConnectionFactory
protected ConnectionFactory determineTargetConnectionFactory()
Retrieve the current targetConnectionFactory
. Determines thecurrent lookup key
, performs a lookup in thetargetConnectionFactories
map, falls back to the specifieddefaultTargetConnectionFactory
if necessary.- Returns:
- The connection factory.
- See Also:
determineCurrentLookupKey()
-
addConnectionListener
public void addConnectionListener(ConnectionListener listener)
- Specified by:
addConnectionListener
in interfaceConnectionFactory
-
removeConnectionListener
public boolean removeConnectionListener(ConnectionListener listener)
- Specified by:
removeConnectionListener
in interfaceConnectionFactory
-
clearConnectionListeners
public void clearConnectionListeners()
- Specified by:
clearConnectionListeners
in interfaceConnectionFactory
-
getHost
public java.lang.String getHost()
- Specified by:
getHost
in interfaceConnectionFactory
-
getPort
public int getPort()
- Specified by:
getPort
in interfaceConnectionFactory
-
getVirtualHost
public java.lang.String getVirtualHost()
- Specified by:
getVirtualHost
in interfaceConnectionFactory
-
getUsername
public java.lang.String getUsername()
- Specified by:
getUsername
in interfaceConnectionFactory
-
getTargetConnectionFactory
public ConnectionFactory getTargetConnectionFactory(java.lang.Object key)
Description copied from interface:RoutingConnectionFactory
Returns theConnectionFactory
bound to given lookup key, or null if one does not exist.- Specified by:
getTargetConnectionFactory
in interfaceRoutingConnectionFactory
- Parameters:
key
- The lookup key to which theConnectionFactory
is bound- Returns:
- the
ConnectionFactory
bound to the given lookup key, or null if one does not exist
-
setConsistentConfirmsReturns
public void setConsistentConfirmsReturns(boolean consistentConfirmsReturns)
Specify whether to apply a validation enforcing allConnectionFactory.isPublisherConfirms()
andConnectionFactory.isPublisherReturns()
have a consistent value.A consistent value means that all ConnectionFactories must have the same value between all
ConnectionFactory.isPublisherConfirms()
and the same value between allConnectionFactory.isPublisherReturns()
.Note that in any case the values between
ConnectionFactory.isPublisherConfirms()
andConnectionFactory.isPublisherReturns()
don't need to be equals between each other.- Parameters:
consistentConfirmsReturns
- true to validate, false to not validate.- Since:
- 2.4.4
-
addTargetConnectionFactory
protected void addTargetConnectionFactory(java.lang.Object key, ConnectionFactory connectionFactory)
Adds the givenConnectionFactory
and associates it with the given lookup key.- Parameters:
key
- the lookup key.connectionFactory
- theConnectionFactory
.
-
removeTargetConnectionFactory
protected ConnectionFactory removeTargetConnectionFactory(java.lang.Object key)
Removes theConnectionFactory
associated with the given lookup key and returns it.- Parameters:
key
- the lookup key- Returns:
- the
ConnectionFactory
that was removed
-
determineCurrentLookupKey
@Nullable protected abstract java.lang.Object determineCurrentLookupKey()
Determine the current lookup key. This will typically be implemented to check a thread-bound context.- Returns:
- The lookup key.
-
destroy
public void destroy()
- Specified by:
destroy
in interfaceDisposableBean
-
resetConnection
public void resetConnection()
Description copied from interface:ConnectionFactory
Close any connection(s) that might be cached by this factory. This does not prevent new connections from being opened.- Specified by:
resetConnection
in interfaceConnectionFactory
-
-