【发布时间】:2018-05-27 21:49:15
【问题描述】:
我第一次使用 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
我希望上面的代码能够处理这个调用异步。但我从 Hystrix documentation 阅读了以下内容。
注意:超时将在 HystrixCommand.queue() 上触发,即使调用者 永远不会在生成的 Future 上调用 get()。在 Hystrix 1.4.0 之前,只有 对 get() 的调用触发了超时机制在这种情况下生效 一个案例。
他们还提到:
请注意,每个命令都有关闭超时的配置, 如果需要(请参阅 command.timeout.enabled)。
几个问题:
- 如果我保持启用超时标志,这是否意味着行为将是同步的?如果我关闭超时标志,您是否发现任何问题?
- 如果池中有 20 个线程并且所有这些线程都忙于进行异步 Set 调用,会发生什么情况?
【问题讨论】:
标签: java multithreading asynchronous hystrix