DSL: Fix RouterSpec for several subflows

The root of the issue is the order of bean registration in the beanFactory.
In this case the first subflows is registered correctly, but the next one has been registered after `RouterSubFlowMappingProvider`.
In this case the input `channel` of the next subflow hasn't been registered yet, hence `NPE` in the `this.router.setChannelMapping`
from `@PostConstruct` of `RouterSubFlowMappingProvider`.

Move the `RouterSubFlowMappingProvider` to end of `componentsToRegister` collection to give a chance to register all subflows before the `RouterSubFlowMappingProvider`.
This commit is contained in:
Artem Bilan
2014-11-01 13:11:17 +02:00
committed by Gary Russell
parent e144feb1b3
commit 13ea2d9111
2 changed files with 36 additions and 1 deletions

View File

@@ -90,7 +90,6 @@ public final class RouterSpec<R extends AbstractMappingMessageRouter> extends Ab
if (this.mappingProvider == null) {
this.mappingProvider = new RouterSubFlowMappingProvider(this.target);
this.subFlows.add(this.mappingProvider);
}
this.mappingProvider.addMapping(key, channel);
return _this();
@@ -98,6 +97,9 @@ public final class RouterSpec<R extends AbstractMappingMessageRouter> extends Ab
@Override
public Collection<Object> getComponentsToRegister() {
if (this.mappingProvider != null) {
this.subFlows.add(this.mappingProvider);
}
return this.subFlows;
}

View File

@@ -18,6 +18,7 @@ package org.springframework.integration.dsl.test.flows;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.instanceOf;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
@@ -563,7 +564,27 @@ public class IntegrationFlowTests {
assertNotNull(receive);
assertEquals(payloads[i * 2 + 1], receive.getPayload());
}
}
@Autowired
@Qualifier("routerTwoSubFlows.input")
private MessageChannel routerTwoSubFlowsInput;
@Autowired
@Qualifier("routerTwoSubFlowsOutput")
private PollableChannel routerTwoSubFlowsOutput;
@Test
public void testRouterWithTwoSubflows() {
this.routerTwoSubFlowsInput.send(new GenericMessage<Object>(Arrays.asList(1, 2, 3, 4, 5, 6)));
Message<?> receive = this.routerTwoSubFlowsOutput.receive(5000);
assertNotNull(receive);
Object payload = receive.getPayload();
assertThat(payload, instanceOf(List.class));
@SuppressWarnings("unchecked")
List<Integer> results = (List<Integer>) payload;
assertArrayEquals(new Integer[] {3, 4, 9, 8, 15, 12}, results.toArray(new Integer[results.size()]));
}
@Test
@@ -1191,6 +1212,18 @@ public class IntegrationFlowTests {
.get();
}
@Bean
public IntegrationFlow routerTwoSubFlows() {
return f -> f
.split()
.<Integer, Boolean>route(p -> p % 2 == 0, m -> m
.subFlowMapping("true", sf -> sf.<Integer>handle((p, h) -> p * 2))
.subFlowMapping("false", sf -> sf.<Integer>handle((p, h) -> p * 3)))
.aggregate()
.channel(c -> c.queue("routerTwoSubFlowsOutput"));
}
@Bean
public RoutingTestBean routingTestBean() {
return new RoutingTestBean();