Spring异常的电子邮件重新传递

时间:2016-09-06 19:29:35

标签: email jms spring-integration

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

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

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

  3. 网络服务

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

    Java Confiuration

        @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, "Papercut0@test.com"))
            .handle(Mail.outboundAdapter("127.0.0.1")
                       .credentials("test","test").port(25)
                       .javaMailProperties(p -> p.put("mail.debug", "true")), 
                        e -> e.id("sendMailEndpoint"))
            .get();
    }
    }
    

1 个答案:

答案 0 :(得分:2)

  

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

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

  

如果抛出异常,如何在Spring Integration的帮助下重新传递邮件?

为此,Spring Integration建议RequestHandlerRetryAdviceMail.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