Skip to content

Commit 9117606

Browse files
committed
Merge remote-tracking branch 'origin/#246-fix-lingering-threads-bug' into develop
2 parents 061c271 + 7e0a89b commit 9117606

File tree

13 files changed

+93
-12
lines changed

13 files changed

+93
-12
lines changed

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[![APACHE v2 License](https://img.shields.io/badge/license-apachev2-blue.svg?style=flat)](modules/simple-java-mail/LICENSE-2.0.txt)
2-
[![Latest Release](https://img.shields.io/maven-central/v/org.simplejavamail/simple-java-mail.svg?style=flat)](https://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22org.simplejavamail%22%20AND%20v%3A%226.0.1%22)
3-
[![Javadocs](https://img.shields.io/badge/javadoc-6.0.1-brightgreen.svg?color=brightgreen)](https://www.javadoc.io/doc/org.simplejavamail/maven-master-project)
2+
[![Latest Release](https://img.shields.io/maven-central/v/org.simplejavamail/simple-java-mail.svg?style=flat)](https://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22org.simplejavamail%22%20AND%20v%3A%226.0.2%22)
3+
[![Javadocs](https://img.shields.io/badge/javadoc-6.0.2-brightgreen.svg?color=brightgreen)](https://www.javadoc.io/doc/org.simplejavamail/maven-master-project)
44
[![Codacy](https://img.shields.io/codacy/grade/c7506663a4ab41e49b9675d87cd900b7.svg?style=flat)](https://www.codacy.com/app/b-bottema/simple-java-mail)
55
![Java 1.7+](https://img.shields.io/badge/java-1.7+-lightgray.svg)
66

@@ -18,12 +18,17 @@ Simple Java Mail is also available in [Maven Central](https://search.maven.org/#
1818
<dependency>
1919
<groupId>org.simplejavamail</groupId>
2020
<artifactId>simple-java-mail</artifactId>
21-
<version>6.0.1</version>
21+
<version>6.0.2</version>
2222
</dependency>
2323
```
2424

2525
### Latest Progress ###
2626

27+
[v6.0.2](https://search.maven.org/#artifactdetails%7Corg.simplejavamail%7Csimple-java-mail%7C6.0.2%7Cjar) (21-Januari-2020)
28+
29+
- [#246](https://github.com/bbottema/simple-java-mail/issues/246) Bug: Sending async emails with and without the Batch module cause lingering threads
30+
31+
2732
v6.0.0-rc1 - [v6.0.1](https://search.maven.org/#artifactdetails%7Corg.simplejavamail%7Csimple-java-mail%7C6.0.1%7Cjar) (18-December-2019 - 24-January-2020)
2833

2934
After almost two years of development the next major release 6.0.0 is finally here! And what a doozy it is, with the following major new features:

RELEASE.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@ http://www.simplejavamail.org
33
<dependency>
44
<groupId>org.simplejavamail</groupId>
55
<artifactId>simple-java-mail</artifactId>
6-
<version>6.0.1</version>
6+
<version>6.0.2</version>
77
</dependency>
88

9+
v6.0.2 (21-Januari-2020)
10+
11+
- #246: Bug: Sending async emails with and without the Batch module cause lingering threads
12+
13+
914
v6.0.0-rc1 - v6.0.1 (18-December-2019 - 24-January-2020)
1015

1116
After almost two years of development the next major release 6.0.0 is finally here! And what a doozy it is, with the following major new features:

modules/batch-module/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
<dependency>
3131
<groupId>org.simplejavamail</groupId>
3232
<artifactId>smtp-connection-pool</artifactId>
33-
<version>1.0.7</version>
33+
<version>1.0.10</version>
3434
</dependency>
3535
</dependencies>
3636
</project>

modules/batch-module/src/main/java/org/simplejavamail/internal/batchsupport/AsyncOperationHelper.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package org.simplejavamail.internal.batchsupport;
22

3+
import org.jetbrains.annotations.NotNull;
34
import org.simplejavamail.api.mailer.AsyncResponse;
45
import org.simplejavamail.internal.batchsupport.concurrent.NamedRunnable;
56
import org.slf4j.Logger;
67

7-
import org.jetbrains.annotations.NotNull;
88
import java.util.concurrent.ExecutorService;
99
import java.util.concurrent.Executors;
1010
import java.util.concurrent.atomic.AtomicReference;
1111

1212
import static java.util.concurrent.Executors.newSingleThreadExecutor;
13+
import static org.simplejavamail.internal.util.Preconditions.assumeTrue;
1314
import static org.slf4j.LoggerFactory.getLogger;
1415

1516
/**
@@ -51,6 +52,7 @@ private static AsyncResponse executeAsync(final @NotNull ExecutorService executo
5152
// atomic reference is needed to be able to smuggle the asyncResponse
5253
// into the Runnable which is passed itself to the asyncResponse.
5354
final AtomicReference<AsyncResponseImpl> asyncResponseRef = new AtomicReference<>();
55+
assumeTrue(!executorService.isShutdown(), "cannot send async email, executor service is already shut down!");
5456
asyncResponseRef.set(new AsyncResponseImpl(executorService.submit(new NamedRunnable(processName) {
5557
@Override
5658
public void run() {

modules/cli-module/src/main/java/org/simplejavamail/internal/clisupport/CliCommandLineProducer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ static CommandLine configurePicoCli(List<CliDeclaredOptionSpec> declaredOptions,
3838
"%n" +
3939
"Note: All the regular functionality regarding properties and config files work with the CLI so you can provide defaults in a " +
4040
"property file as long as it is visible (on class path) or as system environment variables.")
41-
.version("Simple Java Mail 6.0.1");
41+
.version("Simple Java Mail 6.0.2");
4242

4343
rootCommandsHolder.usageMessage()
4444
.customSynopsis("",

modules/core-module/src/main/java/org/simplejavamail/api/mailer/Mailer.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,9 @@ public interface Mailer {
112112
* Shuts down the connection pool associated with this {@link Mailer} instance and closes remaining open connections. Waits until all connections still in use become available again
113113
* to deallocate them as well.
114114
* <p>
115-
* In order to shut down the whole connection pool (in case of clustering), each individual {@link Mailer} instance should be shutdown.
115+
* <strong>Note:</strong> In order to shut down the whole connection pool (in case of clustering), each individual {@link Mailer} instance should be shutdown.
116+
* <p>
117+
* <strong>Note:</strong> This does *not* shut down the executor service if it was provided by the user.
116118
* <p>
117119
* <strong>Note:</strong> this is only works in combination with the {@value org.simplejavamail.internal.modules.BatchModule#NAME}.
118120
*/

modules/core-module/src/main/java/org/simplejavamail/api/mailer/MailerGenericBuilder.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,10 @@ public interface MailerGenericBuilder<T extends MailerGenericBuilder<?>> {
243243
* <strong>Note:</strong> What makes it NonJvm is that the default keepAliveTime is set to the lowest non-zero value (so 1), so that
244244
* any threads will die off as soon as possible, as not to block the JVM from shutting down.
245245
* <p>
246+
* <p>
247+
* <strong>Note:</strong> Simple Java Mail will <strong>not</strong> shut down the provided executor service, even if the connection pool is being shut down.
248+
* <em>This will block the JVM from shutting down</em>. The user is responsible for managing the provided executor's life cycle.
249+
* <p>
246250
* <strong>Note:</strong> this only works in combination with the {@value org.simplejavamail.internal.modules.BatchModule#NAME}.
247251
*
248252
* @param executorService A custom executor service (ThreadPoolExecutor), replacing the {@code NonJvmBlockingThreadPoolExecutor}.

modules/core-module/src/main/java/org/simplejavamail/api/mailer/config/OperationalConfig.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ public interface OperationalConfig {
109109
@NotNull
110110
ExecutorService getExecutorService();
111111

112+
/**
113+
* Indicates whether the executor service was provided by the user or internally. Used to known if Simple Java Mail should shut down the executor
114+
* when the connection pool is shut down.
115+
*/
116+
boolean executorServiceIsUserProvided();
117+
112118
/**
113119
* @see MailerGenericBuilder#withCustomMailer(CustomMailer)
114120
*/
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.simplejavamail.mailer.internal;
2+
3+
import org.simplejavamail.api.mailer.MailerGenericBuilder;
4+
5+
/**
6+
* This interface is only there to improve readability there where internal builder API is used.
7+
*/
8+
@SuppressWarnings("UnusedReturnValue")
9+
public interface InternalMailerBuilder<T extends MailerGenericBuilder<?>> extends MailerGenericBuilder<T> {
10+
boolean isExecutorServiceUserProvided();
11+
}

modules/simple-java-mail/src/main/java/org/simplejavamail/mailer/internal/MailerGenericBuilderImpl.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
* @see MailerGenericBuilder
4040
*/
4141
@SuppressWarnings({"UnusedReturnValue", "unchecked"})
42-
abstract class MailerGenericBuilderImpl<T extends MailerGenericBuilderImpl<?>> implements MailerGenericBuilder<T> {
42+
abstract class MailerGenericBuilderImpl<T extends MailerGenericBuilderImpl<?>> implements InternalMailerBuilder<T> {
4343

4444
/**
4545
* @see MailerGenericBuilder#async()
@@ -95,6 +95,11 @@ abstract class MailerGenericBuilderImpl<T extends MailerGenericBuilderImpl<?>> i
9595
@NotNull
9696
private ExecutorService executorService;
9797

98+
/**
99+
* @see InternalMailerBuilder#isExecutorServiceUserProvided()
100+
*/
101+
private boolean executorServiceIsUserProvided = false;
102+
98103
/**
99104
* @see MailerGenericBuilder#withThreadPoolSize(Integer)
100105
*/
@@ -266,6 +271,7 @@ OperationalConfig buildOperationalConfig() {
266271
isTrustAllSSLHost(),
267272
isVerifyingServerIdentity(),
268273
getExecutorService(),
274+
isExecutorServiceUserProvided(),
269275
getCustomMailer());
270276
}
271277

@@ -376,6 +382,7 @@ public T withEmailAddressCriteria(@NotNull final EnumSet<EmailAddressCriteria> e
376382
@Override
377383
public T withExecutorService(@NotNull final ExecutorService executorService) {
378384
this.executorService = executorService;
385+
this.executorServiceIsUserProvided = true;
379386
return (T) this;
380387
}
381388

@@ -569,6 +576,7 @@ public T resetEmailAddressCriteria() {
569576
@Override
570577
public T resetExecutorService() {
571578
this.executorService = determineDefaultExecutorService();
579+
this.executorServiceIsUserProvided = false;
572580
return (T) this;
573581
}
574582

@@ -773,6 +781,14 @@ public ExecutorService getExecutorService() {
773781
return executorService;
774782
}
775783

784+
/**
785+
* @see InternalMailerBuilder#isExecutorServiceUserProvided()
786+
*/
787+
@Override
788+
public boolean isExecutorServiceUserProvided() {
789+
return executorServiceIsUserProvided;
790+
}
791+
776792
/**
777793
* @see MailerGenericBuilder#getThreadPoolSize()
778794
*/

0 commit comments

Comments
 (0)