Skip to content

GH-2754: Add channel-based mapping to RouterSpec #2882

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

Merged
merged 1 commit into from
Apr 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ public RouterSpec<K, R> suffix(String suffix) {
* @return the router spec.
* @see AbstractMappingMessageRouter#setChannelMapping(String, String)
*/
public RouterSpec<K, R> channelMapping(K key, final String channelName) {
public RouterSpec<K, R> channelMapping(K key, String channelName) {
Assert.notNull(key, "'key' must not be null");
Assert.hasText(channelName, "'channelName' must not be null");
Assert.hasText(channelName, "'channelName' must not be empty");
if (key instanceof String) {
this.handler.setChannelMapping((String) key, channelName);
}
Expand All @@ -140,6 +140,26 @@ public String getComponentType() {
return _this();
}

/**
* The router mapping configuration based on the provided generic key
* and {@link MessageChannel} bean.
* The {@link MessageChannel} must be instance of {@link NamedComponent}
* for proper target router mapping based on the bean name.
* @param key the key.
* @param channel the {@link MessageChannel} instance to use.
* @return the router spec.
* @see AbstractMappingMessageRouter#setChannelMapping(String, String)
* @since 5.2
*/
public RouterSpec<K, R> channelMapping(K key, final MessageChannel channel) {
Assert.notNull(key, "'key' must not be null");
Assert.notNull(channel, "'channel' must not be null");
Assert.isInstanceOf(NamedComponent.class, channel,
() -> "The routing channel '" + channel + " must be instance of 'NamedComponent'.");
this.mappingProvider.addMapping(key, (NamedComponent) channel);
return _this();
}

/**
* Add a subflow as an alternative to a {@link #channelMapping(Object, String)}.
* {@link #prefix(String)} and {@link #suffix(String)} cannot be used when subflow
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public class RouterTests {
public void testRouter() {
this.beanFactory.containsBean("routeFlow.subFlow#0.channel#0");

int[] payloads = new int[] { 1, 2, 3, 4, 5, 6 };
int[] payloads = { 1, 2, 3, 4, 5, 6 };

for (int payload : payloads) {
this.routerInput.send(new GenericMessage<>(payload));
Expand Down Expand Up @@ -124,7 +124,7 @@ public void testRouterWithTwoSubflows() {
@SuppressWarnings("unchecked")
List<Integer> results = (List<Integer>) payload;

assertThat(results.toArray(new Integer[results.size()])).isEqualTo(new Integer[] { 3, 4, 9, 8, 15, 12 });
assertThat(results).containsExactly(3, 4, 9, 8, 15, 12);
}

@Autowired
Expand Down Expand Up @@ -606,7 +606,7 @@ public QueueChannel evenChannel() {
public IntegrationFlow routeFlow() {
return IntegrationFlows.from("routerInput")
.<Integer, Boolean>route(p -> p % 2 == 0,
m -> m.channelMapping(true, "evenChannel")
m -> m.channelMapping(true, evenChannel())
.subFlowMapping(false, f ->
f.<Integer>handle((p, h) -> p * 3))
.defaultOutputToParentFlow())
Expand Down Expand Up @@ -929,5 +929,4 @@ else if (message.getPayload().equals("bar")) {

}


}