遵循我爲使用Spring 3 TaskExecutor發送異步郵件所做的代碼。代碼工作正常,我只想知道是否有任何郵件因任何原因無法發送,那麼它必須重新添加到隊列中,並且必須重試發送。如何在Spring TaskExecutor中重試
我已經谷歌搜索,但沒有得到重試政策的信息。
幫助將高度讚賞!
@Service
public class AsyncMailSender implements MailSender {
/* Logger for Search-Controller Class */
public static final Logger LOGGER = Logger.getLogger(AsyncMailSender.class);
@Autowired
private MailSender mailSender;
@Autowired
private TaskExecutor taskExecutor;
@Autowired
private VelocityEngine velocityEngine;
public void send(SimpleMailMessage simpleMessage) throws MailException {
Map<String, Object> modelMap = new HashMap<String, Object>();
modelMap.put("user", "Manoj");
simpleMessage.setText(VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "templates/email.vm", modelMap));
taskExecutor.execute(new AsyncMailTask(simpleMessage));
}
public void send(SimpleMailMessage[] simpleMessages) throws MailException {
for (SimpleMailMessage message : simpleMessages) {
send(message);
}
}
private class AsyncMailTask implements Runnable {
private SimpleMailMessage message;
private AsyncMailTask(SimpleMailMessage message) {
this.message = message;
}
public void run() {
LOGGER.info("Sending Emails" + message.getSubject());
mailSender.send(message);
}
}
}
感謝這麼好的信息,我也去了SPR。想知道是否有任何方法來配置重試次數,所以如果發生相同的錯誤,那麼在某些數字之後,Spring將不會再次執行該任務。 –
這正是SPR的意義,它還沒有解決。我不認爲有任何內置的方式來配置它,你可能需要編寫你的應用程序來處理這些事情。 – Swapnil