Hystrix异步调用的java超时
我第一次使用Hystrix async对第三方库进行set
调用,我真的不在乎是否成功发布了该库的数据
public class SetCacheDataCommand extends BaseHystrixCommand {
public SetCacheDataCommand(CacheClient cacheClient, String cacheKey, Entry cacheValue, int timeToLive) {
super(HystrixCommand.Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey(GROUP_NAME))
.andCommandKey(HystrixCommandKey.Factory.asKey(COMMAND_NAME))
.andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
.withExecutionTimeoutInMilliseconds(DEFAULT_HYSTRIX_TIMEOUT_MILLISECONDS)));
this.cacheClient = cacheClient;
this.cacheKey = cacheKey;
this.cacheValue = cacheValue;
this.timeToLive = timeToLive;
}
@Override
protected Object run() throws Exception {
cacheClient.set(cacheKey, cacheValue, timeToLive);
return null;
}
Set
是一个void方法。以下是命令调用:
....Doing something here
new SetCacheDataCommand(cacheClient, cacheKey, cacheValue, 10000).queue();
....Doing something here
我希望上面的代码能够处理这个调用async。但我从Hystrix documentation那里读到了以下内容
Note: Timeouts will fire on HystrixCommand.queue(), even if the caller never calls get() on the resulting Future. Before Hystrix 1.4.0, only calls to get() triggered the timeout mechanism to take effect in such a case.
他们还提到:
Note that there is configuration for turning off timeouts per-command, if that is desired (see command.timeout.enabled).
两个问题:
- 如果我启用超时标志,是否意味着行为将是同步的?如果我关闭超时标志,您是否看到任何问题李>
- 如果池中有20个线程,并且所有这些线程都忙于进行一个异步的Set调用,会发生什么李>
# 1 楼答案
除非在命令上调用
execute
,否则行为不会被阻塞。此外,hystrix执行与调用线程池(如Tomcat或任何服务器线程池)分离,并以并行方式在该上下文中执行。 您应该保持线程隔离timeout
以获得适当的值。另外,我建议给它比缓存客户端套接字超时更多的时间。如果您没有正确地调整它们,您可能需要为该特定命令设置的线程池可能会饱和,并且您会被拒绝您会遇到执行拒绝异常。因此,我可能建议针对您的情况调整
timeouts
和thread pool
。忽略故障,并在需要时让电路中断启动