通过弹出邮件提高发送批量邮件的性能 [英] Improve performance on sending bulk emails through spring-mail

查看:199
本文介绍了通过弹出邮件提高发送批量邮件的性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个独立弹簧的应用程序,它使用简单的弹簧电子邮件代码如下,消息使用从map迭代的值构造。



我已经提出了一些问题这里,但我需要一些具体的建议。以下是我的代码

  for(Map.Entry< String,List< values>>条目:testMap 
。 entrySet()){
String key = entry.getKey();
StringBuilder htmlBuilder = new StringBuilder();
列表<模型> valueList = entry.getValue();
for(Model value:valueList){
htmlBuilder.append('在消息中列出值');
}
mail.sendMail(msgFrom,body); //在另一个类中调用sendMail函数
}

发送邮件的代码:

  MimeMessage email = mailSender.createMimeMessage(); 
MimeMessageHelper helper = new MimeMessageHelper(email,true);
helper.setFrom(new InternetAddress(from));
helper.setTo(new InternetAddress(to));
helper.setText(msg,true);
helper.addInline(identifier1234,res);
mailSender.send(email);

发送邮件需要3到4秒钟。我有大量的用户列表,每天大约40万个被发送



我做错了什么或任何其他方法来固定这个过程。感谢您的时间和帮助:

解决方案


$ b

IM HO IM,,,。。。。。。。。。。。。。。。。。。。。。。。您可以通过使用批量发送来改善它。



Spring MailSender 接口本身支持发送一组消息,而不是一个单一的,所以你没有做明确地处理JavaMail会话。您可以简单地修改实际发送邮件的课程

  int batchSize = 16; //例如,调整它需要
MimeMessage [] messages = new MimeMessage [batchSize];
int messageIndex = 0;

public void sendMail(String msgFrom,String body){
//准备MimeMessage
消息[messageIndex ++] =电子邮件;
if(messagesIndex == batchSize){
mailSender.send(messages);
messageIndex = 0;
}

public void sendLastMails(){
if(messageIndex> 0){
MimeMessage [] lastMessages = new MimeMessage [messageIndex]; (int i = 0; i< messageIndex; i ++)
{
lastMessages [i] = messages [i];
}
mailSender.send(lastMessages);
}

编辑:



可以在几个地方调用 sendLastMails 方法。首先,必须在单例bean的destroy方法中调用,以确保在应用程序关闭时没有消息被遗忘。如果发送邮件的类是一个单例bean,则足以声明bean 的destroy方法是 sendLastMail ,或者调用它。 / p>

然后根据您自己的业务规则,可能会在发送邮件的批量之后调用该规则。典型用法:在你的例子中,你有 testMap 。您应该以这种方式重写:

  for(Map.Entry< String,List< values>>条目:testMap 
.entrySet()){
...
mail.sendMail(msgFrom,body); //在另一个类中调用我的sendMail函数
}
mail.sendLastMails();

现在可以看看这种改进是否足够,还是应该外包。 p>

I have a spring-stand alone application which uses simple spring email code as below , the to and the message is constructed using the values iterated from map.

I have already had some suggestions for the question here , but i am in need of some specific advise for this. below is my code

for (Map.Entry<String, List<values>> entry : testMap
                .entrySet()) {
            String key = entry.getKey();
            StringBuilder htmlBuilder = new StringBuilder();            
            List<Model> valueList = entry.getValue();
            for (Model value : valueList) {
                htmlBuilder.append('List Values in the message');
            }
            mail.sendMail( msgFrom,body); // call my sendMail function in another class
        } 

Code for sending mail :

        MimeMessage email = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(email, true);
        helper.setFrom(new InternetAddress(from));
        helper.setTo(new InternetAddress(to));
        helper.setText(msg, true);
        helper.addInline("identifier1234", res);
        mailSender.send(email);

It takes 3 to 4 seconds to send mail . I have large user list of around 400,000 each day to be sent

Am i doing anything wrong or anyother approach to fasten this process. I am in need of experts advise

Thanks for your time and help :)

解决方案

IMHO, the process of sending mail itself can be improved, because currently, you open an new connection to mail server per message. You could improve it by using batched sending.

Spring MailSender interface natively supports sending of an array of messages instead of a single one, so you do not have do explicitely deal with JavaMail Session. You could simply modifiy the class actually sending the mail that way

int batchSize = 16; // for example, adjust it to you needs
MimeMessage[] messages = new MimeMessage[batchSize];
int messageIndex = 0;

public void sendMail(String msgFrom, String body) {
    // prepare MimeMessage
    messages[messageIndex++] = email;
    if (messagesIndex == batchSize) {
        mailSender.send(messages);
        messageIndex = 0;
    }

public void sendLastMails() {
    if (messageIndex > 0) {
        MimeMessage[] lastMessages = new MimeMessage[messageIndex];
        for (int i =0; i<messageIndex; i++) {
            lastMessages[i] = messages[i];
    }
    mailSender.send(lastMessages);
}

Edit:

The sendLastMails method may be called in several places. First, it must be called in the destroy method of a singleton bean to make sure no messages are forgotten when application closes. If the class sending mail is a singleton bean, it is enough to declare that the destroy method for the bean is sendLastMail, or calls it.

Then depending on you own business rules, it may be called after a batch of mails have been sent. Typical usage : in you example, you have testMap. You should rewrite it that way :

    for (Map.Entry<String, List<values>> entry : testMap
            .entrySet()) {
        ...
        mail.sendMail( msgFrom,body); // call my sendMail function in another class
    }
    mail.sendLastMails();

Now it is up to you to see if this improvement is enough or if you should outsource.

这篇关于通过弹出邮件提高发送批量邮件的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