https://github.com/jmnarloch/hystrix-context-spring-boot-starter
Hystrix Context Aware
https://github.com/jmnarloch/hystrix-context-spring-boot-starter
Last synced: 12 days ago
JSON representation
Hystrix Context Aware
- Host: GitHub
- URL: https://github.com/jmnarloch/hystrix-context-spring-boot-starter
- Owner: jmnarloch
- License: apache-2.0
- Created: 2016-07-04T22:17:28.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2018-05-09T11:33:52.000Z (almost 7 years ago)
- Last Synced: 2025-04-03T02:01:41.181Z (about 1 month ago)
- Language: Java
- Size: 74.2 KB
- Stars: 22
- Watchers: 1
- Forks: 10
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Hystrix Context Spring Boot starter
> A Spring Boot starter for making Hystrix context aware
[](https://travis-ci.org/jmnarloch/hystrix-context-spring-boot-starter)
[](https://coveralls.io/github/jmnarloch/hystrix-context-spring-boot-starter?branch=master)## Setup
Add the Spring Boot starter to your project:
```xml
io.jmnarloch
hystrix-context-spring-boot-starter
1.0.0```
## Features
Have you ever wondered why when running your Hystrix Commands the executing thread losses access to the ThreadLocal
dependant functionality?The simple answer is that by default Hystrix will spawn a new thread to execute your code, completely unaware of the
"outer" thread context.For instance fallowing code with fail with an exception because it's going to be executed in thread completely
unaware of the Servlet request.```java
new HystrixCommand(commandKey()) {
@Override
protected Object run() throws Exception {
return RequestContextHolder.currentRequestAttributes().getAttribute("RequestId", SCOPE_REQUEST);
}
}.execute();
```The same rules applies to your [hystrix-javanica](https://github.com/Netflix/Hystrix/tree/master/hystrix-contrib/hystrix-javanica) `@HystrixCommand` annotated methods.
This extension tries to solve this by allowing to register custom Callable wrappers that will be executed prior spawning
new worker thread.### Usage
In order to use this feature all you need to do is register a instance of `HystrixCallableWrapper`.
You may register as many of them as you like, they will be stacked up.Example:
```java
@Component
public class RequestAttributeAwareCallableWrapper implements HystrixCallableWrapper {@Override
public Callable wrapCallable(Callable callable) {
return new RequestAttributeAwareCallable<>(callable, RequestContextHolder.currentRequestAttributes());
}private static class RequestAttributeAwareCallable implements Callable {
private final Callable callable;
private final RequestAttributes requestAttributes;public RequestAttributeAwareCallable(Callable callable, RequestAttributes requestAttributes) {
this.callable = callable;
this.requestAttributes = requestAttributes;
}@Override
public T call() throws Exception {try {
RequestContextHolder.setRequestAttributes(requestAttributes);
return callable.call();
} finally {
RequestContextHolder.resetRequestAttributes();
}
}
}
}
```This will make your Hystrix command aware of the execution context.
## Properties
The only supported property is `hystrix.wrappers.enabled` which allows to disable this extension.
```
hystrix.wrappers.enabled=true # true by default
```## License
Apache 2.0