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 2019/01/16 15:15:39 UTC

[camel] branch camel-2.x updated: CAMEL-13066: camel-hystrix - Do not fallback on HystrixBadRequestException

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch camel-2.x
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/camel-2.x by this push:
     new 5bc9413  CAMEL-13066: camel-hystrix - Do not fallback on HystrixBadRequestException
5bc9413 is described below

commit 5bc9413a5369e7e918d189c5164e13fd57a82702
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Wed Jan 16 16:13:57 2019 +0100

    CAMEL-13066: camel-hystrix - Do not fallback on HystrixBadRequestException
---
 .../hystrix/processor/HystrixProcessorCommand.java | 13 +++--
 .../processor/HystrixBadRequestExceptionTest.java  | 57 ++++++++++++++++++++++
 2 files changed, 67 insertions(+), 3 deletions(-)

diff --git a/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/processor/HystrixProcessorCommand.java b/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/processor/HystrixProcessorCommand.java
index 842dccd..9275285 100644
--- a/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/processor/HystrixProcessorCommand.java
+++ b/components/camel-hystrix/src/main/java/org/apache/camel/component/hystrix/processor/HystrixProcessorCommand.java
@@ -19,6 +19,7 @@ package org.apache.camel.component.hystrix.processor;
 import java.util.concurrent.atomic.AtomicBoolean;
 
 import com.netflix.hystrix.HystrixCommand;
+import com.netflix.hystrix.exception.HystrixBadRequestException;
 import org.apache.camel.CamelExchangeException;
 import org.apache.camel.Exchange;
 import org.apache.camel.Message;
@@ -136,15 +137,21 @@ public class HystrixProcessorCommand extends HystrixCommand {
                 return null;
             }
 
-            // and copy the result
-            ExchangeHelper.copyResults(exchange, copy);
-
             // execution exception must take precedence over exchange exception
             // because hystrix may have caused this command to fail due timeout or something else
             if (hystrixExecutionException != null) {
                 exchange.setException(new CamelExchangeException("Hystrix execution exception occurred while processing Exchange", exchange, hystrixExecutionException));
             }
 
+            // special for HystrixBadRequestException which should not trigger fallback
+            if (camelExchangeException instanceof HystrixBadRequestException) {
+                LOG.debug("Running processor: {} with exchange: {} done as bad request", processor, exchange);
+                return exchange.hasOut() ? exchange.getOut() : exchange.getIn();
+            }
+
+            // copy the result before its regarded as success
+            ExchangeHelper.copyResults(exchange, copy);
+
             // in case of an exception in the exchange
             // we need to trigger this by throwing the exception so hystrix will execute the fallback
             // or open the circuit
diff --git a/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/processor/HystrixBadRequestExceptionTest.java b/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/processor/HystrixBadRequestExceptionTest.java
new file mode 100644
index 0000000..bff123d
--- /dev/null
+++ b/components/camel-hystrix/src/test/java/org/apache/camel/component/hystrix/processor/HystrixBadRequestExceptionTest.java
@@ -0,0 +1,57 @@
+/**
+ * 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.component.hystrix.processor;
+
+import com.netflix.hystrix.exception.HystrixBadRequestException;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+public class HystrixBadRequestExceptionTest extends CamelTestSupport {
+
+    @Test
+    public void testHystrix() throws Exception {
+        getMockEndpoint("mock:fallback").expectedMessageCount(0);
+        getMockEndpoint("mock:result").expectedBodiesReceived("Hello World");
+        getMockEndpoint("mock:result").expectedPropertyReceived(HystrixConstants.HYSTRIX_RESPONSE_SUCCESSFUL_EXECUTION, true);
+        getMockEndpoint("mock:result").expectedPropertyReceived(HystrixConstants.HYSTRIX_RESPONSE_FROM_FALLBACK, false);
+
+        template.sendBody("direct:start", "Hello World");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start")
+                    .to("log:start")
+                    .hystrix()
+                        .throwException(new HystrixBadRequestException("Should not fallback"))
+                    .onFallback()
+                        .to("mock:fallback")
+                        .transform().constant("Fallback message")
+                    .end()
+                    .to("log:result")
+                    .to("mock:result");
+            }
+        };
+    }
+
+}