You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by js...@apache.org on 2007/11/05 13:30:25 UTC

svn commit: r591971 - in /activemq/camel/trunk: camel-core/src/main/java/org/apache/camel/model/ camel-core/src/test/java/org/apache/camel/processor/ components/camel-spring/src/test/java/org/apache/camel/spring/processor/ components/camel-spring/src/t...

Author: jstrachan
Date: Mon Nov  5 04:30:17 2007
New Revision: 591971

URL: http://svn.apache.org/viewvc?rev=591971&view=rev
Log:
added a test case and fix for https://issues.apache.org/activemq/browse/CAMEL-204 to support XML configuration of the Aggregator

Added:
    activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/MyAggregator.java   (with props)
    activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringAggregatorTest.java
      - copied, changed from r591961, activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringChoiceTest.java
    activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringAggregatorWithCustomStrategyTest.java   (with props)
    activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/aggregator-custom-strategy.xml   (with props)
    activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/aggregator.xml
      - copied, changed from r591961, activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/choice.xml
Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/AggregatorType.java
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/AggregatorTest.java

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/AggregatorType.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/AggregatorType.java?rev=591971&r1=591970&r2=591971&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/AggregatorType.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/AggregatorType.java Mon Nov  5 04:30:17 2007
@@ -20,6 +20,7 @@
 
 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlAttribute;
 import javax.xml.bind.annotation.XmlRootElement;
 import javax.xml.bind.annotation.XmlTransient;
 
