email - Spring Integration 异常时重新发送电子邮件

标签 email jms spring-integration

我有一个 Web 服务,用户通过 GET Http 方法请求 person 对象。这个人被发送到 JMS 队列,然后在 Spring Integration 的帮助下,我将其发送到一个假电子邮件地址 ( https://papercut.codeplex.com/ )。我已经使用 Spring Integration Java DSL 编写了代码。我想问一下:

  1. 是否有更灵活的方式发送电子邮件?

  2. 如果抛出异常,如何借助 Spring Integration 重新投递邮件? (例如 5 次,如果未发送,则处理异常并停止程序) 这是我的代码:

网络服务

     public Person findById(Integer id) {
        Person person = jpaPersonRepository.findOne(id);
        jmsTemplate.convertAndSend("testQueue", person);
        return jpaPersonRepository.findOne(id);
     }

Java 配置

    @Configuration
    @EnableIntegration
    @ComponentScan
    public class JavaConfig {
        private static final String DEFAULT_BROKER_URL = "tcp://localhost:61616";
        private static final String DEFAULT_QUEUE = "testQueue";

    @Bean
    public ActiveMQConnectionFactory connectionFactory() {
         ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
         connectionFactory.setBrokerURL(DEFAULT_BROKER_URL);
         return connectionFactory;
    }

    @Bean
    public JmsTemplate jmsTemplate() {
        JmsTemplate template = new JmsTemplate();
        template.setConnectionFactory(this.connectionFactory());
        template.setDefaultDestinationName(DEFAULT_QUEUE);
        return template;
    }

    @Bean
    public DefaultMessageListenerContainer defaultMessageListenerContainer() {
        DefaultMessageListenerContainer defaultMessageListenerContainer = new DefaultMessageListenerContainer();
        defaultMessageListenerContainer.setDestinationName(DEFAULT_QUEUE);
        defaultMessageListenerContainer.setConnectionFactory(this.connectionFactory());
        return defaultMessageListenerContainer;
}

    @Bean(name="inputChannel")
    public DirectChannel directChannel() {
        return new DirectChannel();
    }

   @Bean
   public IntegrationFlow orders() {
        return IntegrationFlows
        .from(Jms.messageDrivenChannelAdapter(defaultMessageListenerContainer()))
            .transform(new ObjectToStringTransformer())
            .enrichHeaders(p -> p.header(MailHeaders.TO, "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8fdfeeffeafdecfafbbfcffbeafcfba1ece0e2" rel="noreferrer noopener nofollow">[email protected]</a>"))
        .handle(Mail.outboundAdapter("127.0.0.1")
                   .credentials("test","test").port(25)
                   .javaMailProperties(p -> p.put("mail.debug", "true")), 
                    e -> e.id("sendMailEndpoint"))
        .get();
}
}

最佳答案

Is there a more flexible way to send the email message?

抱歉,问题不清楚。你有足够的短代码来做到这一点。 Mail.outboundAdapter() 及其所有流畅的 API。什么应该更灵活?

If an exception is thrown, how can the mail be redelivered with the help of Spring Integration?

为此,Spring Integration 建议使用 RequestHandlerRetryAdvice。并且 Mail.outboundAdapter() 可以配置为:

@Bean
public Advice retryAdvice() {
    RequestHandlerRetryAdvice advice = new RequestHandlerRetryAdvice();
    RetryTemplate retryTemplate = new RetryTemplate();
    SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
    retryPolicy.setMaxAttempts(5);
    retryTemplate.setRetryPolicy(retryPolicy);
    advice.setRetryTemplate(retryTemplate);
    advice.setRecoveryCallback(new ErrorMessageSendingRecoverer(emailErrorChannel()));
    return advice;
} 

...

.handle(Mail.outboundAdapter("127.0.0.1")
               .credentials("test","test").port(25)
               .javaMailProperties(p -> p.put("mail.debug", "true")), 
                e -> e.id("sendMailEndpoint")
                      .advice(retryAdvice())) // HERE IS THE TRICK!

查看其 JavaDocs 和 Reference Manual关于此事。

关于email - Spring Integration 异常时重新发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39356483/

相关文章:

java - 远程 JMS 队列的客户端

java - 当 Body 不可分配给类时如何调试 JMS?

java - ActiveMQ中的并发消息消费

java - 如何在 Spring Mail Integration 中启用 IMAPFolder 读写模式

ibm-mq - Websphere 7 + Websphere MQ 7.X + Spring Integration + JMS - 消息监听器停止从队列读取消息

email - Laravel 邮件队列无限循环异常

javascript - 如何使用 JavaScript 发送电子邮件?

python - 如何在 Python 3 中阅读电子邮件内容

email - 需要帮助,使我对如何实现电子邮件提醒系统自动化的想法iti之以鼻

java - 在运行时配置生存时间属性