Close mail folder if no messages to produce

Related to https://stackoverflow.com/questions/71667731/spring-integration-mimemessage-gmail-folder-is-not-open-exception

If no mail messages pulled from the folder or all of them are filtered out,
there is nothing to produce downstream.
Therefore, always close the folder in the end of `AbstractMailReceiver.receive()`
when no messages and even if `autoCloseFolder == false`

**Cherry-pick to `5.5.x`**
This commit is contained in:
Artem Bilan
2022-04-07 12:09:39 -04:00
committed by Gary Russell
parent f013aa8c29
commit 802d217a9a
3 changed files with 21 additions and 7 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -53,6 +53,7 @@ import org.springframework.messaging.MessageHeaders;
import org.springframework.util.Assert;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.MimeTypeUtils;
import org.springframework.util.ObjectUtils;
/**
* Base class for {@link MailReceiver} implementations.
@@ -362,6 +363,7 @@ public abstract class AbstractMailReceiver extends IntegrationObjectSupport impl
public Object[] receive() throws jakarta.mail.MessagingException {
this.folderReadLock.lock(); // NOSONAR - guarded with the getReadHoldCount()
try {
Object[] messagesToReturn = null;
try {
Folder folderToCheck = getFolder();
if (folderToCheck == null || !folderToCheck.isOpen()) {
@@ -375,10 +377,11 @@ public abstract class AbstractMailReceiver extends IntegrationObjectSupport impl
this.folderWriteLock.unlock();
}
}
return convertMessagesIfNecessary(searchAndFilterMessages());
messagesToReturn = convertMessagesIfNecessary(searchAndFilterMessages());
return messagesToReturn;
}
finally {
if (this.autoCloseFolder) {
if (this.autoCloseFolder || ObjectUtils.isEmpty(messagesToReturn)) {
closeFolder();
}
}