Skip to content

Commit

Permalink
#85 overhauled setting sessionTimeout, so that it can be set (overrid…
Browse files Browse the repository at this point in the history
…den) programmatically from Java as well
  • Loading branch information
bbottema committed Aug 12, 2017
1 parent dce4229 commit ddae96e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 17 deletions.
25 changes: 10 additions & 15 deletions src/main/java/org/simplejavamail/mailer/Mailer.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,6 @@ public class Mailer {

private static final Logger LOGGER = LoggerFactory.getLogger(Mailer.class);

/**
* The default maximum timeout value for the transport socket is {@value #DEFAULT_SESSION_TIMEOUT_MILLIS}
* milliseconds. Can be overridden from a config file or through System variable.
*/
private static final int DEFAULT_SESSION_TIMEOUT_MILLIS = 60_000;

private final MailSender mailSender;

/**
Expand All @@ -110,8 +104,8 @@ public Mailer(final Session session) {
}

/**
* Custom Session constructor with proxy, stores the given mail session for later use. Assumes that *all* properties used to make a connection are
* configured (host, port, authentication and transport protocol settings).
* Custom Session constructor with proxy, stores the given mail session for later use. Assumes that *all* properties (except session
* timeouts) used to make a connection are configured (host, port, authentication and transport protocol settings).
* <p>
* Only proxy settings are always added if details are provided.
* <p>
Expand Down Expand Up @@ -236,13 +230,6 @@ public static Session createMailSession(final ServerConfig serverConfig, final T
props.put(transportStrategy.propertyNameHost(), serverConfig.getHost());
props.put(transportStrategy.propertyNamePort(), String.valueOf(serverConfig.getPort()));

// socket timeouts handling
final int sendMailTimeoutInMillis = ConfigLoader.valueOrProperty(null,
Property.DEFAULT_SESSION_TIMEOUT_MILLIS, DEFAULT_SESSION_TIMEOUT_MILLIS);
props.put("mail.smtp.connectiontimeout", String.valueOf(sendMailTimeoutInMillis));
props.put("mail.smtp.timeout", String.valueOf(sendMailTimeoutInMillis));
props.put("mail.smtp.writetimeout", String.valueOf(sendMailTimeoutInMillis));

if (serverConfig.getUsername() != null) {
props.put(transportStrategy.propertyNameUsername(), serverConfig.getUsername());
}
Expand Down Expand Up @@ -328,6 +315,14 @@ public void applyProperties(final Properties properties) {
public void setThreadPoolSize(final int poolSize) {
mailSender.setThreadPoolSize(poolSize);
}

/**
* @param sessionTimeout The timeout to use when sending emails (affects socket connect-, read- and write timeouts).
* @see Property#DEFAULT_SESSION_TIMEOUT_MILLIS
*/
public void setSessionTimeout(final int sessionTimeout) {
mailSender.setSessionTimeout(sessionTimeout);
}

/**
* Delegates to {@link #sendMail(Email, boolean)}, with <code>async = false</code>. This method returns only when the email has been processed by
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ public class MailSender {
* Defaults to {@code false}, sending mails rather than just only logging the mails.
*/
private static final boolean DEFAULT_MODE_LOGGING_ONLY = false;

/**
* The default maximum timeout value for the transport socket is {@value #DEFAULT_SESSION_TIMEOUT_MILLIS}
* milliseconds. Can be overridden from a config file or through System variable.
*/
private static final int DEFAULT_SESSION_TIMEOUT_MILLIS = 60_000;

/**
* For multi-threaded scenario's where a batch of emails sent asynchronously, the default maximum number of threads is {@value
Expand Down Expand Up @@ -79,7 +85,13 @@ public class MailSender {
private Phaser smtpRequestsPhaser;

/**
* The number of concurrent threads sending an email each. Used only when sending emails asynchronously (batch job mode). Default to
* The timeout to use when sending emails (affects socket connect-, read- and write timeouts). Defaults to
* {@value #DEFAULT_SESSION_TIMEOUT_MILLIS}.
*/
private int sessionTimeout;

/**
* The number of concurrent threads sending an email each. Used only when sending emails asynchronously (batch job mode). Defaults to
* {@value #DEFAULT_POOL_SIZE}.
*/
private int threadPoolSize;
Expand All @@ -90,12 +102,20 @@ public class MailSender {
private boolean transportModeLoggingOnly;

/**
* @see #configureSessionWithProxy(ProxyConfig, Session, TransportStrategy)
* Configures proxy: {@link #configureSessionWithProxy(ProxyConfig, Session, TransportStrategy)}(ProxyConfig, Session, TransportStrategy)
* <p>
* Also initializes:
* <ul>
* <li>{@link #sessionTimeout} from properties or default {@link #DEFAULT_SESSION_TIMEOUT_MILLIS}</li>
* <li>{@link #threadPoolSize} from properties or default {@link #DEFAULT_POOL_SIZE}</li>
* <li>{@link #transportModeLoggingOnly} from properties or default {@link #DEFAULT_MODE_LOGGING_ONLY}</li>
* </ul>
*/
public MailSender(final Session session, final ProxyConfig proxyConfig, final TransportStrategy transportStrategy) {
this.session = session;
this.proxyServer = configureSessionWithProxy(proxyConfig, session, transportStrategy);
this.threadPoolSize = ConfigLoader.valueOrProperty(null, Property.DEFAULT_POOL_SIZE, DEFAULT_POOL_SIZE);
this.sessionTimeout = ConfigLoader.valueOrProperty(null, Property.DEFAULT_SESSION_TIMEOUT_MILLIS, DEFAULT_SESSION_TIMEOUT_MILLIS);
this.transportModeLoggingOnly = ConfigLoader.valueOrProperty(null, Property.TRANSPORT_MODE_LOGGING_ONLY, DEFAULT_MODE_LOGGING_ONLY);
}

Expand Down Expand Up @@ -171,6 +191,7 @@ the proxy bridge server (or connection pool in async mode) while a non-async ema
if (executor == null || executor.isTerminated()) {
executor = Executors.newFixedThreadPool(threadPoolSize);
}
configureSessionWithTimeout(session, sessionTimeout);
executor.execute(new Runnable() {
@Override
public void run() {
Expand All @@ -187,6 +208,17 @@ public String toString() {
}
}

/**
* Configures the {@link Session} with the same timeout for socket connection timeout, read and write timeout.
*/
private void configureSessionWithTimeout(final Session session, final int sessionTimeout) {
// socket timeouts handling
final Properties sessionProperties = session.getProperties();
sessionProperties.put("mail.smtp.connectiontimeout", String.valueOf(sessionTimeout));
sessionProperties.put("mail.smtp.timeout", String.valueOf(sessionTimeout));
sessionProperties.put("mail.smtp.writetimeout", String.valueOf(sessionTimeout));
}

/**
* Separate closure that can be executed directly or from a thread. Refer to {@link #send(Email, boolean)} for details.
*
Expand Down Expand Up @@ -326,6 +358,14 @@ public Session getSession() {
public synchronized void setThreadPoolSize(final int threadPoolSize) {
this.threadPoolSize = threadPoolSize;
}

/**
* @param sessionTimeout The timeout to use when sending emails (affects socket connect-, read- and write timeouts).
* @see Property#DEFAULT_SESSION_TIMEOUT_MILLIS
*/
public void setSessionTimeout(final int sessionTimeout) {
this.sessionTimeout = sessionTimeout;
}

/**
* Sets the transport mode for this mail sender to logging only, which means no mail will be actually sent out.
Expand Down

0 comments on commit ddae96e

Please sign in to comment.