11/*
2- * Copyright 2002-2016 the original author or authors.
2+ * Copyright 2002-2018 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
1616
1717package org .springframework .integration .mail ;
1818
19- import java .util .ArrayList ;
19+ import java .util .Arrays ;
2020import java .util .Date ;
21- import java .util .List ;
21+ import java .util .Objects ;
2222import java .util .Properties ;
2323import java .util .concurrent .ScheduledFuture ;
2424
@@ -63,13 +63,15 @@ public class ImapMailReceiver extends AbstractMailReceiver {
6363
6464 private final IdleCanceler idleCanceler = new IdleCanceler ();
6565
66- private volatile boolean shouldMarkMessagesAsRead = true ;
66+ private boolean shouldMarkMessagesAsRead = true ;
6767
68- private volatile SearchTermStrategy searchTermStrategy = new DefaultSearchTermStrategy ();
68+ private SearchTermStrategy searchTermStrategy = new DefaultSearchTermStrategy ();
6969
70- private volatile long cancelIdleInterval = DEFAULT_CANCEL_IDLE_INTERVAL ;
70+ private long cancelIdleInterval = DEFAULT_CANCEL_IDLE_INTERVAL ;
7171
72- private volatile TaskScheduler scheduler ;
72+ private TaskScheduler scheduler ;
73+
74+ private boolean isInternalScheduler ;
7375
7476 private volatile ScheduledFuture <?> pingTask ;
7577
@@ -92,7 +94,6 @@ public ImapMailReceiver(String url) {
9294
9395 /**
9496 * Check if messages should be marked as read.
95- *
9697 * @return true if messages should be marked as read.
9798 */
9899 public Boolean isShouldMarkMessagesAsRead () {
@@ -102,7 +103,6 @@ public Boolean isShouldMarkMessagesAsRead() {
102103 /**
103104 * Provides a way to set custom {@link SearchTermStrategy} to compile a {@link SearchTerm}
104105 * to be applied when retrieving mail
105- *
106106 * @param searchTermStrategy The search term strategy implementation.
107107 */
108108 public void setSearchTermStrategy (SearchTermStrategy searchTermStrategy ) {
@@ -112,7 +112,6 @@ public void setSearchTermStrategy(SearchTermStrategy searchTermStrategy) {
112112
113113 /**
114114 * Specify if messages should be marked as read.
115- *
116115 * @param shouldMarkMessagesAsRead true if messages should be marked as read.
117116 */
118117 public void setShouldMarkMessagesAsRead (Boolean shouldMarkMessagesAsRead ) {
@@ -138,6 +137,7 @@ protected void onInit() throws Exception {
138137 ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler ();
139138 scheduler .initialize ();
140139 this .scheduler = scheduler ;
140+ this .isInternalScheduler = true ;
141141 }
142142 Properties javaMailProperties = getJavaMailProperties ();
143143 for (String name : new String []{"imap" , "imaps" }) {
@@ -148,10 +148,17 @@ protected void onInit() throws Exception {
148148 }
149149 }
150150
151+ @ Override
152+ public void destroy () throws Exception {
153+ super .destroy ();
154+ if (this .isInternalScheduler ) {
155+ ((ThreadPoolTaskScheduler ) this .scheduler ).shutdown ();
156+ }
157+ }
158+
151159 /**
152160 * This method is unique to the IMAP receiver and only works if IMAP IDLE
153161 * is supported (see RFC 2177 for more detail).
154- *
155162 * @throws MessagingException Any MessagingException.
156163 */
157164 public void waitForNewMessages () throws MessagingException {
@@ -189,7 +196,6 @@ else if (!folder.getPermanentFlags().contains(Flags.Flag.RECENT)) {
189196 * {@link javax.mail.Flags.Flag#ANSWERED ANSWERED}, and not
190197 * {@link javax.mail.Flags.Flag#DELETED DELETED}. The search term is used
191198 * to {@link Folder#search(SearchTerm) search} for new messages.
192- *
193199 * @return the new messages
194200 * @throws MessagingException in case of JavaMail errors
195201 */
@@ -204,7 +210,6 @@ protected Message[] searchForNewMessages() throws MessagingException {
204210 throw new MessagingException ("Folder is closed" );
205211 }
206212
207- // INT-3859
208213 private Message [] nullSafeMessages (Message [] messageArray ) {
209214 boolean hasNulls = false ;
210215 for (Message message : messageArray ) {
@@ -217,13 +222,9 @@ private Message[] nullSafeMessages(Message[] messageArray) {
217222 return messageArray ;
218223 }
219224 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 );
227228 }
228229 }
229230
@@ -276,6 +277,7 @@ public void messagesAdded(MessageCountEvent event) {
276277 messages [0 ].getFolder ().isOpen ();
277278 }
278279 }
280+
279281 }
280282
281283 private class DefaultSearchTermStrategy implements SearchTermStrategy {
0 commit comments