You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2008/05/04 16:31:58 UTC

svn commit: r653216 - in /activemq/camel/trunk/components/camel-mail/src: main/java/org/apache/camel/component/mail/ test/java/org/apache/camel/component/mail/

Author: davsclaus
Date: Sun May  4 07:31:58 2008
New Revision: 653216

URL: http://svn.apache.org/viewvc?rev=653216&view=rev
Log:
CAMEL-335
- Added fetchSize option to be used when consuming mails

Added:
    activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailFetchSizeTest.java   (with props)
    activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailFetchSizeZeroTest.java   (with props)
Modified:
    activemq/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConfiguration.java
    activemq/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConsumer.java
    activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailComponentTest.java

Modified: activemq/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConfiguration.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConfiguration.java?rev=653216&r1=653215&r2=653216&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConfiguration.java (original)
+++ activemq/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConfiguration.java Sun May  4 07:31:58 2008
@@ -50,6 +50,7 @@
     private boolean ignoreUriScheme;
     private boolean processOnlyUnseenMessages;
     private Map<Message.RecipientType, String> recipients = new HashMap<Message.RecipientType, String>();
+    private int fetchSize = -1;
 
     public MailConfiguration() {
     }
@@ -273,4 +274,11 @@
         return recipients;
     }
 
+    public int getFetchSize() {
+        return fetchSize;
+    }
+
+    public void setFetchSize(int fetchSize) {
+        this.fetchSize = fetchSize;
+    }
 }

Modified: activemq/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConsumer.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConsumer.java?rev=653216&r1=653215&r2=653216&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConsumer.java (original)
+++ activemq/camel/trunk/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailConsumer.java Sun May  4 07:31:58 2008
@@ -77,8 +77,14 @@
 
     protected void poll() throws Exception {
         if (LOG.isDebugEnabled()) {
-            LOG.debug("Polling mailfolder " + folder.getFullName() + " at host " + endpoint.getConfiguration().getHost());
+            LOG.debug("Polling mailfolder " + folder.getFullName() + " at host " + endpoint.getConfiguration().getHost() + ":" + endpoint.getConfiguration().getPort());
         }
+
+        if (endpoint.getConfiguration().getFetchSize() == 0) {
+            LOG.warn("Fetch size is 0 meaning the configuration is set to poll no new messages at all. Camel will skip this poll.");
+            return;
+        }
+
         // ensure folder is open
         if (!folder.isOpen()) {
             folder.open(Folder.READ_WRITE);
@@ -113,13 +119,21 @@
      * Process all the messages
      */
     protected void processMessages(Message[] messages) throws Exception {
-        for (Message message : messages) {
+        int fetchSize = endpoint.getConfiguration().getFetchSize();
+        int count = fetchSize == -1 ? messages.length : Math.min(fetchSize, messages.length);
+
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("Fetching " + count + " messages. Total " + messages.length + " messages.");
+        }
+
+        for (int i = 0; i < count; i++) {
+            Message message = messages[i];
             if (!message.getFlags().contains(Flags.Flag.DELETED)) {
                 processMessage(message);
                 flagMessageProcessed(message);
             } else {
                 if (LOG.isDebugEnabled()) {
-                    LOG.debug("Skipping message as it was flagged as DELETED: " + MailUtils.dumpMessage(message));
+                    LOG.debug("Skipping message as it was flagged as deleted: " + MailUtils.dumpMessage(message));
                 }
             }
         }

Modified: activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailComponentTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailComponentTest.java?rev=653216&r1=653215&r2=653216&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailComponentTest.java (original)
+++ activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailComponentTest.java Sun May  4 07:31:58 2008
@@ -71,6 +71,7 @@
         assertEquals("password", "secret", config.getPassword());
         assertEquals(true, config.isDeleteProcessedMessages());
         assertEquals(false, config.isIgnoreUriScheme());
+        assertEquals("fetchSize", -1, config.getFetchSize());
     }
 
     public void testDefaultPOP3Configuration() throws Exception {
@@ -87,6 +88,7 @@
         assertEquals("password", "secret", config.getPassword());
         assertEquals(true, config.isDeleteProcessedMessages());
         assertEquals(false, config.isIgnoreUriScheme());
+        assertEquals("fetchSize", -1, config.getFetchSize());
     }
 
     public void testDefaultIMAPConfiguration() throws Exception {
@@ -103,6 +105,7 @@
         assertEquals("password", "secret", config.getPassword());
         assertEquals(true, config.isDeleteProcessedMessages());
         assertEquals(false, config.isIgnoreUriScheme());
+        assertEquals("fetchSize", -1, config.getFetchSize());
     }
 
     public void testManyConfigurations() throws Exception {
@@ -120,6 +123,7 @@
         assertEquals("password", "secret", config.getPassword());
         assertEquals(false, config.isDeleteProcessedMessages());
         assertEquals(false, config.isIgnoreUriScheme());
+        assertEquals("fetchSize", -1, config.getFetchSize());
     }
 
     public void testDestination() {
@@ -136,6 +140,7 @@
         assertEquals("password", "secret", config.getPassword());
         assertEquals(true, config.isDeleteProcessedMessages());
         assertEquals(false, config.isIgnoreUriScheme());
+        assertEquals("fetchSize", -1, config.getFetchSize());
     }
 
     public void testNoUserInfoButUsername() {
@@ -152,6 +157,19 @@
         assertEquals("password", "secret", config.getPassword());
         assertEquals(true, config.isDeleteProcessedMessages());
         assertEquals(false, config.isIgnoreUriScheme());
+        assertEquals("fetchSize", -1, config.getFetchSize());
+    }
+
+    public void testMailEndpointsWithFetchSize() throws Exception {
+        MailEndpoint endpoint = resolveMandatoryEndpoint("pop3://james@myhost?fetchSize=5");
+        MailConfiguration config = endpoint.getConfiguration();
+        assertEquals("getProtocol()", "pop3", config.getProtocol());
+        assertEquals("getHost()", "myhost", config.getHost());
+        assertEquals("getPort()", 110, config.getPort());
+        assertEquals("getUsername()", "james", config.getUsername());
+        assertEquals("getDestination()", "james@myhost", config.getDestination());
+        assertEquals("folder", "INBOX", config.getFolderName());
+        assertEquals("fetchSize", 5, config.getFetchSize());
     }
 
     @Override

Added: activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailFetchSizeTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailFetchSizeTest.java?rev=653216&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailFetchSizeTest.java (added)
+++ activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailFetchSizeTest.java Sun May  4 07:31:58 2008
@@ -0,0 +1,91 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.mail;
+
+import javax.mail.Folder;
+import javax.mail.Message;
+import javax.mail.Store;
+import javax.mail.internet.MimeMessage;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.springframework.mail.javamail.JavaMailSenderImpl;
+import org.jvnet.mock_javamail.Mailbox;
+
+/**
+ * Unit test for fetch size.
+ */
+public class MailFetchSizeTest extends ContextTestSupport {
+
+    public void testFetchSize() throws Exception {
+        prepareMailbox();
+        Mailbox mailbox = Mailbox.get("james@localhost");
+        assertEquals(5, mailbox.size());
+
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(2);
+        mock.expectedBodiesReceived("Message 0", "Message 1");
+        // should be done within 2 seconds as no delay when started
+        mock.setResultWaitTime(2000L);
+        mock.assertIsSatisfied();
+
+        assertEquals(3, mailbox.size());
+
+        // reset mock to assert the next batch of 2 messages polled
+        mock.reset();
+        mock.expectedMessageCount(2);
+        mock.expectedBodiesReceived("Message 2", "Message 3");
+        // should be done within 5 (delay) + 1 seconds (polling)
+        mock.setResultWaitTime(7000L);
+        mock.assertIsSatisfied();
+
+        assertEquals(1, mailbox.size());
+
+        // reset mock to assert the last message polled
+        mock.reset();
+        mock.expectedMessageCount(1);
+        mock.expectedBodiesReceived("Message 4");
+        mock.assertIsSatisfied();
+    }
+
+    private void prepareMailbox() throws Exception {
+        // connect to mailbox
+        JavaMailSenderImpl sender = new JavaMailSenderImpl();
+        Store store = sender.getSession().getStore("pop3");
+        store.connect("localhost", 25, "james", "secret");
+        Folder folder = store.getFolder("INBOX");
+        folder.open(Folder.READ_WRITE);
+
+        // inserts 5 new messages
+        Message[] messages = new Message[5];
+        for (int i = 0; i < 5; i++) {
+            messages[i] = new MimeMessage(sender.getSession());
+            messages[i].setText("Message " + i);
+        }
+        folder.appendMessages(messages);
+        folder.close(true);
+    }
+
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() throws Exception {
+                from("pop3://james@localhost?password=secret&fetchSize=2&consumer.delay=5000").to("mock:result");
+            }
+        };
+    }
+}

