Giter Site home page Giter Site logo

hystrix-context-spring-boot-starter's Introduction

Hystrix Context Spring Boot starter

A Spring Boot starter for making Hystrix context aware

Build Status Coverage Status

Setup

Add the Spring Boot starter to your project:

<dependency>
  <groupId>io.jmnarloch</groupId>
  <artifactId>hystrix-context-spring-boot-starter</artifactId>
  <version>1.0.0</version>
</dependency>

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.

new HystrixCommand<Object>(commandKey()) {
    @Override
    protected Object run() throws Exception {
        return RequestContextHolder.currentRequestAttributes().getAttribute("RequestId", SCOPE_REQUEST);
    }
}.execute();

The same rules applies to your 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:

@Component
public class RequestAttributeAwareCallableWrapper implements HystrixCallableWrapper {

    @Override
    public <T> Callable<T> wrapCallable(Callable<T> callable) {
        return new RequestAttributeAwareCallable<>(callable, RequestContextHolder.currentRequestAttributes());
    }

    private static class RequestAttributeAwareCallable<T> implements Callable<T> {

        private final Callable<T> callable;
        private final RequestAttributes requestAttributes;

        public RequestAttributeAwareCallable(Callable<T> 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

hystrix-context-spring-boot-starter's People

Contributors

jmnarloch avatar

Watchers

James Cloos avatar Indika Munaweera avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.