Last active
March 31, 2019 04:04
-
-
Save yp-palF/a7a3aa231c8c0e54ee5a3d6237caac05 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Buggy code | |
public class HystrixHook extends HystrixCommandExecutionHook { | |
private Integer id; | |
@Override | |
public <T> void onStart(HystrixInvokable<T> commandInstance) { | |
getThreadLocals(); | |
} | |
@Override | |
public <T> void onExecutionStart(HystrixInvokable<T> commandInstance) { | |
setThreadLocals(); | |
} | |
@Override | |
public <T> void onFallbackStart(HystrixInvokable<T> commandInstance) { | |
setThreadLocals(); | |
} | |
@Override | |
public <T> void onExecutionSuccess(HystrixInvokable<T> commandInstance) { | |
ThreadLocalUtil.clear(); | |
super.onExecutionSuccess(commandInstance); | |
} | |
@Override | |
public <T> Exception onExecutionError(HystrixInvokable<T> commandInstance, Exception e) { | |
ThreadLocalUtil.clear(); | |
return super.onExecutionError(commandInstance, e); | |
} | |
@Override | |
public <T> Exception onFallbackError(HystrixInvokable<T> commandInstance, Exception e) { | |
ThreadLocalUtil.clear(); | |
return super.onFallbackError(commandInstance, e); | |
} | |
@Override | |
public <T> void onFallbackSuccess(HystrixInvokable<T> commandInstance) { | |
ThreadLocalUtil.clear(); | |
super.onFallbackSuccess(commandInstance); | |
} | |
private void getThreadLocals() { | |
id = ThreadLocalUtil.getId(); | |
} | |
private void setThreadLocals() { | |
ThreadLocalUtil.setId(id); | |
} | |
} | |
// Register hystrix hook plugin | |
HystrixPlugins.getInstance().registerCommandExecutionHook(new HystrixHook()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment