Skip to content

Commit 3e50190

Browse files
GH-3356: Add the way to not load mail message (#3402)
* GH-3356: Add the way to not load mail message Fixes #3356 In some cases it is not necessary to have a whole mail message to be load when it could be filtered out downstream * Treat "no eager load" for `MimeMessage` in the `AbstractMailReceiver` when no `headerMapper`, not `simpleContent` and no `autoCloseFolder` * Fix language in Docs Co-authored-by: Gary Russell <grussell@vmware.com> Co-authored-by: Gary Russell <grussell@vmware.com>
1 parent cadd4e6 commit 3e50190

File tree

4 files changed

+24
-10
lines changed

4 files changed

+24
-10
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -502,8 +502,8 @@ private void postProcessFilteredMessages(Message[] filteredMessages) throws Mess
502502
if (shouldDeleteMessages()) {
503503
deleteMessages(filteredMessages);
504504
}
505-
if (this.headerMapper == null) {
506-
// Copy messages to cause an eager fetch
505+
// Copy messages to cause an eager fetch
506+
if (this.headerMapper == null && (this.autoCloseFolder || this.simpleContent)) {
507507
for (int i = 0; i < filteredMessages.length; i++) {
508508
MimeMessage mimeMessage = new IntegrationMimeMessage((MimeMessage) filteredMessages[i]);
509509
filteredMessages[i] = mimeMessage;

spring-integration-mail/src/test/java/org/springframework/integration/mail/dsl/MailTests.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,8 @@
3333
import javax.mail.search.FromTerm;
3434
import javax.mail.search.SearchTerm;
3535

36-
import org.junit.BeforeClass;
37-
import org.junit.Test;
38-
import org.junit.runner.RunWith;
36+
import org.junit.jupiter.api.BeforeAll;
37+
import org.junit.jupiter.api.Test;
3938

4039
import org.springframework.beans.factory.annotation.Autowired;
4140
import org.springframework.beans.factory.annotation.Qualifier;
@@ -63,13 +62,13 @@
6362
import org.springframework.messaging.MessageHeaders;
6463
import org.springframework.messaging.PollableChannel;
6564
import org.springframework.test.annotation.DirtiesContext;
66-
import org.springframework.test.context.junit4.SpringRunner;
65+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
6766

6867
/**
6968
* @author Gary Russell
7069
* @author Artem Bilan
7170
*/
72-
@RunWith(SpringRunner.class)
71+
@SpringJUnitConfig
7372
@DirtiesContext
7473
public class MailTests {
7574

@@ -82,7 +81,7 @@ public class MailTests {
8281
private static final ImapServer imapIdleServer = TestMailServer.imap(0);
8382

8483

85-
@BeforeClass
84+
@BeforeAll
8685
public static void setup() throws InterruptedException {
8786
int n = 0;
8887
while (n++ < 100 && (!smtpServer.isListening() || !pop3Server.isListening()
@@ -216,7 +215,7 @@ public IntegrationFlow sendMailFlow() {
216215
.enrichHeaders(Mail.headers()
217216
.subjectFunction(m -> "foo")
218217
.from("foo@bar")
219-
.toFunction(m -> new String[] { "bar@baz" }))
218+
.toFunction(m -> new String[]{ "bar@baz" }))
220219
.handle(Mail.outboundAdapter("localhost")
221220
.port(smtpServer.getPort())
222221
.credentials("user", "pw")

src/reference/asciidoc/mail.adoc

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,17 @@ Closeable closeableResource = StaticMessageHeaderAccessor.getCloseableResource(m
128128
if (closeableResource != null) {
129129
closeableResource.close();
130130
}
131-
132131
----
133132
====
134133

135134
Keeping the folder open is useful in cases where communication with the server is needed during parsing multipart content of the email with attachments.
136135
The `close()` on the `IntegrationMessageHeaderAccessor.CLOSEABLE_RESOURCE` header delegates to the `AbstractMailReceiver` to close the folder with `expunge` option if `shouldDeleteMessages` is configured respectively on the `AbstractMailReceiver`.
137136

137+
Starting with version 5.4, it is possible now to return a `MimeMessage` as is without any conversion or eager content loading.
138+
This functionality is enabled with this combination of options: no `headerMapper` provided, the `simpleContent` property is `false` and the `autoCloseFolder` property is `false`.
139+
The `MimeMessage` is present as the payload of the Spring message produced.
140+
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 the `MimeMessage` is complete.
141+
138142
[[mail-mapping]]
139143
=== Inbound Mail Message Mapping
140144

@@ -210,6 +214,10 @@ Starting with version 4.3, the transformer handles embedded `Part` instances (as
210214
The transformer is a subclass of `AbstractMailTransformer` that maps the address and subject headers from the preceding list.
211215
If you wish to perform some other transformation on the message, consider subclassing `AbstractMailTransformer`.
212216

217+
Starting with version 5.4, when no `headerMapper` is provided, `autoCloseFolder` is `false` and `simpleContent` is `false`, the `MimeMessage` is returned as-is in the payload of the Spring message produced.
218+
This way, the content of the `MimeMessage` is loaded on demand when referenced, later in the flow.
219+
All of the mentioned above transformations are still valid.
220+
213221
[[mail-namespace]]
214222
=== Mail Namespace Support
215223

src/reference/asciidoc/whats-new.adoc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ See <<./ip.adoc#ip-collaborating-adapters,Collaborating Channel Adapters>> and <
7878
The `spring-integration-rmi` module is deprecated with no replacement and is going to be removed in the next major version.
7979
See <<./rmi.adoc#rmi, RMI Support>> for more information.
8080

81+
8182
[[x5.4-amqp]]
8283
=== AMQP Changes
8384

@@ -86,3 +87,9 @@ See <<./amqp.adoc#alternative-confirms-returns,Alternative Mechanism for Publish
8687

8788
A new `BatchMode.EXTRACT_PAYLOAD_WITH_HEADERS` is supported by the `AmqpInboundChannelAdapter`.
8889
See <<./amqp.adoc#amqp-inbound-channel-adapter,Inbound Channel Adapter>> for more information.
90+
91+
[[x5.4-mail]]
92+
=== Mail Changes
93+
94+
The `AbstractMailReceiver` can now produce the `MimeMessage` as-is without eager fetching its content.
95+
See <<./mail.adoc#mail-inbound, Mail-receiving Channel Adapter>> for more information.

0 commit comments

Comments
 (0)