camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r775480 - /camel/trunk/camel-core/src/test/java/org/apache/camel/issues/CustomFailveOverProcessor.java
Date Sat, 16 May 2009 14:50:47 GMT
Author: davsclaus
Date: Sat May 16 14:50:46 2009
New Revision: 775480

URL: http://svn.apache.org/viewvc?rev=775480&view=rev
Log:
Custom failover processor based on camel user forum request

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

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/issues/CustomFailveOverProcessor.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/issues/CustomFailveOverProcessor.java?rev=775480&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/issues/CustomFailveOverProcessor.java
(added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/issues/CustomFailveOverProcessor.java
Sat May 16 14:50:46 2009
@@ -0,0 +1,95 @@
+/**
+ * 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.IOException;
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.builder.RouteBuilder;
+
+/**
+ * Based on user forum request how to do failover with Camel 1.x
+ *
+ * @version $Revision$
+ */
+public class CustomFailveOverProcessor extends ContextTestSupport {
+
+    public void testFailOver() throws Exception {
+        getMockEndpoint("mock:a").expectedMessageCount(1);
+        getMockEndpoint("mock:b").expectedMessageCount(1);
+
+        String out = template.requestBody("direct:start", "Hello World", String.class);
+
+        assertMockEndpointsSatisfied();
+
+        assertEquals("Bye World", out);
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start")
+                    .process(new MyFailOverProcessor(template, "direct:a", "direct:b"));
+
+                // always fail
+                from("direct:a").to("mock:a").process(new Processor() {
+                    public void process(Exchange exchange) throws Exception {
+                        throw new IOException("Forced");
+                    }
+                });
+
+                // return a constant response
+                from("direct:b").to("mock:b").transform(constant("Bye World"));
+            }
+        };
+    }
+
+    private class MyFailOverProcessor implements Processor {
+
+        private List<String> uris;
+        private ProducerTemplate template;
+
+        public MyFailOverProcessor(ProducerTemplate template, String... uris) {
+            this.template = template;
+            this.uris = Arrays.asList(uris);
+        }
+
+        public void process(Exchange exchange) throws Exception {
+            for (String uri : uris) {
+                // reset exception to avoid it being a problem if/when we retry
+                exchange.setException(null);
+
+                // send the exchange to the next uri in the failover list
+                template.send(uri, exchange);
+
+                if (exchange.getException() == null) {
+                    // processed okay so we are finished
+                    return;
+                }
+            }
+        }
+
+    }
+    
+}

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

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



Mime
View raw message