Skip to content

Commit 992c0ce

Browse files
artembilangaryrussell
authored andcommitted
INT-4150: Make ImapMailReceiverTests much faster
JIRA: https://jira.spring.io/browse/INT-4150 * Fix internal `taskScheduler` for destroy in the `ImapMailReceiver` * Some code style polishing in the `ImapIdleChannelAdapter`
1 parent 29b4a29 commit 992c0ce

File tree

4 files changed

+111
-94
lines changed

4 files changed

+111
-94
lines changed

spring-integration-mail/src/main/java/org/springframework/integration/mail/ImapIdleChannelAdapter.java

+10-4
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,12 @@ public void run() {
256256
}
257257
}
258258
catch (Exception e) { //run again after a delay
259-
logger.warn("Failed to execute IDLE task. Will attempt to resubmit in " + ImapIdleChannelAdapter.this.reconnectDelay + " milliseconds.", e);
259+
if (logger.isWarnEnabled()) {
260+
logger.warn("Failed to execute IDLE task. Will attempt to resubmit in "
261+
+ ImapIdleChannelAdapter.this.reconnectDelay + " milliseconds.", e);
262+
}
260263
ImapIdleChannelAdapter.this.receivingTaskTrigger.delayNextExecution();
261-
ImapIdleChannelAdapter.this.publishException(e);
264+
publishException(e);
262265
}
263266
}
264267
}
@@ -312,6 +315,7 @@ public void run() {
312315
}
313316
}
314317
}
318+
315319
}
316320

317321
private class ExceptionAwarePeriodicTrigger implements Trigger {
@@ -334,18 +338,20 @@ public Date nextExecutionTime(TriggerContext triggerContext) {
334338
}
335339
}
336340

337-
public void delayNextExecution() {
341+
void delayNextExecution() {
338342
this.delayNextExecution = true;
339343
}
344+
340345
}
341346

342347
public class ImapIdleExceptionEvent extends MailIntegrationEvent {
343348

344349
private static final long serialVersionUID = -5875388810251967741L;
345350

346-
public ImapIdleExceptionEvent(Exception e) {
351+
ImapIdleExceptionEvent(Exception e) {
347352
super(ImapIdleChannelAdapter.this, e);
348353
}
349354

350355
}
356+
351357
}

spring-integration-mail/src/main/java/org/springframework/integration/mail/ImapMailReceiver.java

+22-20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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.
@@ -16,9 +16,9 @@
1616

1717
package org.springframework.integration.mail;
1818

19-
import java.util.ArrayList;
19+
import java.util.Arrays;
2020
import java.util.Date;
21-
import java.util.List;
21+
import java.util.Objects;
2222
import java.util.Properties;
2323
import 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

Comments
 (0)