camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [1/2] camel git commit: CAMEL-7474: Add option to mail consumer to skip or handle if there is an error retrieving a mail from the mailbox. This can avoid an endless problem if one mail is problematic.
Date Sat, 21 Mar 2015 11:35:12 GMT
Repository: camel
Updated Branches:
  refs/heads/camel-2.15.x adf655d54 -> 99106e411
  refs/heads/master 5224a13e5 -> 8639a14ea


CAMEL-7474: Add option to mail consumer to skip or handle if there is an error retrieving
a mail from the mailbox. This can avoid an endless problem if one mail is problematic.


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/8639a14e
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/8639a14e
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/8639a14e

Branch: refs/heads/master
Commit: 8639a14ead5440860f4638ce2608f407ded278a7
Parents: 5224a13
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Sat Mar 21 12:31:47 2015 +0100
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Sat Mar 21 12:36:43 2015 +0100

----------------------------------------------------------------------
 .../camel/component/mail/MailConfiguration.java | 34 +++++++++++++-
 .../camel/component/mail/MailConsumer.java      | 47 ++++++++++++++++++--
 .../camel/component/mail/MailEndpoint.java      |  3 ++
 3 files changed, 79 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/8639a14e/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConfiguration.java
----------------------------------------------------------------------
diff --git a/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConfiguration.java
b/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConfiguration.java
index 526e32c..794ebf6 100644
--- a/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConfiguration.java
+++ b/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConfiguration.java
@@ -20,7 +20,6 @@ import java.net.URI;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Properties;
-
 import javax.mail.Message;
 import javax.mail.Session;
 import javax.net.ssl.SSLContext;
@@ -98,6 +97,10 @@ public class MailConfiguration implements Cloneable {
     private boolean closeFolder = true;
     @UriParam(defaultValue = "true") @Metadata(label = "consumer")
     private boolean peek = true;
+    @UriParam @Metadata(label = "consumer")
+    private boolean skipFailedMessage;
+    @UriParam @Metadata(label = "consumer")
+    private boolean handleFailedMessage;
     @UriParam
     private SSLContextParameters sslContextParameters;
     private ClassLoader applicationClassLoader;
@@ -633,4 +636,33 @@ public class MailConfiguration implements Cloneable {
     public void setPeek(boolean peek) {
         this.peek = peek;
     }
+
+    public boolean isSkipFailedMessage() {
+        return skipFailedMessage;
+    }
+
+    /**
+     * If the mail consumer cannot retrieve a given mail message, then this option allows
to skip
+     * the message and move on to retrieve the next mail message.
+     * <p/>
+     * The default behavior would be the consumer throws an exception and no mails from the
batch would be able to be routed by Camel.
+     */
+    public void setSkipFailedMessage(boolean skipFailedMessage) {
+        this.skipFailedMessage = skipFailedMessage;
+    }
+
+    public boolean isHandleFailedMessage() {
+        return handleFailedMessage;
+    }
+
+    /**
+     * If the mail consumer cannot retrieve a given mail message, then this option allows
to handle
+     * the caused exception by the consumer's error handler. By enable the bridge error handler
on the consumer,
+     * then the Camel routing error handler can handle the exception instead.
+     * <p/>
+     * The default behavior would be the consumer throws an exception and no mails from the
batch would be able to be routed by Camel.
+     */
+    public void setHandleFailedMessage(boolean handleFailedMessage) {
+        this.handleFailedMessage = handleFailedMessage;
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/8639a14e/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConsumer.java
----------------------------------------------------------------------
diff --git a/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConsumer.java
b/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConsumer.java
index 2015cca..7ba9397 100644
--- a/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConsumer.java
+++ b/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConsumer.java
@@ -16,8 +16,10 @@
  */
 package org.apache.camel.component.mail;
 
+import java.util.ArrayList;
 import java.util.Enumeration;
 import java.util.LinkedList;
+import java.util.List;
 import java.util.Queue;
 import java.util.UUID;
 import javax.mail.Flags;
@@ -54,6 +56,8 @@ public class MailConsumer extends ScheduledBatchPollingConsumer {
     private final JavaMailSender sender;
     private Folder folder;
     private Store store;
+    private boolean skipFailedMessage;
+    private boolean handleFailedMessage;
 
     /**
      * Is true if server is an IMAP server and supports IMAP SORT extension.
@@ -251,11 +255,9 @@ public class MailConsumer extends ScheduledBatchPollingConsumer {
             }
         } else {
             if (searchTerm != null) {
-                // Only search
-                messages = folder.search(searchTerm);
+                messages = folder.search(searchTerm, retrieveAllMessages());
             } else {
-                // No search
-                messages = folder.getMessages();
+                messages = retrieveAllMessages();
             }
             // Now we can sort (emulate email sort but restrict sort terms)
             if (sortTerm != null) {
@@ -265,6 +267,28 @@ public class MailConsumer extends ScheduledBatchPollingConsumer {
         return messages;
     }
 
+    private Message[] retrieveAllMessages() throws MessagingException {
+        int total = folder.getMessageCount();
+        List<Message> msgs = new ArrayList<Message>();
+
+        // Note that message * numbers start at 1, not 0
+        for (int i = 1; i <= total; i++) {
+            try {
+                Message msg = folder.getMessage(i);
+                msgs.add(msg);
+            } catch (MessagingException e) {
+                if (skipFailedMessage) {
+                    LOG.debug("Skipping failed message at index " + i + " due " + e.getMessage(),
e);
+                } else if (handleFailedMessage) {
+                    handleException(e);
+                } else {
+                    throw e;
+                }
+            }
+        }
+        return msgs.toArray(new Message[msgs.size()]);
+    }
+
     /**
      * @return Search term from endpoint (including "seen" check) or null if there is no
search term
      */
@@ -508,4 +532,19 @@ public class MailConsumer extends ScheduledBatchPollingConsumer {
         return (MailEndpoint) super.getEndpoint();
     }
 
+    public boolean isSkipFailedMessage() {
+        return skipFailedMessage;
+    }
+
+    public void setSkipFailedMessage(boolean skipFailedMessage) {
+        this.skipFailedMessage = skipFailedMessage;
+    }
+
+    public boolean isHandleFailedMessage() {
+        return handleFailedMessage;
+    }
+
+    public void setHandleFailedMessage(boolean handleFailedMessage) {
+        this.handleFailedMessage = handleFailedMessage;
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/8639a14e/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailEndpoint.java
----------------------------------------------------------------------
diff --git a/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailEndpoint.java
b/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailEndpoint.java
index a7a89cd..4f9e3be 100644
--- a/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailEndpoint.java
+++ b/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailEndpoint.java
@@ -104,6 +104,9 @@ public class MailEndpoint extends ScheduledPollEndpoint {
     public Consumer createConsumer(Processor processor, JavaMailSender sender) throws Exception
{
         MailConsumer answer = new MailConsumer(this, processor, sender);
 
+        answer.setHandleFailedMessage(configuration.isHandleFailedMessage());
+        answer.setSkipFailedMessage(configuration.isSkipFailedMessage());
+
         // ScheduledPollConsumer default delay is 500 millis and that is too often for polling
a mailbox,
         // so we override with a new default value. End user can override this value by providing
a consumer.delay parameter
         answer.setDelay(MailConsumer.DEFAULT_CONSUMER_DELAY);


Mime
View raw message