You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2009/03/10 08:10:36 UTC

svn commit: r752002 - /camel/trunk/camel-core/src/test/java/org/apache/camel/processor/SplitAggregateInOutTest.java

Author: davsclaus
Date: Tue Mar 10 07:10:35 2009
New Revision: 752002

URL: http://svn.apache.org/viewvc?rev=752002&view=rev
Log:
Splitter with aggregation strategy with InOut for wiki sample

Added:
    camel/trunk/camel-core/src/test/java/org/apache/camel/processor/SplitAggregateInOutTest.java   (with props)

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/SplitAggregateInOutTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/SplitAggregateInOutTest.java?rev=752002&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/SplitAggregateInOutTest.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/SplitAggregateInOutTest.java Tue Mar 10 07:10:35 2009
@@ -0,0 +1,128 @@
+/**
+ * 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.processor;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.impl.JndiRegistry;
+import org.apache.camel.processor.aggregate.AggregationStrategy;
+
+/**
+ * @version $Revision$
+ */
+public class SplitAggregateInOutTest extends ContextTestSupport {
+
+    private String expectedBody = "Response[(id=1,item=A);(id=2,item=B);(id=3,item=C)]";
+
+    public void testSplitAndAggregateInOut() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedBodiesReceived(expectedBody);
+
+        // use requestBody as its InOut
+        Object out = template.requestBody("direct:start", "A@B@C");
+        assertEquals(expectedBody, out);
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected JndiRegistry createRegistry() throws Exception {
+        JndiRegistry jndi = super.createRegistry();
+        jndi.bind("MyOrderService", new MyOrderService());
+        return jndi;
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                // START SNIPPET: e1
+                // this routes starts from the direct:start endpoint
+                // the body is then splitted based on @ separator
+                // the splitter in Camel supports InOut as well and for that we need
+                // to be able to aggregate what response we need to send back, so we provide our
+                // own strategy with the class MyOrderStrategy.
+                from("direct:start")
+                    .split(body().tokenize("@"), new MyOrderStrategy())
+                        // each splitted message is then send to this bean where we can process it
+                        .to("bean:MyOrderService?method=handleOrder")
+                        // this is important to end the splitter route as we do not want to do more routing
+                        // on each splitted message
+                    .end()
+                    // after we have splitted and handled each message we want to send a single combined
+                    // response back to the original caller, so we let this bean build it for us
+                    // this bean will receive the result of the aggregate strategy: MyOrderStrategy
+                    .to("bean:MyOrderService?method=buildCombinedResponse")
+                // END SNIPPET: e1
+                    .to("mock:result");
+            }
+        };
+    }
+
+    // START SNIPPET: e2
+    public static class MyOrderService {
+
+        private static int counter;
+
+        /**
+         * We just handle the order by returning a id line for the order
+         */
+        public String handleOrder(String line) {
+            return "(id=" + ++counter + ",item=" + line + ")";
+        }
+
+        /**
+         * We use the same bean for building the combined response to send
+         * back to the original caller
+         */
+        public String buildCombinedResponse(String line) {
+            return "Response[" + line + "]";
+        }
+    }
+    // END SNIPPET: e2
+
+    // START SNIPPET: e3
+    /**
+     * This is our own order aggregation strategy where we can control
+     * how each splitted message should be combined. As we do not want to
+     * loos any message we copy from the new to the old to preserve the
+     * order lines as long we process them
+     */
+    public static class MyOrderStrategy implements AggregationStrategy {
+
+        public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
+            // put order together in old exchange by adding the order from new exchange
+
+            // copy from OUT as we use InOut pattern
+            String orders = oldExchange.getOut().getBody(String.class);
+            String newLine = newExchange.getOut().getBody(String.class);
+
+            // put orders together separating by semi colon
+            orders = orders + ";" + newLine;
+            // put combined order back on old to preserve it
+            oldExchange.getOut().setBody(orders);
+
+            // return old as this is the one that has all the orders gathered until now
+            return oldExchange;
+        }
+    }
+    // END SNIPPET: e3
+
+}
\ No newline at end of file

Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/SplitAggregateInOutTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/SplitAggregateInOutTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date