Author: janstey
Date: Thu Sep 18 15:56:21 2008
New Revision: 696845

URL: http://svn.apache.org/viewvc?rev=696845&view=rev
Log:
CAMEL-501 - Save exception thrown on poll() and thrown it on shutdown.

Added:
    
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/MockScheduledPollConsumer.java
   (with props)
    
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/ScheduledPollConsumerTest.java
   (with props)
Modified:
    
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ScheduledPollConsumer.java

Modified: 
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ScheduledPollConsumer.java
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ScheduledPollConsumer.java?rev=696845&r1=696844&r2=696845&view=diff
==============================================================================
--- 
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ScheduledPollConsumer.java
 (original)
+++ 
activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ScheduledPollConsumer.java
 Thu Sep 18 15:56:21 2008
@@ -41,7 +41,8 @@
     private TimeUnit timeUnit = TimeUnit.MILLISECONDS;
     private boolean useFixedDelay;
     private ScheduledFuture<?> future;
-
+    private Exception firstExceptionThrown;
+    
     public ScheduledPollConsumer(DefaultEndpoint<E> endpoint, Processor 
processor) {
         this(endpoint, processor, endpoint.getExecutorService());
     }
@@ -64,8 +65,10 @@
         try {
             poll();
         } catch (Exception e) {
-            // TODO: We should not swallow this but handle it better. See 
CAMEL-501
             LOG.warn("An exception occured while polling: " + 
this.getEndpoint() + ": " + e.getMessage(), e);
+            if (firstExceptionThrown == null) {
+                firstExceptionThrown = e;
+            } 
         }
     }
 
@@ -109,12 +112,13 @@
     /**
      * The polling method which is invoked periodically to poll this consumer
      * 
-     * @throws Exception can be thrown if an exception occured during polling
+     * @throws Exception can be thrown if an exception occurred during polling
      */
     protected abstract void poll() throws Exception;
 
     @Override
     protected void doStart() throws Exception {
+        firstExceptionThrown = null;
         super.doStart();
         if (isUseFixedDelay()) {
             future = executor.scheduleWithFixedDelay(this, getInitialDelay(), 
getDelay(), getTimeUnit());
@@ -129,5 +133,9 @@
             future.cancel(false);
         }
         super.doStop();
+        
+        if (firstExceptionThrown != null) {
+            throw firstExceptionThrown;
+        }
     }
 }

Added: 
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/MockScheduledPollConsumer.java
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/MockScheduledPollConsumer.java?rev=696845&view=auto
==============================================================================
--- 
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/MockScheduledPollConsumer.java
 (added)
+++ 
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/MockScheduledPollConsumer.java
 Thu Sep 18 15:56:21 2008
@@ -0,0 +1,45 @@
+/**
+ * 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.impl;
+
+import java.util.concurrent.ScheduledThreadPoolExecutor;
+
+import org.apache.camel.Processor;
+
+public class MockScheduledPollConsumer extends ScheduledPollConsumer {
+
+    private Exception exceptionToThrowOnPoll;
+
+    public MockScheduledPollConsumer(DefaultEndpoint endpoint, Processor 
processor) {
+        super(endpoint, processor);
+    }
+
+    // dummy constructor here - we just want to test the run() method, which 
calls poll()   
+    public MockScheduledPollConsumer(Exception exceptionToThrowOnPoll) {
+        super(null, null, new ScheduledThreadPoolExecutor(1));
+        this.exceptionToThrowOnPoll = exceptionToThrowOnPoll;
+    }
+    
+    @Override
+    protected void poll() throws Exception {
+        if (exceptionToThrowOnPoll != null) {
+            throw exceptionToThrowOnPoll;
+        }
+    }
+
+}

Propchange: 
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/MockScheduledPollConsumer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/ScheduledPollConsumerTest.java
URL: 
http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/ScheduledPollConsumerTest.java?rev=696845&view=auto
==============================================================================
--- 
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/ScheduledPollConsumerTest.java
 (added)
+++ 
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/ScheduledPollConsumerTest.java
 Thu Sep 18 15:56:21 2008
@@ -0,0 +1,63 @@
+/**
+ * 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.impl;
+
+import junit.framework.Assert;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+
+public class ScheduledPollConsumerTest extends ContextTestSupport {
+    
+    public void testExceptionOnPollGetsThrownOnShutdown() throws Exception {
+        Exception expectedException = new Exception("Hello, I should be thrown 
on shutdown only!");
+        Exception actualException = null;
+        MockScheduledPollConsumer consumer = new 
MockScheduledPollConsumer(expectedException);
+
+        consumer.start();
+        // exception is caught and saved
+        consumer.run(); 
+        
+        try {
+            // exception should be thrown
+            consumer.stop();           
+        } catch (Exception e) {
+            actualException = e;
+        }
+        
+        // make sure its the right exception!
+        Assert.assertEquals(expectedException, actualException);
+    }
+    
+    public void testNoExceptionOnPollAndNoneThrownOnShutdown() throws 
Exception {
+        Exception actualException = null;
+        MockScheduledPollConsumer consumer = new 
MockScheduledPollConsumer(null);
+
+        consumer.start();
+        consumer.run(); 
+        
+        try {
+            // exception should not be thrown
+            consumer.stop();           
+        } catch (Exception e) {
+            actualException = e;
+        }
+        
+        // make sure no exception was thrown
+        Assert.assertEquals(null, actualException);
+    }
+}

Propchange: 
activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/ScheduledPollConsumerTest.java
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to