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 12:00:00 UTC

svn commit: r690170 - in /activemq/camel/trunk/camel-core/src: main/java/org/apache/camel/builder/ main/java/org/apache/camel/converter/stream/ main/java/org/apache/camel/processor/ main/java/org/apache/camel/processor/interceptor/ test/java/org/apache...

Author: ningjiang
Date: Fri Aug 29 02:59:59 2008
New Revision: 690170

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

Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DeadLetterChannelBuilder.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/stream/StreamCache.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/stream/StreamCacheConverter.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/DeadLetterChannel.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/StreamCachingInterceptor.java
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/issues/CacheInputStreamInDeadLetterIssue520Test.java
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/StreamCachingInterceptorTest.java

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DeadLetterChannelBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DeadLetterChannelBuilder.java?rev=690170&r1=690169&r2=690170&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DeadLetterChannelBuilder.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/builder/DeadLetterChannelBuilder.java Fri Aug 29 02:59:59 2008
@@ -63,7 +63,7 @@
     }
 
     public Processor createErrorHandler(RouteContext routeContext, Processor processor) throws Exception {
-        Processor deadLetter = getDeadLetterFactory().createProcessor();
+        Processor deadLetter = getDeadLetterFactory().createProcessor();       
         DeadLetterChannel answer = new DeadLetterChannel(processor, deadLetter, getRedeliveryPolicy(), getLogger(), getExceptionPolicyStrategy());
         configure(answer);
         return answer;

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/stream/StreamCache.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/stream/StreamCache.java?rev=690170&r1=690169&r2=690170&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/stream/StreamCache.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/stream/StreamCache.java Fri Aug 29 02:59:59 2008
@@ -16,6 +16,8 @@
  */
 package org.apache.camel.converter.stream;
 
+import java.io.IOException;
+
 import org.apache.camel.processor.interceptor.StreamCachingInterceptor;
 
 /**
@@ -27,4 +29,9 @@
  */
 public interface StreamCache {
 
+    /**
+     * Resets the StreamCache for a new stream consumption.
+     */
+    void reset() throws IOException;
+
 }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/stream/StreamCacheConverter.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/stream/StreamCacheConverter.java?rev=690170&r1=690169&r2=690170&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/stream/StreamCacheConverter.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/converter/stream/StreamCacheConverter.java Fri Aug 29 02:59:59 2008
@@ -19,7 +19,10 @@
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.Reader;
+import java.io.StringReader;
 
+import javax.xml.transform.Source;
 import javax.xml.transform.TransformerException;
 import javax.xml.transform.stream.StreamSource;
 
@@ -34,34 +37,57 @@
  */
 @Converter
 public class StreamCacheConverter {
-    
+
     private XmlConverter converter = new XmlConverter();
 
     @Converter
-    public StreamCache convertToStreamCache(StreamSource source) throws TransformerException {
-        //TODO: we can probably build a more generic converter method to support other kinds of Sources as well (e.g. SAXSource, StAXSource, ...)
+    public StreamCache convertToStreamCache(Source source) throws TransformerException {
         return new StreamSourceCache(converter.toString(source));
     }
-    
+
     @Converter
     public StreamCache convertToStreamCache(InputStream stream) throws IOException {
         return new InputStreamCache(IOConverter.toBytes(stream));
     }
 
-    private class StreamSourceCache extends StringSource implements StreamCache {
-        
+    @Converter
+    public StreamCache convertToStreamCache(Reader reader) throws IOException {
+        return new ReaderCache(IOConverter.toString(reader));
+    }
+
+    public class StreamSourceCache extends StringSource implements StreamCache {
+
         private static final long serialVersionUID = 4147248494104812945L;
 
         public StreamSourceCache(String text) {
             super(text);
         }
+
+        public void reset() {
+            // do nothing here
+        }
+
     }
-    
+
     public class InputStreamCache extends ByteArrayInputStream implements StreamCache {
-     
+
         public InputStreamCache(byte[] data) {
             super(data);
         }
-        
+
+    }
+
+    public class ReaderCache extends StringReader implements StreamCache {
+
+        public ReaderCache(String s) {
+            super(s);
+        }
+
+        public void close() {
+            // Do not release the string for caching
+        }
+
     }
+
+
 }

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=690170&r1=690169&r2=690170&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 Fri Aug 29 02:59:59 2008
@@ -16,9 +16,13 @@
  */
 package org.apache.camel.processor;
 
+import java.io.IOException;
 import java.io.InputStream;
+import java.io.Reader;
 import java.util.concurrent.RejectedExecutionException;
 
+import javax.xml.transform.Source;
+
 import org.apache.camel.AsyncCallback;
 import org.apache.camel.AsyncProcessor;
 import org.apache.camel.Exchange;
@@ -141,8 +145,7 @@
                 // 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) {
@@ -167,8 +170,7 @@
                 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) {
@@ -299,19 +301,6 @@
         }
     }
 
-    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 {

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/StreamCachingInterceptor.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/StreamCachingInterceptor.java?rev=690170&r1=690169&r2=690170&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/StreamCachingInterceptor.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/interceptor/StreamCachingInterceptor.java Fri Aug 29 02:59:59 2008
@@ -34,8 +34,9 @@
         super();
         setInterceptorLogic(new Processor() {
             public void process(Exchange exchange) throws Exception {
-                Object newBody = exchange.getIn().getBody(StreamCache.class);
+                StreamCache newBody = exchange.getIn().getBody(StreamCache.class);
                 if (newBody != null) {
+                    newBody.reset();
                     exchange.getIn().setBody(newBody);
                 }
                 proceed(exchange);

Modified: 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=690170&r1=690169&r2=690170&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/issues/CacheInputStreamInDeadLetterIssue520Test.java (original)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/issues/CacheInputStreamInDeadLetterIssue520Test.java Fri Aug 29 02:59:59 2008
@@ -18,6 +18,9 @@
 package org.apache.camel.issues;
 
 import java.io.ByteArrayInputStream;
+import java.io.StringReader;
+
+import javax.xml.transform.stream.StreamSource;
 
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.Exchange;
@@ -28,37 +31,60 @@
 public class CacheInputStreamInDeadLetterIssue520Test extends ContextTestSupport {
     private int count;
 
-    public void testException() throws Exception {
+    public void testSendingInputStream() throws Exception {
         count = 0;
         MockEndpoint mock = getMockEndpoint("mock:error");
         mock.expectedMessageCount(1);
 
-        template.sendBody("direct:start", new ByteArrayInputStream("Hello from Willem".getBytes()));
+        template.sendBody("direct:start", new ByteArrayInputStream("<hello>Willem</hello>".getBytes()));
         assertEquals("The message should be delivered 4 times", count, 4);
         mock.assertIsSatisfied();
 
     }
 
+    public void testSendingReader() throws Exception {
+        count = 0;
+        MockEndpoint mock = getMockEndpoint("mock:error");
+        mock.expectedMessageCount(1);
+
+        template.sendBody("direct:start", new StringReader("<hello>Willem</hello>"));
+        assertEquals("The message should be delivered 4 times", count, 4);
+        mock.assertIsSatisfied();
+
+    }
+
+    public void testSendingSource() throws Exception {
+        count = 0;
+        MockEndpoint mock = getMockEndpoint("mock:error");
+        mock.expectedMessageCount(1);
+        StreamSource message = new StreamSource(new StringReader("<hello>Willem</hello>"));
+
+        template.sendBody("direct:start", message);
+        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 {
+                streamCaching();
                 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
+                        // Read the in stream from cache
                         String result = exchange.getIn().getBody(String.class);
-                        assertEquals("Should read the inputstream out again", result, "Hello from Willem");
+                        assertEquals("Should read the inputstream out again", result, "<hello>Willem</hello>");
                         throw new Exception("Forced exception by unit test");
                     }
                 });
 
-                from("direct:errorHandler").process(new Processor() {
+                //Need to set the streamCaching for the deadLetterChannel
+                from("direct:errorHandler").streamCaching().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");
+                        assertEquals("Should read the inputstream out again", result, "<hello>Willem</hello>");
                     }
                 }).to("mock:error");
 

Modified: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/StreamCachingInterceptorTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/StreamCachingInterceptorTest.java?rev=690170&r1=690169&r2=690170&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/StreamCachingInterceptorTest.java (original)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/interceptor/StreamCachingInterceptorTest.java Fri Aug 29 02:59:59 2008
@@ -16,6 +16,8 @@
  */
 package org.apache.camel.processor.interceptor;
 
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
 import java.io.StringReader;
 import java.util.LinkedList;
 import java.util.List;
@@ -53,6 +55,18 @@
 
         assertMockEndpointsSatisifed();
         assertTrue(b.assertExchangeReceived(0).getIn().getBody() instanceof StreamCache);
+        assertEquals(b.assertExchangeReceived(0).getIn().getBody(String.class), "<hello>world!</hello>");
+    }
+
+    public void testConvertInputStreamWithRouteBuilderStreamCaching() throws Exception {
+        a.expectedMessageCount(1);
+
+        InputStream message = new ByteArrayInputStream("<hello>world!</hello>".getBytes());
+        template.sendBody("direct:a", message);
+
+        assertMockEndpointsSatisifed();
+        assertTrue(a.assertExchangeReceived(0).getIn().getBody() instanceof StreamCache);
+        assertEquals(a.assertExchangeReceived(0).getIn().getBody(String.class), "<hello>world!</hello>");
     }
 
     public void testIgnoreAlreadyRereadable() throws Exception {