我有两个线程,线程中的每个方法都会引发异常。如何获取每个线程中引发的所有错误?在此代码中,错误通道仅捕获其中一个错误。基本上我的目标是捕获所有错误并将它们发送给调用者(休息控制器)。任何帮助将不胜感激。谢谢。
集成.java
public IntegrationFlow provisionUserFlow() {
return IntegrationFlows.from("input.channel")
.publishSubscribeChannel(Executors.newCachedThreadPool(),
s -> s
.subscribe(f -> f.handle(provisionerA, "provision"))
.subscribe(f -> f.handle(provisionerB, "provision"))
.get();
}
@ServiceActivator( inputChannel = "errorChannel", outputChannel = "replyChannel")
public boolean processErrors(Exception message) throws RuntimeException{
System.out.println("Message" + message.getMessage());
System.out.println ("******************************");
throw new RuntimeException(message.getMessage());
}
MGateway.java
@MessagingGateway(errorChannel = "errorChannel")
public interface MGateway {
@Gateway(requestChannel = "input.channel", replyChannel = "replyChannel")
boolean invokeProvisioner(User user);
}
解决方案
@Bean
public IntegrationFlow provisionUserFlow() {
return
IntegrationFlows.from("input.channel")
.publishSubscribeChannel(Executors.newCachedThreadPool(),
s -> s.applySequence(true)
.subscribe(f -> f.enrichHeaders(e -> e.header(MessageHeaders.ERROR_CHANNEL, "errorChannel", true))
.handle(provisionerA, "provision")
.channel("aggregatorChannel")
)
.subscribe(f -> f.enrichHeaders(e -> e.header(MessageHeaders.ERROR_CHANNEL, "errorChannel", true))
.handle(provisionerB, "provision")
.channel("aggregatorChannel"))
)
.get();
}
@Bean
public IntegrationFlow aggregateFlow() {
return IntegrationFlows.from("aggregatorChannel")
.channel( aggregatorChannel)
.aggregate( a -> a.processor( collect, "aggregatingMethod"))
.get();
}
@Transformer( inputChannel = "errorChannel", outputChannel = "aggregatorChannel")
public Message<?> errorChannelHandler(ErrorMessage errorMessage) throws RuntimeException {
Message<?> failedMessage = ((MessagingException) errorMessage.getPayload())
.getFailedMessage();
Exception exception = (Exception) errorMessage.getPayload();
return MessageBuilder.withPayload( exception.getMessage())
.copyHeadersIfAbsent( failedMessage.getHeaders() )
.build();
}