diff --git a/spring-integration-core/src/main/java/org/springframework/integration/dsl/RouterSpec.java b/spring-integration-core/src/main/java/org/springframework/integration/dsl/RouterSpec.java index cfd2aaafed3..6b61dd63462 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/dsl/RouterSpec.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/dsl/RouterSpec.java @@ -116,9 +116,9 @@ public RouterSpec suffix(String suffix) { * @return the router spec. * @see AbstractMappingMessageRouter#setChannelMapping(String, String) */ - public RouterSpec channelMapping(K key, final String channelName) { + public RouterSpec 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); } @@ -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 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 diff --git a/spring-integration-core/src/test/java/org/springframework/integration/dsl/routers/RouterTests.java b/spring-integration-core/src/test/java/org/springframework/integration/dsl/routers/RouterTests.java index 32ffd476fb5..e2e55c7680c 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/dsl/routers/RouterTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/dsl/routers/RouterTests.java @@ -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)); @@ -124,7 +124,7 @@ public void testRouterWithTwoSubflows() { @SuppressWarnings("unchecked") List results = (List) 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 @@ -606,7 +606,7 @@ public QueueChannel evenChannel() { public IntegrationFlow routeFlow() { return IntegrationFlows.from("routerInput") .route(p -> p % 2 == 0, - m -> m.channelMapping(true, "evenChannel") + m -> m.channelMapping(true, evenChannel()) .subFlowMapping(false, f -> f.handle((p, h) -> p * 3)) .defaultOutputToParentFlow()) @@ -929,5 +929,4 @@ else if (message.getPayload().equals("bar")) { } - }