gpt4 book ai didi

java - 如何在 Spring TaskExecutor 中进行重试

转载 作者:行者123 更新时间:2023-12-01 18:55:35 24 4
gpt4 key购买 nike

遵循我使用 Spring 3 TaskExecutor 进行异步邮件发送的代码。代码工作正常,我只是想知道是否有任何邮件因任何原因未能发送,那么必须将其再次添加到队列中并重试发送。

我已在 Google 上搜索,但没有获得有关重试政策的信息。

我们将非常感谢您的帮助!!!

@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);
}
}
}

最佳答案

据我所知,目前 Spring 3 TaskExecutor 无法配置重试,有一个SPR与此相关。话虽如此,我认为如果抛出任何异常,Spring 会自动重试该方法。如果异常不断重复出现,这可能会给您带来问题(在资源消耗方面)。

关于java - 如何在 Spring TaskExecutor 中进行重试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14111069/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com