Timeout休息服务



我在休息服务。该服务使用由API抽象的专有协议从遗留的第三方系统检索信息,该API无法在对该系统的调用上设置任何类型的超时。业务负载增加,系统运行速度变慢。

如果操作花费太长时间,是否有方法让服务发送默认响应?对于如何解决这个问题,我将不胜感激。

你可以将发出api请求的代码块包装到另一个线程中,然后使用Future来超时该请求。

这里有一个如何做的例子;

https://stackoverflow.com/a/15120935/975816

超时后

;只要基本实现异常,并在catch块中设置默认响应;

String response;
try { 
  response = future.get(5, TimeUnit.MINUTES); 
}
catch (InterruptedException ie) { 
  /* Handle the interruption. Or ignore it. */
  response = "Default interrupted response";
}
catch (ExecutionException ee) { 
  /* Handle the error. Or ignore it. */
  response = "Default exception response";
}
catch (TimeoutException te) { 
  /* Handle the timeout. Or ignore it. */
  response = "Default timeout response"; 
}

我认为您应该提供更多关于特定专有系统的细节,以便我们更好地了解。从目前提供的信息来看,没有办法从客户端实现控制,因为他只是一个请求者,对服务没有任何控制。

最新更新