INT-2803 Fix Lazy Fetch of Email Messages

INT-2805 Update JavaMail to 1.4.5

There is a need to fetch the entire email message before the folder
is closed. Once the folder is closed, you cannot perform any
more operations on the message.

Prior to RC1, the message was copied, which forced an eager fetch.

Add code to copy the message.

Also, transaction synchronization operations need access to a folder
instance to perform operations, such as delete, on a message.

Add a wrapper to lazily create a folder instance in message.getFolder()
when needed.

Add documentation to explain that messages must be re-fetched before
performing transaction synchronization operations.

JavaMail 1.4.5 is now Open Source, which makes debugging much
easier.
This commit is contained in:
Gary Russell
2012-11-01 17:14:31 -04:00
committed by Mark Fisher
parent bc8d499054
commit 413d5354a1
5 changed files with 215 additions and 59 deletions

View File

@@ -207,7 +207,7 @@ public abstract class AbstractMailReceiver extends IntegrationObjectSupport impl
protected void openFolder() throws MessagingException {
this.openSession();
if (this.folder == null) {
this.folder = this.store.getFolder(this.url);
this.folder = obtainFolderInstance();
}
if (this.folder == null || !this.folder.exists()) {
throw new IllegalStateException("no such folder [" + this.url.getFile() + "]");
@@ -221,6 +221,10 @@ public abstract class AbstractMailReceiver extends IntegrationObjectSupport impl
this.folder.open(this.folderOpenMode);
}
private Folder obtainFolderInstance() throws MessagingException {
return this.store.getFolder(this.url);
}
public Message[] receive() throws javax.mail.MessagingException {
synchronized (this.folderMonitor) {
try {
@@ -263,6 +267,11 @@ public abstract class AbstractMailReceiver extends IntegrationObjectSupport impl
if (this.shouldDeleteMessages()) {
this.deleteMessages(filteredMessages);
}
// Copy messages to cause an eager fetch
for (int i = 0; i < filteredMessages.length; i++) {
MimeMessage mimeMessage = new IntegrationMimeMessage((MimeMessage) filteredMessages[i]);
filteredMessages[i] = mimeMessage;
}
}
private void setMessageFlags(Message[] filteredMessages) throws MessagingException {
@@ -382,4 +391,29 @@ public abstract class AbstractMailReceiver extends IntegrationObjectSupport impl
return this.store;
}
/**
* Since we copy the message to eagerly fetch the message, it has no folder.
* However, we need to make a folder available in case the user wants to
* perform operations on the message in the folder later in the flow.
* @author Gary Russell
* @since 2.2
*
*/
public class IntegrationMimeMessage extends MimeMessage {
public IntegrationMimeMessage(MimeMessage source) throws MessagingException {
super(source);
}
@Override
public Folder getFolder() {
try {
return AbstractMailReceiver.this.obtainFolderInstance();
}
catch (MessagingException e) {
throw new org.springframework.integration.MessagingException("Unable to obtain the mail folder", e);
}
}
}
}