-
Notifications
You must be signed in to change notification settings - Fork 131
Add support for AbstractRoutingConnectionFactory #132
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
We now provide an abstract base class for ConnectionFactory routing. Routing keys are typically obtained from a subscriber context. AbstractRoutingConnectionFactory is backed by either a map of string identifiers or connection factories. When using string identifiers, these can map agains e.g. Spring bean names that can be resolved using BeanFactoryConnectionFactoryLookup. class MyRoutingConnectionFactory extends AbstractRoutingConnectionFactory { @OverRide protected Mono<Object> determineCurrentLookupKey() { return Mono.subscriberContext().filter(it -> it.hasKey(ROUTING_KEY)).map(it -> it.get(ROUTING_KEY)); } } @bean public void routingConnectionFactory() { MyRoutingConnectionFactory router = new MyRoutingConnectionFactory(); Map<String, ConnectionFactory> factories = new HashMap<>(); ConnectionFactory myDefault = …; ConnectionFactory primary = …; ConnectionFactory secondary = …; factories.put("primary", primary); factories.put("secondary", secondary); router.setTargetConnectionFactories(factories); router.setDefaultTargetConnectionFactory(myDefault); return router; }
Formatting. Changed generics to avoid unchecked casting. Avoided abbreviated variable name. Simplified handling of absent keys at ConnectionFactory lookup.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By copying and resolving the map of ConnectionFactory
instances we force knowledge at creation time of all the connection factories supported. If we moved the configuration from Map
to a Function
instead and do the resolution only when a ConnectionFactory
for a given key is actually requested we could support dynamic creation of connection factories quite naturally.
Apart from that I have a minor question inline and some polishing in a commit.
* @param lookupKey the lookup key object as specified by the user. | ||
* @return the lookup key as needed for matching. | ||
*/ | ||
protected Object resolveSpecifiedLookupKey(Object lookupKey) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So far I don't get the point of this extra translation step. Why wouldn't a user apply this transformation to the map of connection factories outside this class?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This allows for being the lookup key any arbitrary tenant Id: You want to post-process the tenantId to e.g. a bean name.
The design is similar to Spring's AbstractRoutingDataSource
.
That's indented. We do not want to create (register) connection factories at runtime but rather keep these as beans allowing to verify and fail early. |
We now provide an abstract base class for ConnectionFactory routing. Routing keys are typically obtained from a subscriber context. AbstractRoutingConnectionFactory is backed by either a map of string identifiers or connection factories. When using string identifiers, these can map agains e.g. Spring bean names that can be resolved using BeanFactoryConnectionFactoryLookup. class MyRoutingConnectionFactory extends AbstractRoutingConnectionFactory { @OverRide protected Mono<Object> determineCurrentLookupKey() { return Mono.subscriberContext().filter(it -> it.hasKey(ROUTING_KEY)).map(it -> it.get(ROUTING_KEY)); } } @bean public void routingConnectionFactory() { MyRoutingConnectionFactory router = new MyRoutingConnectionFactory(); Map<String, ConnectionFactory> factories = new HashMap<>(); ConnectionFactory myDefault = …; ConnectionFactory primary = …; ConnectionFactory secondary = …; factories.put("primary", primary); factories.put("secondary", secondary); router.setTargetConnectionFactories(factories); router.setDefaultTargetConnectionFactory(myDefault); return router; } Original pull request: #132.
Formatting. Changed generics to avoid unchecked casting. Avoided abbreviated variable name. Simplified handling of absent keys at ConnectionFactory lookup. Original pull request: #132.
That's merged. |
We now provide an abstract base class for
ConnectionFactory
routing. Routing keys are typically obtained from a subscriber context.AbstractRoutingConnectionFactory
is backed by either a map of string identifiers or connection factories. When using string identifiers, these can map against e.g. Spring bean names that can be resolved usingBeanFactoryConnectionFactoryLookup
.Related ticket: #98.