获取errorHandler并写入文件后如何获取原始消息

时间:2016-12-16 13:47:24

标签: email spring-integration dsl

我使用Java DSL 构建了Spring集成服务电子邮件。 此服务必须具有恢复策略才能重试发送电子邮件,但我没有获得成功。 一个简短的故事:应用程序收到Payload和Header并尝试发送到电子邮件服务器。它尝试3次,如果失败,它会创建一个带有Header和Body of message的新文件。

如果无法发送电子邮件,我如何获取原始消息(标题和有效负载)并将信息对放在json文件中?

感谢。

这是我的豆子和服务:

/**
 * ################# 
 * MESSAGE ENDPOINTS
 * #################
 */

@Bean(name = PollerMetadata.DEFAULT_POLLER)
public PollerMetadata poller() {
    return Pollers              
            .fixedRate(NumberUtils.createLong(QUEUE_RATE))
            .maxMessagesPerPoll(NumberUtils.createLong(QUEUE_CAPACITY))
            .errorHandler(e -> LOG.error("Exception : " + e.getMessage()))
            .get();
}


@Bean
public MessageChannel recoveryChannel() {
    return MessageChannels.direct().get();
}


@MessagingGateway 
public static interface MailService {
    @Gateway(requestChannel = "mail.input")
    void sendMail(String body, @Headers Map<String,String> headers);        
}

@Bean
public RetryPolicy retryPolicy() {
    final Map<Class<? extends Throwable>, Boolean> map = 
            new HashMap<Class<? extends Throwable>, Boolean>() {
                {
                    put(MailSendException.class,true);
                    put(RuntimeException.class, true);
                }
                private static final long serialVersionUID = -1L;
            };
    final RetryPolicy ret = new SimpleRetryPolicy(3, map, true);
    return ret;
}

@Bean
public RetryTemplate retryTemplate() {
    final RetryTemplate ret = new RetryTemplate();
    ret.setRetryPolicy(retryPolicy());
    ret.setThrowLastExceptionOnExhausted(false);
    return ret;
}

@Bean
public Advice retryAdvice() {
    final RequestHandlerRetryAdvice advice = new RequestHandlerRetryAdvice();       
    advice.setRetryTemplate(retryTemplate());
    RecoveryCallback<Object> recoveryCallBack = new ErrorMessageSendingRecoverer(recoveryChannel());

    advice.setRecoveryCallback(recoveryCallBack);
    return advice;
}


private MailSendingMessageHandlerSpec mailOutboundAdapter(){
    MailSendingMessageHandlerSpec msmhs = 
            Mail.outboundAdapter(emailServerHost())
            .port(serverPort())
            .credentials(MAIL_USER_NAME, MAIL_PASSWORD)
            .protocol(emailProtocol())
            .javaMailProperties(p -> p
                    .put("mail.debug", "true")
                    .put("mail.smtp.ssl.enable",enableSSL())
                    .put("mail.smtp.connectiontimeout", 5000)
                    .put("mail.smtp.timeout", 5000));
    return msmhs;
}

@Bean
public FileWritingMessageHandler fileOutboundAdapter(){
    FileWritingMessageHandler fwmhs = Files
            .outboundAdapter(new File("logs/errors/"))
            .autoCreateDirectory(true)
            .get();     

    return fwmhs;
}


/**
 * ################ 
 * FLOWS
 * ################
 */


@Bean
public IntegrationFlow smtp(){  

    return IntegrationFlows.from("mail.input")
            .channel(MessageChannels.queue())
            .handle(this.mailOutboundAdapter(), 
                    e -> e.id("smtpOut")
                            .advice(retryAdvice())
                    )
            .get();
}

@Bean 
public IntegrationFlow errorFlow(){
    return IntegrationFlows.from(recoveryChannel())
                            .transform(Transformers.toJson())
                            .enrichHeaders(c -> c.header(FileHeaders.FILENAME, "emailErrors"))
                            .handle(this.fileOutboundAdapter())
                            .get();
}

}

1 个答案:

答案 0 :(得分:1)

错误消息的有效负载为Check out the fiddle 。它有两个属性MessagingExceptioncause

失败的消息是失败点的消息,包含标题和有效负载。