我有一個Web服務,通過GET Http方法,用戶請求一個人對象。這個人被髮送到JMS Queue,然後在Spring Integration的幫助下,我把它發送到一個虛假的電子郵件地址(https://papercut.codeplex.com/)。我已經用Spring Integration Java DSL編寫了代碼。我想問問:春季集成電子郵件重新發送異常
是否有更靈活的方式發送電子郵件?
如果拋出異常,如何在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();
}
}
謝謝!你的回答很明確。 .advice(retryAdvice())尤其幫助我很多。 – user3590899