Skip to content

Instantly share code, notes, and snippets.

@chenws1012
Last active June 16, 2023 09:54
Show Gist options
  • Save chenws1012/a4ed6cd6a8144d54fb17f4c4f0718307 to your computer and use it in GitHub Desktop.
Save chenws1012/a4ed6cd6a8144d54fb17f4c4f0718307 to your computer and use it in GitHub Desktop.
#HystrixRequestContext
import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestVariableDefault;
import java.util.Map;
/**
* @author chenwenshun@gmail.com on 2023/5/16
*
* In order to pass request context to a FeignClient thread.
*/
public class ServiceContextHolder {
private static final HystrixRequestVariableDefault<Map<String, String>> context = new HystrixRequestVariableDefault<>();
static Map<String, String> getServiceContext() {
initServiceContext();
return context.get();
}
static void setServiceContext(Map<String, String> contexts) {
initServiceContext();
context.set(contexts);
}
static void destroy() {
if (HystrixRequestContext.isCurrentThreadInitialized()) {
HystrixRequestContext.getContextForCurrentThread().shutdown();
}
}
private static void initServiceContext() {
if (!HystrixRequestContext.isCurrentThreadInitialized()) {
HystrixRequestContext.initializeContext();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment