如何处理InfegrationFlows上的异常?

问题描述

我通过Spring Cloud DataFlow连接了此流程。一个微服务从队列中获取电子邮件,然后发送到另一个应该发送电子邮件的微服务。

我希望能够捕获失败的电子邮件,并在.transform()和异常消息之前保留原始消息。

如何做到?

@Bean
public IntegrationFlow sendMailFlow() {
    return IntegrationFlows.from(Sink.INPUT)
            .transform(Transformers.converter(converter))
            .handle(Mail.outboundAdapter(EmailSinkApplication.this.props.getServer())
                            .port(EmailSinkApplication.this.props.getPort())
                            .credentials(EmailSinkApplication.this.props.getUser(),EmailSinkApplication.this.props.getpass())
                            .protocol(EmailSinkApplication.this.props.getProto())
                            .javaMailProperties(p -> {
                                p.put("mail.smtp.starttls.enable","true");
                                p.put("mail.smtp.auth","true");
                            }),e -> e.id("sendMailEndpoint"))
            .get();
}

解决方法

我建议您实现ChannelInterceptor并将逻辑添加到afterSendCompletion()中。在该拦截器bean上使用@GlobalChannelInterceptor,将patterns指定为Sink.INPUT。因此,您将在该try..catch频道的整个流程中得到一个Sink.INPUT