Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SAK-51048 - Send emails asynchronously #13376

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.nio.charset.Charset;
import java.util.*;
import java.util.Map.Entry;
import java.util.concurrent.CompletableFuture;

import javax.activation.DataHandler;
import javax.activation.DataSource;
Expand Down Expand Up @@ -741,113 +742,121 @@ private List<Address[]> getMessageSets(List<InternetAddress> addresses) {
}
return messageSets;
}

private void transportMessage(Session session, List<Address[]> messageSets, Collection<String> headers, MimeMessage msg) {
// transport the message
long time1 = 0;
long time2 = 0;
long time3 = 0;
long time4 = 0;
long time5 = 0;
long time6 = 0;
long timeExtraConnect = 0;
long timeExtraClose = 0;
long timeTmp = 0;
int numConnects = 1;
try
{
if (log.isDebugEnabled()) time1 = System.currentTimeMillis();
Transport transport = session.getTransport(protocol);
// Create a CompletableFuture to handle the asynchronous operation
CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
long time1 = 0;
long time2 = 0;
long time3 = 0;
long time4 = 0;
long time5 = 0;
long time6 = 0;
long timeExtraConnect = 0;
long timeExtraClose = 0;
long timeTmp = 0;
int numConnects = 1;
try
{
if (log.isDebugEnabled()) time1 = System.currentTimeMillis();
Transport transport = session.getTransport(protocol);

if (log.isDebugEnabled()) time2 = System.currentTimeMillis();
msg.saveChanges();
if (log.isDebugEnabled()) time2 = System.currentTimeMillis();
msg.saveChanges();

if (log.isDebugEnabled()) time3 = System.currentTimeMillis();
if (smtpUser != null && smtpPassword != null) {
transport.connect(smtp,smtpUser,smtpPassword);
} else {
transport.connect();
}
if (log.isDebugEnabled()) time3 = System.currentTimeMillis();
if (smtpUser != null && smtpPassword != null) {
transport.connect(smtp, smtpUser, smtpPassword);
} else {
transport.connect();
}

if (log.isDebugEnabled()) time4 = System.currentTimeMillis();
if (log.isDebugEnabled()) time4 = System.currentTimeMillis();

// loop the send for each message set
for (Iterator<Address[]> i = messageSets.iterator(); i.hasNext();)
{
Address[] toAddresses = i.next();

try
// loop the send for each message set
for (Iterator<Address[]> i = messageSets.iterator(); i.hasNext();)
{
transport.sendMessage(msg, toAddresses);
Address[] toAddresses = i.next();

// if we need to use the connection for just one send, and we have more, close and re-open
if ((oneMessagePerConnection) && (i.hasNext()))
try
{
if (log.isDebugEnabled()) timeTmp = System.currentTimeMillis();
transport.close();
if (log.isDebugEnabled()) timeExtraClose += (System.currentTimeMillis() - timeTmp);
transport.sendMessage(msg, toAddresses);

if (log.isDebugEnabled()) timeTmp = System.currentTimeMillis();
transport.connect();
if (log.isDebugEnabled())
// if we need to use the connection for just one send, and we have more, close and re-open
if ((oneMessagePerConnection) && (i.hasNext()))
{
timeExtraConnect += (System.currentTimeMillis() - timeTmp);
numConnects++;
if (log.isDebugEnabled()) timeTmp = System.currentTimeMillis();
transport.close();
if (log.isDebugEnabled()) timeExtraClose += (System.currentTimeMillis() - timeTmp);

if (log.isDebugEnabled()) timeTmp = System.currentTimeMillis();
transport.connect();
if (log.isDebugEnabled())
{
timeExtraConnect += (System.currentTimeMillis() - timeTmp);
numConnects++;
}
}
}
catch (SendFailedException e)
{
if (log.isDebugEnabled()) log.debug("transportMessage: " + e);
}
catch (MessagingException e)
{
log.warn("transportMessage: " + e);
}
}
catch (SendFailedException e)
{
if (log.isDebugEnabled()) log.debug("transportMessage: " + e);
}
catch (MessagingException e)
{
log.warn("transportMessage: " + e);
}
}

if (log.isDebugEnabled()) time5 = System.currentTimeMillis();
transport.close();
if (log.isDebugEnabled()) time5 = System.currentTimeMillis();
transport.close();

if (log.isDebugEnabled()) time6 = System.currentTimeMillis();
}
catch (MessagingException e)
{
log.warn("transportMessage:" + e);
}

// log
if (log.isInfoEnabled())
{
StringBuilder buf = new StringBuilder();
buf.append("transportMessage: headers[");
for (String header : headers)
if (log.isDebugEnabled()) time6 = System.currentTimeMillis();
}
catch (MessagingException e)
{
buf.append(" ");
buf.append(cleanUp(header));
log.warn("transportMessage:" + e);
}
buf.append("]");
for (Address[] toAddresses : messageSets)

// log
if (log.isInfoEnabled())
{
buf.append(" to[ ");
for (int a = 0; a < toAddresses.length; a++)
StringBuilder buf = new StringBuilder();
buf.append("transportMessage: headers[");
for (String header : headers)
{
buf.append(" ");
buf.append(toAddresses[a]);
buf.append(cleanUp(header));
}
buf.append("]");
}
for (Address[] toAddresses : messageSets)
{
buf.append(" to[ ");
for (int a = 0; a < toAddresses.length; a++)
{
buf.append(" ");
buf.append(toAddresses[a]);
}
buf.append("]");
}

if (log.isDebugEnabled())
{
buf.append(" times[ ");
buf.append(" getransport:" + (time2 - time1) + " savechanges:" + (time3 - time2) + " connect(#" + numConnects + "):"
+ ((time4 - time3) + timeExtraConnect) + " send:" + (((time5 - time4) - timeExtraConnect) - timeExtraClose)
+ " close:" + ((time6 - time5) + timeExtraClose) + " total: " + (time6 - time1) + " ]");
if (log.isDebugEnabled())
{
buf.append(" times[ ");
buf.append(" getransport:" + (time2 - time1) + " savechanges:" + (time3 - time2) + " connect(#" + numConnects + "):"
+ ((time4 - time3) + timeExtraConnect) + " send:" + (((time5 - time4) - timeExtraConnect) - timeExtraClose)
+ " close:" + ((time6 - time5) + timeExtraClose) + " total: " + (time6 - time1) + " ]");
}

log.info(buf.toString());
}
});

log.info(buf.toString());
}
// Optionally handle exceptions or completion
future.exceptionally(ex -> {
log.error("Error occurred while sending email: " + ex.getMessage());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
log.error("Error occurred while sending email: " + ex.getMessage());
log.error("Error occurred while sending email: {}", ex.toString());

return null;
});
}

private Properties createMailSessionProperties()
Expand Down
Loading