public abstract class AbstractRoutingConnectionFactory extends Object implements ConnectionFactory, RoutingConnectionFactory
ConnectionFactory
implementation that routes createConnection()
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.Constructor and Description |
---|
AbstractRoutingConnectionFactory() |
Modifier and Type | Method and Description |
---|---|
void |
addConnectionListener(ConnectionListener listener) |
protected void |
addTargetConnectionFactory(Object key,
ConnectionFactory connectionFactory)
Adds the given
ConnectionFactory and associates it with the given lookup key. |
void |
clearConnectionListeners() |
Connection |
createConnection() |
protected abstract Object |
determineCurrentLookupKey()
Determine the current lookup key.
|
protected ConnectionFactory |
determineTargetConnectionFactory()
Retrieve the current target
ConnectionFactory . |
String |
getHost() |
int |
getPort() |
ConnectionFactory |
getTargetConnectionFactory(Object key)
Returns the
ConnectionFactory bound to given lookup key, or null if one does not exist. |
String |
getUsername() |
String |
getVirtualHost() |
boolean |
isLenientFallback() |
boolean |
removeConnectionListener(ConnectionListener listener) |
protected ConnectionFactory |
removeTargetConnectionFactory(Object key)
Removes the
ConnectionFactory associated with the given lookup key and returns it. |
void |
setDefaultTargetConnectionFactory(ConnectionFactory defaultTargetConnectionFactory)
Specify the default target
ConnectionFactory , if any. |
void |
setLenientFallback(boolean lenientFallback)
Specify whether to apply a lenient fallback to the default
ConnectionFactory
if no specific ConnectionFactory could be found for the current lookup key. |
void |
setTargetConnectionFactories(Map<Object,ConnectionFactory> targetConnectionFactories)
Specify the map of target ConnectionFactories, with the lookup key as key.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
getPublisherConnectionFactory, isPublisherConfirms, isPublisherReturns, isSimplePublisherConfirms
public void setTargetConnectionFactories(Map<Object,ConnectionFactory> targetConnectionFactories)
The key can be of arbitrary type; this class implements the
generic lookup process only. The concrete key representation will
be handled by determineCurrentLookupKey()
.
targetConnectionFactories
- The target connection factories and lookup keys.public void setDefaultTargetConnectionFactory(ConnectionFactory defaultTargetConnectionFactory)
ConnectionFactory
, if any.
This ConnectionFactory
will be used as target if none of the keyed
targetConnectionFactories
match the
determineCurrentLookupKey()
current lookup key.
defaultTargetConnectionFactory
- The default target connection factory.public void setLenientFallback(boolean lenientFallback)
ConnectionFactory
if no specific ConnectionFactory
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 default
ConnectionFactory
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 a ConnectionFactory
entry will then lead to an IllegalStateException
.
lenientFallback
- true to fall back to the default, if specified.setTargetConnectionFactories(java.util.Map<java.lang.Object, org.springframework.amqp.rabbit.connection.ConnectionFactory>)
,
setDefaultTargetConnectionFactory(org.springframework.amqp.rabbit.connection.ConnectionFactory)
,
determineCurrentLookupKey()
public boolean isLenientFallback()
public Connection createConnection() throws AmqpException
createConnection
in interface ConnectionFactory
AmqpException
protected ConnectionFactory determineTargetConnectionFactory()
ConnectionFactory
. Determines the
current lookup key
, performs
a lookup in the targetConnectionFactories
map,
falls back to the specified
defaultTargetConnectionFactory
if necessary.determineCurrentLookupKey()
public void addConnectionListener(ConnectionListener listener)
addConnectionListener
in interface ConnectionFactory
public boolean removeConnectionListener(ConnectionListener listener)
removeConnectionListener
in interface ConnectionFactory
public void clearConnectionListeners()
clearConnectionListeners
in interface ConnectionFactory
public String getHost()
getHost
in interface ConnectionFactory
public int getPort()
getPort
in interface ConnectionFactory
public String getVirtualHost()
getVirtualHost
in interface ConnectionFactory
public String getUsername()
getUsername
in interface ConnectionFactory
public ConnectionFactory getTargetConnectionFactory(Object key)
RoutingConnectionFactory
ConnectionFactory
bound to given lookup key, or null if one does not exist.getTargetConnectionFactory
in interface RoutingConnectionFactory
key
- The lookup key to which the ConnectionFactory
is boundConnectionFactory
bound to the given lookup key, or null if one does not existprotected void addTargetConnectionFactory(Object key, ConnectionFactory connectionFactory)
ConnectionFactory
and associates it with the given lookup key.key
- the lookup key.connectionFactory
- the ConnectionFactory
.protected ConnectionFactory removeTargetConnectionFactory(Object key)
ConnectionFactory
associated with the given lookup key and returns it.key
- the lookup keyConnectionFactory
that was removed