I have following failing test. The question is why reply in case of error on one of splitted "sub-messages" is error only, without result for the other, successfully-handled, sub-message (as expected in test)? Is there a modification to this code to achieve result expected in test?
@RunWith(SpringRunner.class)
public class ErrorHandlingTests {
@Autowired
StringsService stringsService;
interface StringsService {
@Nonnull
List<String> process(@Nonnull List<String> data);
}
@EnableIntegration
@Configuration
static class Config {
@Bean
IntegrationFlow errorHandler() {
return IntegrationFlows.from("errorChannel")
.<MessagingException>handle((ex, h) -> "Failure for " + ex.getFailedMessage().getPayload())
.get();
}
@Bean
IntegrationFlow errorsHandlingFlow2() {
AtomicInteger incomingCorrelationId = new AtomicInteger();
return IntegrationFlows.from(StringsService.class, gws -> gws.errorChannel("errorChannel"))
.split(new AbstractMessageSplitter() {
@Override
protected Object splitMessage(Message<?> message) {
List<String> strings = (List<String>) message.getPayload();
int id = incomingCorrelationId.get();
return strings
.stream()
.map(r -> MessageBuilder
.withPayload(r)
.setHeader(IntegrationMessageHeaderAccessor.CORRELATION_ID, id)
.setHeader(IntegrationMessageHeaderAccessor.SEQUENCE_SIZE, strings.size())
.build())
.collect(Collectors.toList());
}
})
.transform(new AbstractPayloadTransformer<String, String>() {
@Override
protected String transformPayload(String s) {
if (s.contains("oops"))
throw new IllegalArgumentException("Bad value");
return "R: " + s;
}
})
.aggregate(a -> a.outputProcessor(new AbstractAggregatingMessageGroupProcessor() {
@Override
protected Object aggregatePayloads(MessageGroup group, Map<String, Object> defaultHeaders) {
return group.getMessages()
.stream()
.map(m -> (String) m.getPayload())
.collect(Collectors.toList());
}
}))
.get();
}
}
@Test
public void testErrorHandlingInFlow2() {
assertEquals(Arrays.asList("R: a", "R: b"), stringsService.process(Arrays.asList("a", "b")));
assertEquals(Arrays.asList("R: a", "Failure for oops"), stringsService.process(Arrays.asList("a", "oops")));
}
}
Updated version, working, with applied advice.
@RunWith(SpringRunner.class)
public class ErrorHandlingTests2 {
interface StringsService {
@Nonnull
List<String> process(@Nonnull List<String> data);
}
@Autowired
StringsService stringsService;
@EnableIntegration
@Configuration
static class Config {
@Bean
IntegrationFlow errorHandler() {
return IntegrationFlows.from("errorChannel")
.<MessagingException>handle((ex, h) -> "Failure for " + ex.getFailedMessage().getPayload())
.get();
}
@Bean
IntegrationFlow errorsHandlingFlow2() {
return IntegrationFlows.from(StringsService.class, gws -> gws.errorChannel("errorChannel"))
.split(new AbstractMessageSplitter() {
@Override
protected Object splitMessage(Message<?> message) {
List<String> strings = (List<String>) message.getPayload();
return strings
.stream()
.map(r -> MessageBuilder
.withPayload(r)
.build())
.collect(Collectors.toList());
}
})
.transform(new AbstractPayloadTransformer<String, String>() {
@Override
protected String transformPayload(String s) {
if (s.contains("oops"))
throw new IllegalArgumentException("Bad value");
return "R: " + s;
}
}, c -> c.advice(advice()))
.aggregate(a -> a.outputProcessor(new AbstractAggregatingMessageGroupProcessor() {
@Override
protected Object aggregatePayloads(MessageGroup group, Map<String, Object> defaultHeaders) {
return group.getMessages()
.stream()
.map(m -> (String) m.getPayload())
.collect(Collectors.toList());
}
}))
.get();
}
@Bean
ExpressionEvaluatingRequestHandlerAdvice advice() {
ExpressionEvaluatingRequestHandlerAdvice advice = new ExpressionEvaluatingRequestHandlerAdvice();
advice.setReturnFailureExpressionResult(true);
advice.setOnFailureExpression(
new FunctionExpression<Message<?>>(s ->
MessageBuilder
.withPayload("Failure for " + s.getPayload())
.copyHeaders(s.getHeaders()).build())
);
return advice;
}
}
@Test
public void testErrorHandlingInFlow2() {
assertEquals(Arrays.asList("R: a", "R: b"), stringsService.process(Arrays.asList("a", "b")));
assertEquals(Arrays.asList("R: a", "Failure for oops", "R: b"), stringsService.process(Arrays.asList("a", "oops", "b")));
}
}
question from:
https://stackoverflow.com/questions/65887787/spring-integration-unexpected-behavior-on-error-handling-in-splitter-aggregator 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…