2016-09-06 83 views
2

我有一個Web服務,通​​過GET Http方法,用戶請求一個人對象。這個人被髮送到JMS Queue,然後在Spring Integration的幫助下,我把它發送到一個虛假的電子郵件地址(https://papercut.codeplex.com/)。我已經用Spring Integration Java DSL編寫了代碼。我想問問:春季集成電子郵件重新發送異常

  1. 是否有更靈活的方式發送電子郵件?

  2. 如果拋出異常,如何在Spring Integration的幫助下重新發送郵件? (例如5次,如果不發送它,然後異常得到處理,並在程序停止) 這裏是我的代碼:

Web服務

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

回答

2

有沒有更靈活的方式發送電子郵件?

對不起,問題不明確。你有足夠的短代碼來做到這一點。 Mail.outboundAdapter()及其所有流利的API。什麼應該更靈活?

如果拋出異常,在Spring Integration的幫助下如何重新發送郵件?

爲此目的,Spring Integration建議RequestHandlerRetryAdvice

@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! 

看到它的JavaDoc和Reference Manual對此事:和Mail.outboundAdapter()可以作爲配置。

+0

謝謝!你的回答很明確。 .advice(retryAdvice())尤其幫助我很多。 – user3590899