java - Improve performance on sending bulk emails through spring-mail -



java - Improve performance on sending bulk emails through spring-mail -

i have spring-stand lone application uses simple spring email code below , to , message constructed using values iterated map.

i have had suggestions question here , in need of specific advise this. below code

for (map.entry<string, list<values>> entry : testmap .entryset()) { string key = entry.getkey(); stringbuilder htmlbuilder = new stringbuilder(); list<model> valuelist = entry.getvalue(); (model value : valuelist) { htmlbuilder.append('list values in message'); } mail.sendmail( msgfrom,body); // phone call sendmail function in class }

code sending mail service :

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 4 seconds send mail service . have big user list of around 400,000 each day sent

am doing wrong or anyother approach fasten process. in need of experts advise

thanks time , help :)

imho, process of sending mail service can improved, because currently, open new connection mail service server per message. improve using batched sending.

spring mailsender interface natively supports sending of array of messages instead of single one, not have explicitely deal javamail session. modifiy class sending mail service way

int batchsize = 16; // example, adjust 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]; (int =0; i<messageindex; i++) { lastmessages[i] = messages[i]; } mailsender.send(lastmessages); }

edit:

the sendlastmails method may called in several places. first, must called in destroy method of singleton bean create sure no messages forgotten when application closes. if class sending mail service singleton bean, plenty declare destroy method bean is sendlastmail, or calls it.

then depending on own business rules, may called after batch of mails have been sent. typical usage : in example, have testmap. should rewrite way :

(map.entry<string, list<values>> entry : testmap .entryset()) { ... mail.sendmail( msgfrom,body); // phone call sendmail function in class } mail.sendlastmails();

now see if improvement plenty or if should outsource.

java spring performance email bulk

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -