Jhipster:Async sendemail()在一段时间后停止工作

时间:2017-11-27 07:29:38

标签: java spring jhipster

在我的Jhipster应用程序中,Async sendemail()方法在部署之后停止工作。我认为问题出在异步配置中。有人可以帮助我。

以下是我的异步配置代码

  public Executor getAsyncExecutor() {
    log.debug("Creating Async Task Executor");
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(jHipsterProperties.getAsync().getCorePoolSize());
    executor.setMaxPoolSize(jHipsterProperties.getAsync().getMaxPoolSize());
    executor.setQueueCapacity(jHipsterProperties.getAsync().getQueueCapacity());
    executor.setThreadNamePrefix("investhry-Executor-");
    return new ExceptionHandlingAsyncTaskExecutor(executor);
}

发送电子邮件的方法如下所述:

@Async
public void sendEmail(String to, String subject, String content, boolean isMultipart, boolean isHtml) {
    log.debug("Send e-mail[multipart '{}' and html '{}'] to '{}' with subject '{}' and content={}",
        isMultipart, isHtml, to, subject, content);

    // Prepare message using a Spring helper
    MimeMessage mimeMessage = javaMailSender.createMimeMessage();
    try {
        MimeMessageHelper message = new MimeMessageHelper(mimeMessage, isMultipart, CharEncoding.UTF_8);
        message.setTo(to);
        message.setFrom(jHipsterProperties.getMail().getFrom());
        message.setSubject(subject);
        message.setText(content, isHtml);
        javaMailSender.send(mimeMessage);
        log.debug("Sent e-mail to User '{}'", to);
    } catch (Exception e) {
        log.warn("E-mail could not be sent to user '{}'", to, e);
    }
}

1 个答案:

答案 0 :(得分:1)

我已经找到了问题所在。问题不在于您的Async Executer,而在于JAVA Mail API

transport.send()(最终用于发送电子邮件)的超时值是无限的。所有异步线程都处于等待状态。如果您将其更改为某些值,如

       mailprop.put("mail.smtp.timeout", 1000);

您可以查看此解决方案here