1
1
/*
2
- * Copyright 2002-2016 the original author or authors.
2
+ * Copyright 2002-2018 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
16
16
17
17
package org .springframework .integration .mail ;
18
18
19
- import java .util .ArrayList ;
19
+ import java .util .Arrays ;
20
20
import java .util .Date ;
21
- import java .util .List ;
21
+ import java .util .Objects ;
22
22
import java .util .Properties ;
23
23
import java .util .concurrent .ScheduledFuture ;
24
24
@@ -63,13 +63,15 @@ public class ImapMailReceiver extends AbstractMailReceiver {
63
63
64
64
private final IdleCanceler idleCanceler = new IdleCanceler ();
65
65
66
- private volatile boolean shouldMarkMessagesAsRead = true ;
66
+ private boolean shouldMarkMessagesAsRead = true ;
67
67
68
- private volatile SearchTermStrategy searchTermStrategy = new DefaultSearchTermStrategy ();
68
+ private SearchTermStrategy searchTermStrategy = new DefaultSearchTermStrategy ();
69
69
70
- private volatile long cancelIdleInterval = DEFAULT_CANCEL_IDLE_INTERVAL ;
70
+ private long cancelIdleInterval = DEFAULT_CANCEL_IDLE_INTERVAL ;
71
71
72
- private volatile TaskScheduler scheduler ;
72
+ private TaskScheduler scheduler ;
73
+
74
+ private boolean isInternalScheduler ;
73
75
74
76
private volatile ScheduledFuture <?> pingTask ;
75
77
@@ -92,7 +94,6 @@ public ImapMailReceiver(String url) {
92
94
93
95
/**
94
96
* Check if messages should be marked as read.
95
- *
96
97
* @return true if messages should be marked as read.
97
98
*/
98
99
public Boolean isShouldMarkMessagesAsRead () {
@@ -102,7 +103,6 @@ public Boolean isShouldMarkMessagesAsRead() {
102
103
/**
103
104
* Provides a way to set custom {@link SearchTermStrategy} to compile a {@link SearchTerm}
104
105
* to be applied when retrieving mail
105
- *
106
106
* @param searchTermStrategy The search term strategy implementation.
107
107
*/
108
108
public void setSearchTermStrategy (SearchTermStrategy searchTermStrategy ) {
@@ -112,7 +112,6 @@ public void setSearchTermStrategy(SearchTermStrategy searchTermStrategy) {
112
112
113
113
/**
114
114
* Specify if messages should be marked as read.
115
- *
116
115
* @param shouldMarkMessagesAsRead true if messages should be marked as read.
117
116
*/
118
117
public void setShouldMarkMessagesAsRead (Boolean shouldMarkMessagesAsRead ) {
@@ -138,6 +137,7 @@ protected void onInit() throws Exception {
138
137
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler ();
139
138
scheduler .initialize ();
140
139
this .scheduler = scheduler ;
140
+ this .isInternalScheduler = true ;
141
141
}
142
142
Properties javaMailProperties = getJavaMailProperties ();
143
143
for (String name : new String []{"imap" , "imaps" }) {
@@ -148,10 +148,17 @@ protected void onInit() throws Exception {
148
148
}
149
149
}
150
150
151
+ @ Override
152
+ public void destroy () throws Exception {
153
+ super .destroy ();
154
+ if (this .isInternalScheduler ) {
155
+ ((ThreadPoolTaskScheduler ) this .scheduler ).shutdown ();
156
+ }
157
+ }
158
+
151
159
/**
152
160
* This method is unique to the IMAP receiver and only works if IMAP IDLE
153
161
* is supported (see RFC 2177 for more detail).
154
- *
155
162
* @throws MessagingException Any MessagingException.
156
163
*/
157
164
public void waitForNewMessages () throws MessagingException {
@@ -189,7 +196,6 @@ else if (!folder.getPermanentFlags().contains(Flags.Flag.RECENT)) {
189
196
* {@link javax.mail.Flags.Flag#ANSWERED ANSWERED}, and not
190
197
* {@link javax.mail.Flags.Flag#DELETED DELETED}. The search term is used
191
198
* to {@link Folder#search(SearchTerm) search} for new messages.
192
- *
193
199
* @return the new messages
194
200
* @throws MessagingException in case of JavaMail errors
195
201
*/
@@ -204,7 +210,6 @@ protected Message[] searchForNewMessages() throws MessagingException {
204
210
throw new MessagingException ("Folder is closed" );
205
211
}
206
212
207
- // INT-3859
208
213
private Message [] nullSafeMessages (Message [] messageArray ) {
209
214
boolean hasNulls = false ;
210
215
for (Message message : messageArray ) {
@@ -217,13 +222,9 @@ private Message[] nullSafeMessages(Message[] messageArray) {
217
222
return messageArray ;
218
223
}
219
224
else {
220
- List <Message > messages = new ArrayList <Message >();
221
- for (Message message : messageArray ) {
222
- if (message != null ) {
223
- messages .add (message );
224
- }
225
- }
226
- return messages .toArray (new Message [messages .size ()]);
225
+ return Arrays .stream (messageArray )
226
+ .filter (Objects ::nonNull )
227
+ .toArray (Message []::new );
227
228
}
228
229
}
229
230
@@ -276,6 +277,7 @@ public void messagesAdded(MessageCountEvent event) {
276
277
messages [0 ].getFolder ().isOpen ();
277
278
}
278
279
}
280
+
279
281
}
280
282
281
283
private class DefaultSearchTermStrategy implements SearchTermStrategy {
0 commit comments