Skip to content

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

Closed
wants to merge 3 commits into from
Closed

Conversation

mp911de
Copy link
Member

@mp911de mp911de commented May 28, 2019

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 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;
}

Related ticket: #98.

mp911de added 2 commits May 28, 2019 13:30
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;
}
@mp911de mp911de added this to the 1.0 M3 milestone May 28, 2019
@mp911de mp911de requested a review from schauder May 28, 2019 13:16
@mp911de mp911de changed the title Issue/gh 98 Add support for AbstractRoutingConnectionFactory May 28, 2019
Formatting.
Changed generics to avoid unchecked casting.
Avoided abbreviated variable name.
Simplified handling of absent keys at ConnectionFactory lookup.
Copy link
Contributor

@schauder schauder left a 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) {
Copy link
Contributor

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?

Copy link
Member Author

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.

@mp911de
Copy link
Member Author

mp911de commented Jun 4, 2019

By copying and resolving the map of ConnectionFactory instances we force knowledge at creation time of all the connection factories supported.

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.

schauder pushed a commit that referenced this pull request Jun 4, 2019
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.
schauder added a commit that referenced this pull request Jun 4, 2019
Formatting.
Changed generics to avoid unchecked casting.
Avoided abbreviated variable name.
Simplified handling of absent keys at ConnectionFactory lookup.

Original pull request: #132.
@schauder schauder closed this Jun 4, 2019
@schauder schauder deleted the issue/gh-98 branch June 4, 2019 09:54
@schauder
Copy link
Contributor

schauder commented Jun 4, 2019

That's merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants