From c75be107fa12d063c2876031fefc07c16d0eed12 Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Thu, 7 Apr 2022 12:09:39 -0400 Subject: [PATCH] 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`** --- .../integration/mail/AbstractMailReceiver.java | 9 ++++++--- .../integration/mail/MailReceiverTests.java | 16 ++++++++++++---- src/reference/asciidoc/mail.adoc | 3 +++ 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/spring-integration-mail/src/main/java/org/springframework/integration/mail/AbstractMailReceiver.java b/spring-integration-mail/src/main/java/org/springframework/integration/mail/AbstractMailReceiver.java index 587c75f0ef..a53d1f9ad6 100755 --- a/spring-integration-mail/src/main/java/org/springframework/integration/mail/AbstractMailReceiver.java +++ b/spring-integration-mail/src/main/java/org/springframework/integration/mail/AbstractMailReceiver.java @@ -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 javax.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(); } } diff --git a/spring-integration-mail/src/test/java/org/springframework/integration/mail/MailReceiverTests.java b/spring-integration-mail/src/test/java/org/springframework/integration/mail/MailReceiverTests.java index 0cf899d4da..1f57965228 100644 --- a/spring-integration-mail/src/test/java/org/springframework/integration/mail/MailReceiverTests.java +++ b/spring-integration-mail/src/test/java/org/springframework/integration/mail/MailReceiverTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2021 the original author or authors. + * Copyright 2014-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. @@ -16,6 +16,8 @@ package org.springframework.integration.mail; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; @@ -46,12 +48,12 @@ import org.springframework.beans.DirectFieldAccessor; public class MailReceiverTests { @Test - public void testStoreConnect() throws Exception { + public void testStoreConnectAndFolderCloseWhenNoMessages() throws Exception { AbstractMailReceiver receiver = new AbstractMailReceiver() { @Override protected Message[] searchForNewMessages() { - return null; + return new Message[0]; } }; @@ -59,18 +61,24 @@ public class MailReceiverTests { Session session = Session.getInstance(props); receiver.setSession(session); receiver.setProtocol("imap"); + receiver.setAutoCloseFolder(false); Store store = session.getStore("imap"); store = spy(store); new DirectFieldAccessor(receiver).setPropertyValue("store", store); when(store.isConnected()).thenReturn(false); Folder folder = mock(Folder.class); when(folder.exists()).thenReturn(true); - when(folder.isOpen()).thenReturn(false); + when(folder.isOpen()).thenReturn(false, true); doReturn(folder).when(store).getFolder((URLName) null); doNothing().when(store).connect(); receiver.openFolder(); receiver.openFolder(); verify(store, times(2)).connect(); + + Object[] receive = receiver.receive(); + assertThat(receive).isEmpty(); + + verify(folder).close(anyBoolean()); } } diff --git a/src/reference/asciidoc/mail.adoc b/src/reference/asciidoc/mail.adoc index 84d061d311..1ab142f1d7 100644 --- a/src/reference/asciidoc/mail.adoc +++ b/src/reference/asciidoc/mail.adoc @@ -140,6 +140,9 @@ This functionality is enabled with this combination of options: no `headerMapper The `MimeMessage` is present as the payload of the Spring message produced. In this case, the only header populated is the above mentioned `IntegrationMessageHeaderAccessor.CLOSEABLE_RESOURCE` for the folder which must be closed when processing of the `MimeMessage` is complete. +Starting with version 5.5.11, the folder is closed automatically after `AbstractMailReceiver.receive()` if no messages received or all of them are filtered out independently of the `autoCloseFolder` flag. +In this case there is nothing to produce downstream for possible logic around `IntegrationMessageHeaderAccessor.CLOSEABLE_RESOURCE` header. + [[mail-mapping]] === Inbound Mail Message Mapping