Skip to content

Commit 063759d

Browse files
artembilangaryrussell
authored andcommitted
GH-2754: Add channel-based mapping to RouterSpec
Fixes #2754 For better end-user experience when we have `@Bean` declared for a channel it is good to have a `MessageChannel` based `RouterSpec.channelMapping` for possible traceability and code navigation in the IDE
1 parent ad7ccb3 commit 063759d

File tree

2 files changed

+25
-6
lines changed
  • spring-integration-core/src

2 files changed

+25
-6
lines changed

spring-integration-core/src/main/java/org/springframework/integration/dsl/RouterSpec.java

+22-2
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,9 @@ public RouterSpec<K, R> suffix(String suffix) {
116116
* @return the router spec.
117117
* @see AbstractMappingMessageRouter#setChannelMapping(String, String)
118118
*/
119-
public RouterSpec<K, R> channelMapping(K key, final String channelName) {
119+
public RouterSpec<K, R> channelMapping(K key, String channelName) {
120120
Assert.notNull(key, "'key' must not be null");
121-
Assert.hasText(channelName, "'channelName' must not be null");
121+
Assert.hasText(channelName, "'channelName' must not be empty");
122122
if (key instanceof String) {
123123
this.handler.setChannelMapping((String) key, channelName);
124124
}
@@ -140,6 +140,26 @@ public String getComponentType() {
140140
return _this();
141141
}
142142

143+
/**
144+
* The router mapping configuration based on the provided generic key
145+
* and {@link MessageChannel} bean.
146+
* The {@link MessageChannel} must be instance of {@link NamedComponent}
147+
* for proper target router mapping based on the bean name.
148+
* @param key the key.
149+
* @param channel the {@link MessageChannel} instance to use.
150+
* @return the router spec.
151+
* @see AbstractMappingMessageRouter#setChannelMapping(String, String)
152+
* @since 5.2
153+
*/
154+
public RouterSpec<K, R> channelMapping(K key, final MessageChannel channel) {
155+
Assert.notNull(key, "'key' must not be null");
156+
Assert.notNull(channel, "'channel' must not be null");
157+
Assert.isInstanceOf(NamedComponent.class, channel,
158+
() -> "The routing channel '" + channel + " must be instance of 'NamedComponent'.");
159+
this.mappingProvider.addMapping(key, (NamedComponent) channel);
160+
return _this();
161+
}
162+
143163
/**
144164
* Add a subflow as an alternative to a {@link #channelMapping(Object, String)}.
145165
* {@link #prefix(String)} and {@link #suffix(String)} cannot be used when subflow

spring-integration-core/src/test/java/org/springframework/integration/dsl/routers/RouterTests.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public class RouterTests {
8989
public void testRouter() {
9090
this.beanFactory.containsBean("routeFlow.subFlow#0.channel#0");
9191

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

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

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

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

930930
}
931931

932-
933932
}

0 commit comments

Comments
 (0)