Class AbstractRoutingConnectionFactory
- All Implemented Interfaces:
ConnectionFactory
,InitializingBean
ConnectionFactory
implementation that routes
create()
calls to one of various target
factories based on a lookup key.
The latter is typically (but not necessarily) determined from some
subscriber context.
Allows to configure a default target ConnectionFactory
as a fallback.
Calls to getMetadata()
are routed to the
default ConnectionFactory
if configured.
- Since:
- 5.3
- Author:
- Mark Paluch, Jens Schauder
- See Also:
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Delegates toinitialize()
.reactor.core.publisher.Mono<Connection>
create()
protected abstract reactor.core.publisher.Mono<Object>
Determine the current lookup key.protected reactor.core.publisher.Mono<ConnectionFactory>
Retrieve the current targetConnectionFactory
.void
Initialize the internal state of thisAbstractRoutingConnectionFactory
by resolving the configured target ConnectionFactories.protected ConnectionFactory
resolveSpecifiedConnectionFactory
(Object connectionFactory) Resolve the specified connection factory object into aConnectionFactory
instance.protected Object
resolveSpecifiedLookupKey
(Object lookupKey) Resolve the given lookup key object, as specified in thetargetConnectionFactories
map, into the actual lookup key to be used for matching with thecurrent lookup key
.void
setConnectionFactoryLookup
(ConnectionFactoryLookup connectionFactoryLookup) Set theConnectionFactoryLookup
implementation to use for resolving connection factory name Strings in thetargetConnectionFactories
map.void
setDefaultTargetConnectionFactory
(Object 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
(Map<?, ?> targetConnectionFactories) Specify the map of targetConnectionFactories
, with the lookup key as key.
-
Constructor Details
-
AbstractRoutingConnectionFactory
public AbstractRoutingConnectionFactory()
-
-
Method Details
-
setTargetConnectionFactories
Specify the map of targetConnectionFactories
, with the lookup key as key. The mapped value can either be a correspondingConnectionFactory
instance or a connection factory name String (to be resolved via aConnectionFactoryLookup
).The key can be of arbitrary type; this class implements the generic lookup process only. The concrete key representation will be handled by
resolveSpecifiedLookupKey(Object)
anddetermineCurrentLookupKey()
. -
setDefaultTargetConnectionFactory
Specify the default targetConnectionFactory
, if any.The mapped value can either be a corresponding
ConnectionFactory
instance or a connection factory nameString
(to be resolved via aConnectionFactoryLookup
).This
ConnectionFactory
will be used as target if none of the keyedtargetConnectionFactories
match thecurrent lookup key
. -
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 targetConnectionFactory
map - simply falling back to the defaultConnectionFactory
in that case.Switch this flag to
false
if you would prefer the fallback to only apply when no lookup key was emitted. Lookup keys without aConnectionFactory
entry will then lead to anIllegalStateException
. -
setConnectionFactoryLookup
Set theConnectionFactoryLookup
implementation to use for resolving connection factory name Strings in thetargetConnectionFactories
map. -
afterPropertiesSet
public void afterPropertiesSet()Delegates toinitialize()
.- Specified by:
afterPropertiesSet
in interfaceInitializingBean
-
initialize
public void initialize()Initialize the internal state of thisAbstractRoutingConnectionFactory
by resolving the configured target ConnectionFactories.- Throws:
IllegalArgumentException
- if the target ConnectionFactories have not been configured- Since:
- 6.1
- See Also:
-
resolveSpecifiedLookupKey
Resolve the given lookup key object, as specified in thetargetConnectionFactories
map, into the actual lookup key to be used for matching with thecurrent lookup key
.The default implementation simply returns the given key as-is.
- Parameters:
lookupKey
- the lookup key object as specified by the user- Returns:
- the lookup key as needed for matching.
-
resolveSpecifiedConnectionFactory
protected ConnectionFactory resolveSpecifiedConnectionFactory(Object connectionFactory) throws IllegalArgumentException Resolve the specified connection factory object into aConnectionFactory
instance.The default implementation handles
ConnectionFactory
instances and connection factory names (to be resolved via aConnectionFactoryLookup
).- Parameters:
connectionFactory
- the connection factory value object as specified in thetargetConnectionFactories
map- Returns:
- the resolved
ConnectionFactory
(nevernull
) - Throws:
IllegalArgumentException
- in case of an unsupported value type
-
create
- Specified by:
create
in interfaceConnectionFactory
-
getMetadata
- Specified by:
getMetadata
in interfaceConnectionFactory
-
determineTargetConnectionFactory
Retrieve the current targetConnectionFactory
. Determines thecurrent lookup key
, performs a lookup in thetargetConnectionFactories
map, falls back to the specifieddefault target ConnectionFactory
if necessary.- Returns:
Mono
that emits the currentConnectionFactory
as perdetermineCurrentLookupKey()
- See Also:
-
determineCurrentLookupKey
Determine the current lookup key. This will typically be implemented to check a subscriber context. Allows for arbitrary keys. The returned key needs to match the stored lookup key type, as resolved by theresolveSpecifiedLookupKey(java.lang.Object)
method.- Returns:
Mono
emitting the lookup key. May complete without emitting a value if no lookup key available
-