Spring中的自定义电子邮件服务 - 需要帮助改进

时间:2010-12-30 13:50:49

标签: java multithreading spring email concurrency

我有以下代码可以正常工作150个连续的线程。任何事情,服务器都没有反应。请帮助我提供有关如何正确集成多线程的建议。我在考虑ThreadPoolExecutor和ArrayBlockingQueue,但我想我是否已经有我可以使用的库或其他更适合此类的解决方案:

@Transactional
public class EmailServiceImpl implements EmailService{

private static final Logger log = Logger
        .getLogger(MailNotificationServiceImpl.class);
private JavaMailSender mailSender;
private MessageSource messageSource;
private FreeMarkerConfigurer freemarkerConfig;

public void sendEmail(final String subject, final String recipient, final ModelMap mm, final String emailTemplate, final String from) {

   try {
        MimeMessagePreparator preparator = new MimeMessagePreparator() {
            public void prepare(MimeMessage mimeMessage) throws Exception {
                MimeMessageHelper message = new MimeMeaassageHelper(

                mimeMessage);
                message.setFrom(from);
                message.setTo(recipient);
                message.setSubject(subject);

                //freemarker integration
                Template textTemplate = freemarkerConfig.getConfiguration().getTemplate(emailTemplate); // "/WEB-INF/email/*.ftl"
                log.debug(emailTemplate);
                final StringWriter textWriter = new StringWriter();
                textTemplate.process(mm, textWriter);

                message.setText(textWriter.toString(), true);
            }
        };
       Thread thread = new SendMail(preparator);
       thread.start();

    } catch (Exception e) {
        log.error(e);
    }          
}


class SendMail extends Thread {
    MimeMessagePreparator preparator;

    SendMail(MimeMessagePreparator preparator) {
        this.preparator = preparator;
    }

    public void run() {
        log.debug("About to send email:");
        mailSender.send(preparator);
        log.debug("Email send.");
    }
}



public void sendEmail(final String subject, final String recipient, final ModelMap mm, final String emailTemplate, final boolean flag, final String from) {
    if(flag){
        sendEmail(subject, recipient, mm, emailTemplate, from);
    }
}

public void sendEmail(final String subject, final String recipient, final ModelMap mm, final String emailTemplate, final boolean flag) {
    if(flag){
        sendEmail(subject, recipient, mm, emailTemplate, "support@domain.com");
    }
}    

public void sendEmail(final String subject, final String recipient, final ModelMap mm, final String emailTemplate) {
        sendEmail(subject, recipient, mm, emailTemplate, "support@domain.com");
}    

public void setMailSender(JavaMailSender mailSender) {
    this.mailSender = mailSender;
}

private String gm(String messageName) {
    return messageSource.getMessage(messageName, null, null);
}

public void setMessageSource(MessageSource messageSource) {
    this.messageSource = messageSource;
}

public void setFreemarkerConfig(FreeMarkerConfigurer freemarkerConfig) {
    this.freemarkerConfig = freemarkerConfig;
}

}

1 个答案:

答案 0 :(得分:3)

您应该考虑使用ExecutorService。在所有处理器达到峰值并且操作系统花费更多时间切换线程上下文之前,您可以通过更多线程实现这么多的并发性。

如果将在任何给定时间运行的线程数限制为甚至只有50,您应该会看到良好的整体吞吐量。

你可以这样试试:

ExecutorService executor = Executors.newFixedThreadPool(50);

public void sendEmail(final String subject, final String recipient, final ModelMap mm, final String emailTemplate, final String from) {
 MimeMessagePreparator preparator = new MimeMessagePreparator() {
       // rest of your mail building logic
      };

  executor.submit(preparator);

}