有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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).

两个问题:

  1. 如果我启用超时标志,是否意味着行为将是同步的?如果我关闭超时标志,您是否看到任何问题
  2. 如果池中有20个线程,并且所有这些线程都忙于进行一个异步的Set调用,会发生什么

共 (1) 个答案

  1. # 1 楼答案

    If I Keep timeout flag enabled, Does it mean behavior would be synchronous? Do you see any problem if I turn off the timeout flag?

    除非在命令上调用execute,否则行为不会被阻塞。此外,hystrix执行与调用线程池(如Tomcat或任何服务器线程池)分离,并以并行方式在该上下文中执行。 您应该保持线程隔离timeout以获得适当的值。另外,我建议给它比缓存客户端套接字超时更多的时间。如果您没有正确地调整它们,您可能需要为该特定命令设置的线程池可能会饱和,并且您会被拒绝

    What happens if there are let's say 20 thread in the pool and all these threads are busy making a Set call which is async?

    您会遇到执行拒绝异常。因此,我可能建议针对您的情况调整timeoutsthread pool。忽略故障,并在需要时让电路中断启动