@@ -41,9 +42,14 @@
 @XmlAccessorType(XmlAccessType.FIELD)
 public class AggregatorType extends ExpressionNode {
     @XmlTransient
-    private AggregationStrategy aggregationStrategy = new UseLatestAggregationStrategy();
-    private int batchSize;
-    private long batchTimeout;
+    private AggregationStrategy aggregationStrategy;
+    @XmlAttribute(required = false)
+    private Integer batchSize;
+    @XmlAttribute(required = false)
+    private Long batchTimeout;
+    @XmlAttribute(required = false)
+    private String strategyRef;
+
     public AggregatorType() {
     }
 
@@ -69,13 +75,20 @@
     public void addRoutes(RouteContext routeContext, Collection<Route> routes) throws Exception {
         Endpoint from = routeContext.getEndpoint();
         final Processor processor = routeContext.createProcessor(this);
+        AggregationStrategy strategy = getAggregationStrategy();
+        if (strategy == null && strategyRef != null) {
+            strategy = routeContext.lookup(strategyRef, AggregationStrategy.class);
+        }
+        if (strategy == null) {
+            strategy = new UseLatestAggregationStrategy();
+        }
         final Aggregator service = new Aggregator(from, processor, getExpression()
-            .createExpression(routeContext), aggregationStrategy);
+                .createExpression(routeContext), strategy);
 
-        if (batchSize != 0) {
+        if (batchSize != null) {
             service.setBatchSize(batchSize);
         }
-        if (batchSize != 0) {
+        if (batchSize != null) {
             service.setBatchTimeout(batchTimeout);
         }
 
@@ -97,30 +110,38 @@
         this.aggregationStrategy = aggregationStrategy;
     }
 
-    public int getBatchSize() {
+    public Integer getBatchSize() {
         return batchSize;
     }
 
-    public void setBatchSize(int batchSize) {
+    public void setBatchSize(Integer batchSize) {
         this.batchSize = batchSize;
     }
 
-    public long getBatchTimeout() {
+    public Long getBatchTimeout() {
         return batchTimeout;
     }
 
-    public void setBatchTimeout(long batchTimeout) {
+    public void setBatchTimeout(Long batchTimeout) {
         this.batchTimeout = batchTimeout;
     }
 
+    public String getStrategyRef() {
+        return strategyRef;
+    }
+
+    public void setStrategyRef(String strategyRef) {
+        this.strategyRef = strategyRef;
+    }
+
     // Fluent API
     //-------------------------------------------------------------------------
-    public AggregatorType batchSize(int batchSize){
+    public AggregatorType batchSize(int batchSize) {
         setBatchSize(batchSize);
         return this;
     }
-    
-    public AggregatorType batchTimeout(long batchTimeout){
+
+    public AggregatorType batchTimeout(long batchTimeout) {
         setBatchTimeout(batchTimeout);
         return this;
     }

Modified: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/AggregatorTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/AggregatorTest.java?rev=591971&r1=591970&r2=591971&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/AggregatorTest.java (original)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/AggregatorTest.java Mon Nov  5 04:30:17 2007
@@ -34,23 +34,17 @@
         // lets send a large batch of messages
         for (int i = 1; i <= messageCount; i++) {
             String body = "message:" + i;
-            template.sendBodyAndHeader("direct:a", body, "cheese", 123);
+            template.sendBodyAndHeader("direct:start", body, "cheese", 123);
         }
 
         resultEndpoint.assertIsSatisfied();
     }
 
-    @Override
-    protected void setUp() throws Exception {
-        super.setUp();
-
-    }
-
     protected RouteBuilder createRouteBuilder() {
         return new RouteBuilder() {
             public void configure() {
                 // START SNIPPET: ex
-                from("direct:a").aggregator(header("cheese")).to("mock:result");
+                from("direct:start").aggregator(header("cheese")).to("mock:result");
                 // END SNIPPET: ex
             }
         };

Added: activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/MyAggregator.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/MyAggregator.java?rev=591971&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/MyAggregator.java (added)
+++ activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/MyAggregator.java Mon Nov  5 04:30:17 2007
@@ -0,0 +1,49 @@
+/**
+ *
+ * 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.spring.processor;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+import org.apache.camel.processor.aggregate.AggregationStrategy;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ * A simple aggregator which just appends the message bodies together
+ *
+ * @version $Revision: 1.1 $
+ */
+public class MyAggregator implements AggregationStrategy {
+    private static final transient Log LOG = LogFactory.getLog(MyAggregator.class);
+
+    public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
+        // lets append the old body to the new body
+        String body = oldExchange.getIn().getBody(String.class);
+        if (body != null) {
+            Message newIn = newExchange.getIn();
+            String newBody = newIn.getBody(String.class);
+            if (newBody != null) {
+                body += " " + newBody;
+            }
+            LOG.debug("**** invoked my strategy with result: " + body);
+
+            newIn.setBody(body);
+        }
+        return newExchange;
+    }
+}

Propchange: activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/MyAggregator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Copied: activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringAggregatorTest.java (from r591961, activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringChoiceTest.java)
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringAggregatorTest.java?p2=activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringAggregatorTest.java&p1=activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringChoiceTest.java&r1=591961&r2=591971&rev=591971&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringChoiceTest.java (original)
+++ activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringAggregatorTest.java Mon Nov  5 04:30:17 2007
@@ -17,14 +17,14 @@
 package org.apache.camel.spring.processor;
 
 import org.apache.camel.CamelContext;
-import org.apache.camel.processor.ChoiceTest;
+import org.apache.camel.processor.AggregatorTest;
 import static org.apache.camel.spring.processor.SpringTestHelper.createSpringCamelContext;
 
 /**
  * @version $Revision: $
  */
-public class SpringChoiceTest extends ChoiceTest {
+public class SpringAggregatorTest extends AggregatorTest {
     protected CamelContext createCamelContext() throws Exception {
-        return createSpringCamelContext(this, "org/apache/camel/spring/processor/choice.xml");
+        return createSpringCamelContext(this, "org/apache/camel/spring/processor/aggregator.xml");
     }
 }

Added: activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringAggregatorWithCustomStrategyTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringAggregatorWithCustomStrategyTest.java?rev=591971&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringAggregatorWithCustomStrategyTest.java (added)
+++ activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringAggregatorWithCustomStrategyTest.java Mon Nov  5 04:30:17 2007
@@ -0,0 +1,47 @@
+/**
+ * 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.spring.processor;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.processor.AggregatorTest;
+import static org.apache.camel.spring.processor.SpringTestHelper.createSpringCamelContext;
+
+/**
+ * @version $Revision: $
+ */
+public class SpringAggregatorWithCustomStrategyTest extends ContextTestSupport {
+
+    public void testSendingMessagesWithCustomAggregator() throws Exception {
+        MockEndpoint resultEndpoint = resolveMandatoryEndpoint("mock:result", MockEndpoint.class);
+
+        resultEndpoint.expectedBodiesReceived("message:1 message:2 message:3");
+
+        // lets send a large batch of messages
+        for (int i = 1; i <= 3; i++) {
+            String body = "message:" + i;
+            template.sendBodyAndHeader("direct:start", body, "cheese", 123);
+        }
+
+        resultEndpoint.assertIsSatisfied();
+    }
+
+    protected CamelContext createCamelContext() throws Exception {
+        return createSpringCamelContext(this, "org/apache/camel/spring/processor/aggregator-custom-strategy.xml");
+    }
+}
\ No newline at end of file

Propchange: activemq/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/SpringAggregatorWithCustomStrategyTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/aggregator-custom-strategy.xml
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/aggregator-custom-strategy.xml?rev=591971&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/aggregator-custom-strategy.xml (added)
+++ activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/aggregator-custom-strategy.xml Mon Nov  5 04:30:17 2007
@@ -0,0 +1,38 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+    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.
+-->
+<beans xmlns="http://www.springframework.org/schema/beans"
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xsi:schemaLocation="
+       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
+       http://activemq.apache.org/camel/schema/spring http://activemq.apache.org/camel/schema/spring/camel-spring.xsd
+    ">
+
+  <!-- START SNIPPET: example -->
+  <camelContext id="camel" xmlns="http://activemq.apache.org/camel/schema/spring">
+    <route>
+      <from uri="direct:start"/>
+      <aggregator strategyRef="aggregatorStrategy">
+        <simple>header.cheese</simple>
+        <to uri="mock:result"/>
+      </aggregator>
+    </route>
+  </camelContext>
+  <!-- END SNIPPET: example -->
+
+  <bean id="aggregatorStrategy" class="org.apache.camel.spring.processor.MyAggregator"/>
+</beans>

Propchange: activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/aggregator-custom-strategy.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Copied: activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/aggregator.xml (from r591961, activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/choice.xml)
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/aggregator.xml?p2=activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/aggregator.xml&p1=activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/choice.xml&r1=591961&r2=591971&rev=591971&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/choice.xml (original)
+++ activemq/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/aggregator.xml Mon Nov  5 04:30:17 2007
@@ -26,19 +26,10 @@
   <camelContext id="camel" xmlns="http://activemq.apache.org/camel/schema/spring">
     <route>
       <from uri="direct:start"/>
-      <choice>
-        <when>
-          <xpath>$foo = 'bar'</xpath>
-          <to uri="mock:x"/>
-        </when>
-        <when>
-          <xpath>$foo = 'cheese'</xpath>
-          <to uri="mock:y"/>
-        </when>
-        <otherwise>
-          <to uri="mock:z"/>
-        </otherwise>
-      </choice>
+      <aggregator>
+        <simple>header.cheese</simple>
+        <to uri="mock:result"/>
+      </aggregator>
     </route>
   </camelContext>
   <!-- END SNIPPET: example -->