.andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey("RemoteServiceXFallback")))
My another concern was if there is another better way of doing Hystrix with Spring if I am not using spring boot. I created a new annotations kind of same as of @HystrixCommand of Javanica, but javanica need spring boot, and mine doesnt.
MY ANNOTATIONS
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ExecuteByHystrix {
String groupKey() default "";
String commandKey();
String threadPoolKey() default "";
String fallbackMethod() default "";
int threadPoolSize() default 10;
}
MY ASPECT : still is has some test config data.
@Around("@annotation(com.sd.annotation.ExecuteByHystrix)")
public Object circuitBreakerAround(final ProceedingJoinPoint aJoinPoint) throws Throwable {
final Method method = getMethodFromTarget(aJoinPoint);
Assert.notNull(method, "failed to get method from joinPoint:"+ aJoinPoint);
ExecuteByHystrix annotation = method.getAnnotation(ExecuteByHystrix.class);
final String fallbackMethod = annotation.fallbackMethod().trim();
String commandKey=annotation.commandKey().trim();
if(commandKey.length()==0){
throw new RuntimeException("Please provide Command attributes for @ExecuteByHystrix at "+ method.getClass());
}
String commandGroupKey=annotation.groupKey();
if(null==commandGroupKey|| commandGroupKey.trim().length()==0){
commandGroupKey= aJoinPoint.getSignature().toShortString();
}
HystrixCommand.Setter theSetter =
HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(commandGroupKey));
theSetter = theSetter.andCommandKey(HystrixCommandKey.Factory.asKey(commandKey));
String hystrixPropertyName="hystrix.command."+commandKey+".circuitBreaker.forceOpen";
boolean circuitClose =
DynamicPropertyFactory.getInstance().getBooleanProperty(hystrixPropertyName, false).get();
ConfigurationManager.getConfigInstance().setProperty(hystrixPropertyName, circuitClose);
HystrixCommand theCommand = new HystrixCommand(theSetter) {
@Override
protected Object run() throws Exception {
try {
return aJoinPoint.proceed();
} catch (Exception e) {
throw e;
} catch (Throwable e) {
throw new Exception(e);
}
}
@Override
protected Object getFallback() {
try {
if(null==fallbackMethod || fallbackMethod.length()==0){
System.out.println("There is no fallback method provided, Unable to get Data from "+method.getName());
return null;
}
//String accountId=(String) aJoinPoint.getArgs()[0];
Method method = aJoinPoint.getTarget().getClass().getMethod(fallbackMethod, String.class);
return method.invoke(aJoinPoint.getThis(), aJoinPoint.getArgs());
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
};
return theCommand.execute();
}
Pranav