java - 捕获 Spring Integration DSL 上的错误

标签 java spring error-handling google-cloud-platform spring-integration

我们有一个连接到 GCP Pubsub 的 Spring Integration DSL 管道,并且一切正常:使用 Function 实现和 的集合,按照管道中的定义接收和处理数据.handle().

我们遇到的问题(以及为什么我在引号中使用“work”)是,在某些处理程序中,当在配套数据库中找不到某些数据时,我们会引发 IllegalStateException,这迫使数据被重新处理(在此过程中,另一个服务可能会完成配套数据库,然后功能将开始工作)。此异常从未在任何地方显示。

我们 try catch errorHandler 的内容,但我们确实找不到以编程方式执行此操作的正确方法(无 XML)。

我们的函数有这样的东西:

Record record = recordRepository.findById(incomingData).orElseThrow(() -> new IllegalStateException("Missing information: " + incomingData));

IllegalStateException 没有出现在日志中的任何位置。

此外,也许值得一提的是,我们的 channel 定义为

    @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;
    }

最佳答案

我对适配器不熟悉,但我只是查看了代码,看起来他们只是拒绝消息并且不记录任何内容。

您可以将建议添加到处理程序的端点以捕获并记录异常

    .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

关于java - 捕获 Spring Integration DSL 上的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57224614/

相关文章:

java - MultipartFile 不在 "@RequestParam Map<String, Object> requestParams"变量中

eclipse - 将 Tomcat 服务器与 Hibernate 和 Springsource Tool Suite 一起使用?

php - 在发送请求之前,PHP是否针对WSDL验证请求?

r - 出现警告时如何停止功能?

java.lang.AbstractMethodError 在 createQuery 期间

java - Spring AspectJ loadtimeweaving 未调用

spring - 如何记录 Spring 加载的属性?

error-handling - 绕过Play的HttpErrorHandler处理4xx错误

java - Stream.of 和 IntStream.range 有什么区别?

java - 使用 .io TLD 作为 Java 包的优点和缺点?