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 2009/08/28 14:14:22 UTC

svn commit: r808857 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/impl/DefaultMessage.java main/java/org/apache/camel/impl/MessageSupport.java test/java/org/apache/camel/processor/PipelineTest.java

Author: ningjiang
Date: Fri Aug 28 12:14:21 2009
New Revision: 808857

URL: http://svn.apache.org/viewvc?rev=808857&view=rev
Log:
CAMEL-1955 fixed the MessageSupport.copyFrom() issue

Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultMessage.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/impl/MessageSupport.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/processor/PipelineTest.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultMessage.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultMessage.java?rev=808857&r1=808856&r2=808857&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultMessage.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultMessage.java Fri Aug 28 12:14:21 2009
@@ -31,7 +31,7 @@
  * <p/>
  * This implementation uses a {@link org.apache.camel.util.CaseInsensitiveMap} storing the headers.
  * This allows us to be able to lookup headers using case insensitive keys, making it easier for end users
- * as they do not have to be worried about using excact keys.
+ * as they do not have to be worried about using exact keys.
  * See more details at {@link org.apache.camel.util.CaseInsensitiveMap}.
  *
  * @version $Revision$

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/MessageSupport.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/MessageSupport.java?rev=808857&r1=808856&r2=808857&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/MessageSupport.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/MessageSupport.java Fri Aug 28 12:14:21 2009
@@ -131,9 +131,11 @@
     public void copyFrom(Message that) {
         setMessageId(that.getMessageId());
         setBody(that.getBody());
+        getHeaders().clear();
         if (that.hasHeaders()) {
             getHeaders().putAll(that.getHeaders());
         }
+        getAttachments().clear();
         if (that.hasAttachments()) {
             getAttachments().putAll(that.getAttachments());
         }

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/PipelineTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/PipelineTest.java?rev=808857&r1=808856&r2=808857&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/PipelineTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/PipelineTest.java Fri Aug 28 12:14:21 2009
@@ -16,8 +16,12 @@
  */
 package org.apache.camel.processor;
 
+import javax.activation.DataHandler;
+import javax.activation.FileDataSource;
+
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.Exchange;
+import org.apache.camel.ExchangePattern;
 import org.apache.camel.Message;
 import org.apache.camel.Processor;
 import org.apache.camel.builder.RouteBuilder;
@@ -119,6 +123,21 @@
         assertEquals(3, exchange.getOut().getHeader("copy-counter"));  
     }
     
+    public void testCopyInOutExchange() {
+        Exchange exchange = template.request("direct:start", new Processor() {
+            public void process(Exchange exchange) {
+                exchange.setPattern(ExchangePattern.InOut);
+                exchange.getIn().setBody("test");
+            }
+        });
+        assertEquals("There should have no message header", 0, exchange.getOut().getHeaders().size());
+        assertEquals("There should have no attachments", 0, exchange.getOut().getAttachments().size());
+        assertEquals("Get a wrong message body", "test", exchange.getOut().getBody());
+        assertNull(exchange.getOut().getHeader("test"));
+        assertNull(exchange.getOut().getAttachment("test1.xml"));
+        
+    }
+    
     @Override
     protected void setUp() throws Exception {
         super.setUp();
@@ -151,6 +170,25 @@
                 from("direct:b").process(new InToOut()).process(new InToOut()).process(new InToOut());
                 // Create a route that uses the  InToFault processor.. the last InToOut will not be called since the Fault occurs before.
                 from("direct:c").process(new InToOut()).process(new InToFault()).process(new InToOut());
+                
+                from("direct:start")
+                    .process(new Processor() {
+                        public void process(Exchange exchange) throws Exception {
+                            exchange.getOut().copyFrom(exchange.getIn());
+                            //Added the header and attachment
+                            exchange.getOut().setHeader("test", "testValue");
+                            exchange.getOut().addAttachment("test1.xml", new DataHandler(new FileDataSource("pom.xml")));
+                        }
+                    })
+                    .process(new Processor() {
+                        public void process(Exchange exchange) throws Exception {
+                            exchange.getOut().copyFrom(exchange.getIn());
+                            assertNotNull("The test attachment should not be null", exchange.getOut().getAttachment("test1.xml"));
+                            assertNotNull("The test header should not be null", exchange.getOut().getHeader("test"));
+                            exchange.getOut().removeAttachment("test1.xml");
+                            exchange.getOut().removeHeader("test");
+                        }
+                    });
             }
         };
     }



Re: svn commit: r808857 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/impl/DefaultMessage.java main/java/org/apache/camel/impl/MessageSupport.java test/java/org/apache/camel/processor/PipelineTest.java

Posted by Willem Jiang <wi...@gmail.com>.
Hi Claus,

Thanks for the suggestion, I will commit the a quick fix for better 
performance.

Willem

Claus Ibsen wrote:
> Hi
> 
> Please try to avoid using getHeaders().clear() in which is not needed
> as it cost to much object creation not needed and in the longer run
> performance as well.
> 
> Hence why there is the hasXXX methods.
> So check with hasXXX before invoking getHeaders() otherwise they are
> lazily created and often that is not needed.
> 
> 

Re: svn commit: r808857 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/impl/DefaultMessage.java main/java/org/apache/camel/impl/MessageSupport.java test/java/org/apache/camel/processor/PipelineTest.java

Posted by Claus Ibsen <cl...@gmail.com>.
Hi

