【问题标题】:Timeout Rest service超时休息服务
【发布时间】:2015-11-24 22:29:55
【问题描述】:

我正在从事休息服务。该服务使用由 API 抽象出来的专有协议从遗留的第三方系统检索信息,该 API 无法在调用该系统时设置任何类型的超时。随着服务负载的增加,系统会变慢。

如果操作时间过长,有没有办法让服务发送默认响应?任何有关如何解决此问题的建议将不胜感激?

【问题讨论】:

  • 如果你可以修改服务器,你可以为所欲为。如果不能,请在单独的线程中执行调用
  • 什么样的修改?

标签: java rest


【解决方案1】:

您可以将发出 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"; 
}

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-22
      • 2015-08-07
      • 2015-03-15
      • 2012-06-29
      • 2017-07-28
      相关资源
      最近更新 更多