[英]Capturing Errors on Spring Integration DSL
我们有一个连接到GCP Pubsub的Spring Integration DSL管道,并且“工作”:使用Function
实现和.handle()
的集合,按照管道中的定义接收和处理数据。
我们遇到的问题(以及为什么在引号中使用“ work”的原因)是,在某些处理程序中,当在伴随数据库中找不到某些数据时,我们引发IllegalStateException
,这迫使数据被重新处理(沿着这样,另一项服务可能会完成伴随数据库,然后该功能将开始工作)。 永远不会在任何地方显示此异常。
我们试图捕获errorHandler
的内容,但实际上找不到以编程方式(没有XML)进行操作的正确方法。
我们的Function
具有以下内容:
Record record = recordRepository.findById(incomingData).orElseThrow(() -> new IllegalStateException("Missing information: " + incomingData));
此IllegalStateException
是未在日志中的任何位置出现的异常。
另外,也许值得一提的是,我们将频道定义为
@Bean
public DirectChannel cardInputChannel() {
return new DirectChannel();
}
@Bean
public PubSubInboundChannelAdapter cardChannelAdapter(
@Qualifier("cardInputChannel") MessageChannel inputChannel,
PubSubTemplate pubSubTemplate) {
PubSubInboundChannelAdapter adapter = new PubSubInboundChannelAdapter(pubSubTemplate, SUBSCRIPTION_NAME);
adapter.setOutputChannel(inputChannel);
adapter.setAckMode(AckMode.AUTO);
adapter.setPayloadType(CardDto.class);
return adapter;
}
我对适配器不熟悉,但是我只是看了一下代码,看起来他们只是在打消信息而没有记录任何内容。
您可以向处理程序的端点添加Advice
以捕获并记录异常
.handle(..., e -> e.advice(exceptionLoggingAdvice)
@Bean
public MethodInterceptor exceptionLoggingAdvice() {
return invocation -> {
try {
return invocation.proceed();
}
catch (Exception thrown) {
// log it
throw thrown;
}
}
}
编辑
@SpringBootApplication
public class So57224614Application {
public static void main(String[] args) {
SpringApplication.run(So57224614Application.class, args);
}
@Bean
public IntegrationFlow flow(MethodInterceptor myAdvice) {
return IntegrationFlows.from(() -> "foo", endpoint -> endpoint.poller(Pollers.fixedDelay(5000)))
.handle("crasher", "crash", endpoint -> endpoint.advice(myAdvice))
.get();
}
@Bean
public MethodInterceptor myAdvice() {
return invocation -> {
try {
return invocation.proceed();
}
catch (Exception e) {
System.out.println("Failed with " + e.getMessage());
throw e;
}
};
}
}
@Component
class Crasher {
public void crash(Message<?> msg) {
throw new RuntimeException("test");
}
}
和
Failed with nested exception is java.lang.RuntimeException: test
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.