[[hystrix-eip]]
== Hystrix EIP

*Available as of Camel 2.18*

The Hystrix EIP provides integration with Netflix link:https://github.com/Netflix/Hystrix[Hystrix] to be used as circuit breaker in the Camel routes. Hystrix is a latency and fault tolerance library designed to isolate points of access to remote systems, services and 3rd party libraries, stop cascading failure and enable resilience in complex distributed systems where failure is inevitable.

Maven users will need to add the following dependency to their pom.xml to use this EIP:

[source]
----
<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-hystrix</artifactId>
    <version>x.x.x</version><!-- use the same version as your Camel core version -->
</dependency>
----

=== Configuration options

// eip options: START
The Hystrix EIP supports 2 options which are listed below:

[width="100%",cols="2,5,^1,2",options="header"]
|===
| Name | Description | Default | Type
| *hystrixConfiguration* | Configures the Hystrix EIP Use end when configuration is complete, to return back to the Hystrix EIP. |  | HystrixConfiguration Definition
| *hystrixConfigurationRef* | Refers to a Hystrix configuration to use for configuring the Hystrix EIP. |  | String
|===
// eip options: END

=== Camel's Error Handler and Hystrix EIP

By default the Hystrix EIP handles errors by itself. This means if the circuit breaker is open and
the message fails, then Camel's error handler is not reacting also. However from *Camel 2.23* onwards
you can enable Camels error handler with Hystrix by enabling the `inheritErrorHandler` option, as shown:

[source,java]
----
// Camel's error handler that will attempt to redeliver the message 3 times
errorHandler(deadLetterChannel("mock:dead").maximumRedeliveries(3).redeliveryDelay(0));

from("direct:start")
    .to("log:start")
    // turn on Camel's error handler on hystrix so it can do redeliveries
    .hystrix().inheritErrorHandler(true)
        .to("mock:a")
        .throwException(new IllegalArgumentException("Forced"))
    .end()
    .to("log:result")
    .to("mock:result");
----

This example is from an unit test, where you can see the Hystrix EIP block has been hardcoded
to always fail by throwning an exception. Because the `inheritErrorHandler` has been enabled,
then Camel's error handler will attempt to call the Hystrix EIP block again.

That means the `mock:a` endpoint will receive the message again, and a total of 1 + 3 = 4 message
(first time + 3 redeliveries).

If we turn off the `inheritErrorHandler` option (default) then the Hystrix EIP will only be
executed once because it handled the error itself.


=== Samples

Below is an example route showing an Hystrix endpoint that protects against slow operation by falling back to the in-lined fallback route. By default the timeout request is just *1000ms* so the HTTP endpoint has to be fairly quick to succeed.
[source,java]
----
from("direct:start")
    .hystrix()
        .to("http://fooservice.com/slow")
    .onFallback()
        .transform().constant("Fallback message")
    .end()
    .to("mock:result");
----

And in XML DSL:
[source,xml]
----
<camelContext xmlns="http://camel.apache.org/schema/spring">
  <route>
    <from uri="direct:start"/>
    <hystrix>
      <to uri="http://fooservice.com/slow"/>
      <onFallback>
        <transform>
          <constant>Fallback message</constant>
        </transform>
      </onFallback>
    </hystrix>
    <to uri="mock:result"/>
  </route>
</camelContext>
----

See link:https://github.com/apache/camel/blob/master/camel-core/src/main/docs/eips/onFallback-eip.adoc[onFallback]

=== Other examples

You can find an example with the source code: link:https://github.com/apache/camel/tree/master/examples/camel-example-hystrix[camel-example-hystrix].
