You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ni...@apache.org on 2008/08/29 07:51:14 UTC

svn commit: r690114 - in /activemq/camel/trunk/camel-core/src: main/java/org/apache/camel/processor/DeadLetterChannel.java test/java/org/apache/camel/issues/CacheInputStreamInDeadLetterIssue520Test.java

Author: ningjiang
Date: Thu Aug 28 22:51:13 2008
New Revision: 690114

URL: http://svn.apache.org/viewvc?rev=690114&view=rev
Log:
CAMEL-520 Enable stream caching on DeadLetterChannel by default

Added:
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/issues/CacheInputStreamInDeadLetterIssue520Test.java   (with props)
Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/DeadLetterChannel.java

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/DeadLetterChannel.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/DeadLetterChannel.java?rev=690114&r1=690113&r2=690114&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/DeadLetterChannel.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/DeadLetterChannel.java Thu Aug 28 22:51:13 2008
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.processor;
 
+import java.io.InputStream;
 import java.util.concurrent.RejectedExecutionException;
 
 import org.apache.camel.AsyncCallback;
@@ -23,6 +24,8 @@
 import org.apache.camel.Exchange;
 import org.apache.camel.Message;
 import org.apache.camel.Processor;
+import org.apache.camel.converter.stream.StreamCache;
+import org.apache.camel.converter.stream.StreamCacheConverter.InputStreamCache;
 import org.apache.camel.impl.converter.AsyncProcessorTypeConverter;
 import org.apache.camel.model.ExceptionType;
 import org.apache.camel.processor.exceptionpolicy.ExceptionPolicyStrategy;
@@ -138,6 +141,8 @@
                 // must decrement the redelivery counter as we didn't process the redelivery but is
                 // handling by the failure handler. So we must -1 to not let the counter be out-of-sync
                 decrementRedeliveryCounter(exchange);
+                // cache the exchange in message's inputstream
+                cacheInMessageInputStream(exchange);
                 AsyncProcessor afp = AsyncProcessorTypeConverter.convert(data.failureProcessor);
                 boolean sync = afp.process(exchange, new AsyncCallback() {
                     public void done(boolean sync) {
@@ -162,6 +167,8 @@
                 data.redeliveryDelay = data.currentRedeliveryPolicy.sleep(data.redeliveryDelay);
             }
 
+            // cache the exchange in message's inputstream
+            cacheInMessageInputStream(exchange);
             // process the exchange
             boolean sync = outputAsync.process(exchange, new AsyncCallback() {
                 public void done(boolean sync) {
@@ -292,6 +299,20 @@
         }
     }
 
+    private void cacheInMessageInputStream(Exchange exchange) {
+        Object newBody = null;
+        InputStreamCache cache = null;
+        Message in = exchange.getIn();
+        if (in.getBody() instanceof InputStream) {
+            newBody = in.getBody(StreamCache.class);
+            if (newBody != null) {
+                cache = (InputStreamCache) newBody;
+                cache.reset();
+                in.setBody(cache);
+            }
+        }
+    }
+
     @Override
     protected void doStart() throws Exception {
         ServiceHelper.startServices(output, deadLetter);

Added: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/issues/CacheInputStreamInDeadLetterIssue520Test.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/issues/CacheInputStreamInDeadLetterIssue520Test.java?rev=690114&view=auto
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/issues/CacheInputStreamInDeadLetterIssue520Test.java (added)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/issues/CacheInputStreamInDeadLetterIssue520Test.java Thu Aug 28 22:51:13 2008
@@ -0,0 +1,69 @@
+/**
+ * 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.issues;
+
+import java.io.ByteArrayInputStream;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+
+public class CacheInputStreamInDeadLetterIssue520Test extends ContextTestSupport {
+    private int count;
+
+    public void testException() throws Exception {
+        count = 0;
+        MockEndpoint mock = getMockEndpoint("mock:error");
+        mock.expectedMessageCount(1);
+
+        template.sendBody("direct:start", new ByteArrayInputStream("Hello from Willem".getBytes()));
+        assertEquals("The message should be delivered 4 times", count, 4);
+        mock.assertIsSatisfied();
+
+    }
+
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() throws Exception {
+                errorHandler(deadLetterChannel("direct:errorHandler").maximumRedeliveries(3));
+                from("direct:start").process(new Processor() {
+                    public void process(Exchange exchange) throws Exception {
+                        count++;
+                        // Read the inputstream from cache
+                        String result = exchange.getIn().getBody(String.class);
+                        assertEquals("Should read the inputstream out again", result, "Hello from Willem");
+                        throw new Exception("Forced exception by unit test");
+                    }
+                });
+
+                from("direct:errorHandler").process(new Processor() {
+                    public void process(Exchange exchange) throws Exception {
+                        String result = exchange.getIn().getBody(String.class);
+                        assertEquals("Should read the inputstream out again", result, "Hello from Willem");
+                    }
+                }).to("mock:error");
+
+            }
+        };
+    }
+
+}

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

Propchange: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/issues/CacheInputStreamInDeadLetterIssue520Test.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date