24
24
import java .io .UnsupportedEncodingException ;
25
25
import java .util .List ;
26
26
import java .util .Properties ;
27
- import java .util .concurrent .ExecutorService ;
28
27
import java .util .concurrent .Phaser ;
29
28
30
29
import static java .lang .String .format ;
@@ -49,6 +48,7 @@ public class MailSenderImpl implements MailSender {
49
48
/**
50
49
* Used to actually send the email. This session can come from being passed in the default constructor, or made by <code>Mailer</code> directly.
51
50
*/
51
+ @ Nonnull
52
52
private final Session session ;
53
53
54
54
/**
@@ -65,6 +65,7 @@ public class MailSenderImpl implements MailSender {
65
65
/**
66
66
* @see OperationalConfig
67
67
*/
68
+ @ Nonnull
68
69
private final OperationalConfig operationalConfig ;
69
70
70
71
/**
@@ -74,14 +75,6 @@ public class MailSenderImpl implements MailSender {
74
75
@ Nullable
75
76
private final AnonymousSocks5Server proxyServer ;
76
77
77
- /**
78
- * Allows us to manage how many thread we run at the same time using a thread pool.
79
- * <p>
80
- * Can't be initialized in the field, because we need to reinitialize it whenever the threadpool was closed after a batch of emails and this
81
- * MailSender instance is again engaged.
82
- */
83
- private ExecutorService executor ;
84
-
85
78
/**
86
79
* Used to keep track of running SMTP requests, so that we know when to close down the proxy bridging server (if used).
87
80
* <p>
@@ -106,7 +99,7 @@ private void initSession(@Nonnull final Session session, @Nonnull OperationalCon
106
99
session .setDebug (operationalConfig .isDebugLogging ());
107
100
session .getProperties ().putAll (operationalConfig .getProperties ());
108
101
109
- configureSessionWithTimeout (session , operationalConfig .getSessionTimeout ());
102
+ configureSessionWithTimeout (session , operationalConfig .getSessionTimeout (), transportStrategy );
110
103
111
104
if (transportStrategy != null ) {
112
105
if (operationalConfig .isTrustAllSSLHost ()) {
@@ -186,18 +179,14 @@ the proxy bridge server (or connection pool in async mode) while a non-async ema
186
179
sendMailClosure .run ();
187
180
return null ;
188
181
} else {
189
- // start up thread pool if necessary
190
- if (executor == null ) {
191
- executor = new NonJvmBlockingThreadPoolExecutor (operationalConfig , "Simple Java Mail async mail sender" );
192
- }
193
- return AsyncOperationHelper .executeAsync (executor , "sendMail process" , sendMailClosure );
182
+ return AsyncOperationHelper .executeAsync (operationalConfig .getExecutorService (), "sendMail process" , sendMailClosure );
194
183
}
195
184
}
196
185
197
186
/**
198
187
* Configures the {@link Session} with the same timeout for socket connection timeout, read and write timeout.
199
188
*/
200
- private void configureSessionWithTimeout (final Session session , final int sessionTimeout ) {
189
+ private void configureSessionWithTimeout (@ Nonnull final Session session , final int sessionTimeout , @ Nullable final TransportStrategy transportStrategy ) {
201
190
if (transportStrategy != null ) {
202
191
// socket timeouts handling
203
192
final Properties sessionProperties = session .getProperties ();
@@ -384,7 +373,7 @@ public void run() {
384
373
385
374
boolean proxyBridgeStartedForTestingConnection = false ;
386
375
387
- configureSessionWithTimeout (session , operationalConfig .getSessionTimeout ());
376
+ configureSessionWithTimeout (session , operationalConfig .getSessionTimeout (), transportStrategy );
388
377
389
378
logSession (session , async , "connection test" );
390
379
@@ -419,6 +408,7 @@ private boolean needsAuthenticatedProxy() {
419
408
* @see MailSender#getSession()
420
409
*/
421
410
@ Override
411
+ @ Nonnull
422
412
public Session getSession () {
423
413
return session ;
424
414
}
@@ -427,6 +417,7 @@ public Session getSession() {
427
417
* @see MailSender#getOperationalConfig()
428
418
*/
429
419
@ Override
420
+ @ Nonnull
430
421
public OperationalConfig getOperationalConfig () {
431
422
return operationalConfig ;
432
423
}
0 commit comments