Please try to avoid using getHeaders().clear() in which is not needed
as it cost to much object creation not needed and in the longer run
performance as well.

Hence why there is the hasXXX methods.
So check with hasXXX before invoking getHeaders() otherwise they are
lazily created and often that is not needed.



On Fri, Aug 28, 2009 at 2:14 PM, <ni...@apache.org> wrote:
> Author: ningjiang
> Date: Fri Aug 28 12:14:21 2009
> New Revision: 808857
>
> URL: http://svn.apache.org/viewvc?rev=808857&view=rev
> Log:
> CAMEL-1955 fixed the MessageSupport.copyFrom() issue
>
> Modified:
>    camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultMessage.java
>    camel/trunk/camel-core/src/main/java/org/apache/camel/impl/MessageSupport.java
>    camel/trunk/camel-core/src/test/java/org/apache/camel/processor/PipelineTest.java
>
> Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultMessage.java
> URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultMessage.java?rev=808857&r1=808856&r2=808857&view=diff
> ==============================================================================
> --- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultMessage.java (original)
> +++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultMessage.java Fri Aug 28 12:14:21 2009
> @@ -31,7 +31,7 @@
>  * <p/>
>  * This implementation uses a {@link org.apache.camel.util.CaseInsensitiveMap} storing the headers.
>  * This allows us to be able to lookup headers using case insensitive keys, making it easier for end users
> - * as they do not have to be worried about using excact keys.
> + * as they do not have to be worried about using exact keys.
>  * See more details at {@link org.apache.camel.util.CaseInsensitiveMap}.
>  *
>  * @version $Revision$
>
> Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/MessageSupport.java
> URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/MessageSupport.java?rev=808857&r1=808856&r2=808857&view=diff
> ==============================================================================
> --- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/MessageSupport.java (original)
> +++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/MessageSupport.java Fri Aug 28 12:14:21 2009
> @@ -131,9 +131,11 @@
>     public void copyFrom(Message that) {
>         setMessageId(that.getMessageId());
>         setBody(that.getBody());
> +        getHeaders().clear();
>         if (that.hasHeaders()) {
>             getHeaders().putAll(that.getHeaders());
>         }
> +        getAttachments().clear();
>         if (that.hasAttachments()) {
>             getAttachments().putAll(that.getAttachments());
>         }
>
> Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/PipelineTest.java
> URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/PipelineTest.java?rev=808857&r1=808856&r2=808857&view=diff
> ==============================================================================
> --- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/PipelineTest.java (original)
> +++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/PipelineTest.java Fri Aug 28 12:14:21 2009
> @@ -16,8 +16,12 @@
>  */
>  package org.apache.camel.processor;
>
> +import javax.activation.DataHandler;
> +import javax.activation.FileDataSource;
> +
>  import org.apache.camel.ContextTestSupport;
>  import org.apache.camel.Exchange;
> +import org.apache.camel.ExchangePattern;
>  import org.apache.camel.Message;
>  import org.apache.camel.Processor;
>  import org.apache.camel.builder.RouteBuilder;
> @@ -119,6 +123,21 @@
>         assertEquals(3, exchange.getOut().getHeader("copy-counter"));
>     }
>
> +    public void testCopyInOutExchange() {
> +        Exchange exchange = template.request("direct:start", new Processor() {
> +            public void process(Exchange exchange) {
> +                exchange.setPattern(ExchangePattern.InOut);
> +                exchange.getIn().setBody("test");
> +            }
> +        });
> +        assertEquals("There should have no message header", 0, exchange.getOut().getHeaders().size());
> +        assertEquals("There should have no attachments", 0, exchange.getOut().getAttachments().size());
> +        assertEquals("Get a wrong message body", "test", exchange.getOut().getBody());
> +        assertNull(exchange.getOut().getHeader("test"));
> +        assertNull(exchange.getOut().getAttachment("test1.xml"));
> +
> +    }
> +
>     @Override
>     protected void setUp() throws Exception {
>         super.setUp();
> @@ -151,6 +170,25 @@
>                 from("direct:b").process(new InToOut()).process(new InToOut()).process(new InToOut());
>                 // Create a route that uses the  InToFault processor.. the last InToOut will not be called since the Fault occurs before.
>                 from("direct:c").process(new InToOut()).process(new InToFault()).process(new InToOut());
> +
> +                from("direct:start")
> +                    .process(new Processor() {
> +                        public void process(Exchange exchange) throws Exception {
> +                            exchange.getOut().copyFrom(exchange.getIn());
> +                            //Added the header and attachment
> +                            exchange.getOut().setHeader("test", "testValue");
> +                            exchange.getOut().addAttachment("test1.xml", new DataHandler(new FileDataSource("pom.xml")));
> +                        }
> +                    })
> +                    .process(new Processor() {
> +                        public void process(Exchange exchange) throws Exception {
> +                            exchange.getOut().copyFrom(exchange.getIn());
> +                            assertNotNull("The test attachment should not be null", exchange.getOut().getAttachment("test1.xml"));
> +                            assertNotNull("The test header should not be null", exchange.getOut().getHeader("test"));
> +                            exchange.getOut().removeAttachment("test1.xml");
> +                            exchange.getOut().removeHeader("test");
> +                        }
> +                    });
>             }
>         };
>     }
>
>
>



-- 
Claus Ibsen
Apache Camel Committer

Open Source Integration: http://fusesource.com
Blog: http://davsclaus.blogspot.com/
Twitter: http://twitter.com/davsclaus