concept HystrixConcurrencyStrategy in category microservice

This is an excerpt from Manning's book Spring Microservices in Action.
As expected, once the call hits the Hystrix protected method on License-Service.getLicensesByOrder(), you’ll get no value written out for the correlation ID. Fortunately, Hystrix and Spring Cloud offer a mechanism to propagate the parent thread’s context to threads managed by the Hystrix Thread pool. This mechanism is called a HystrixConcurrencyStrategy.
All the examples for the HystrixConcurrencyStrategy can be found in the licensing-service/src/main/java/com/thoughtmechanix/licenses/hystrix package.
Now that you have your HystrixConcurrencyStrategy via the ThreadLocal-AwareStrategy class and your Callable class defined via the DelegatingUserContextCallable class, you need to hook them in Spring Cloud and Hystrix. To do this, you’re going to define a new configuration class. This configuration, called ThreadLocalConfiguration, is shown in the following listing.
package com.thoughtmechanix.licenses.hystrix; //Imports removed for conciseness @Configuration public class ThreadLocalConfiguration { @Autowired(required = false) private HystrixConcurrencyStrategy existingConcurrencyStrategy; #1 @PostConstruct public void init() { // Keeps references of existing Hystrix plugins. HystrixEventNotifier eventNotifier = HystrixPlugins #2 .getInstance() .getEventNotifier(); HystrixMetricsPublisher metricsPublisher = HystrixPlugins .getInstance() .getMetricsPublisher(); HystrixPropertiesStrategy propertiesStrategy = HystrixPlugins .getInstance() .getPropertiesStrategy(); HystrixCommandExecutionHook commandExecutionHook = HystrixPlugins .getInstance() .getCommandExecutionHook(); HystrixPlugins.reset(); HystrixPlugins.getInstance() .registerConcurrencyStrategy( new ThreadLocalAwareStrategy(existingConcurrencyStrategy)); #3 HystrixPlugins.getInstance() .registerEventNotifier(eventNotifier); #4 HystrixPlugins.getInstance() .registerMetricsPublisher(metricsPublisher); HystrixPlugins.getInstance() .registerPropertiesStrategy(propertiesStrategy); HystrixPlugins.getInstance() .registerCommandExecutionHook(commandExecutionHook); } }This Spring configuration class basically rebuilds the Hystrix plugin that manages all the different components running within your service. In the init() method, you’re grabbing references to all the Hystrix components used by the plugin. You then register your custom HystrixConcurrencyStrategy (ThreadLocalAwareStrategy).
HystrixPlugins.getInstance().registerConcurrencyStrategy( new ThreadLocalAwareStrategy(existingConcurrencyStrategy));Remember, Hystrix allows only one HystrixConcurrencyStrategy. Spring will attempt to autowire in any existing HystrixConcurrencyStrategy (if it exists). Finally, when you’re all done, you re-register the original Hystrix components that you grabbed at the beginning of the init() method back with the Hystrix plugin.