gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 06:17:24 25 4
gpt4 key购买 nike

我有下面的代码在 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;
}

}

最佳答案

您应该明确考虑使用 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);

}

关于java - Spring 中的自定义电子邮件服务 - 需要帮助改进,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4563107/

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