Propchange: activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailFetchSizeTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailFetchSizeTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailFetchSizeZeroTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailFetchSizeZeroTest.java?rev=653216&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailFetchSizeZeroTest.java (added)
+++ activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailFetchSizeZeroTest.java Sun May  4 07:31:58 2008
@@ -0,0 +1,60 @@
+package org.apache.camel.component.mail;
+
+import javax.mail.Store;
+import javax.mail.Folder;
+import javax.mail.Message;
+import javax.mail.internet.MimeMessage;
+
+import org.jvnet.mock_javamail.Mailbox;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.ContextTestSupport;
+import org.springframework.mail.javamail.JavaMailSenderImpl;
+
+/**
+ * Unit test for a special corner case with fetchSize=0
+ */
+public class MailFetchSizeZeroTest extends ContextTestSupport {
+
+    public void testFetchSize() throws Exception {
+        prepareMailbox();
+        Mailbox mailbox = Mailbox.get("james@localhost");
+        assertEquals(5, mailbox.size());
+
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        // no messages expected as we have a fetch size of zero
+        mock.expectedMessageCount(0);
+        // should be done within 2 seconds as no delay when started
+        mock.setResultWaitTime(2000L);
+        mock.assertIsSatisfied();
+
+        assertEquals(5, mailbox.size());
+    }
+
+    private void prepareMailbox() throws Exception {
+        // connect to mailbox
+        JavaMailSenderImpl sender = new JavaMailSenderImpl();
+        Store store = sender.getSession().getStore("pop3");
+        store.connect("localhost", 25, "james", "secret");
+        Folder folder = store.getFolder("INBOX");
+        folder.open(Folder.READ_WRITE);
+
+        // inserts 5 new messages
+        Message[] messages = new Message[5];
+        for (int i = 0; i < 5; i++) {
+            messages[i] = new MimeMessage(sender.getSession());
+            messages[i].setText("Message " + i);
+        }
+        folder.appendMessages(messages);
+        folder.close(true);
+    }
+
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() throws Exception {
+                from("pop3://james@localhost?password=secret&fetchSize=0").to("mock:result");
+            }
+        };
+    }
+
+}

Propchange: activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailFetchSizeZeroTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: activemq/camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailFetchSizeZeroTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date