在 Java 超时时在主要来源和次要来源之间切换

Switch between Primary to Secondary source on Timeout in Java

我正在开发一个 API,它依赖于 两个第三方数据源 - 一个是 SOAP API(主要来源),另一个是 mssql数据库(次要来源)。问题是这两个来源不稳定,影响了生态系统中的 API 和其他 API,因为大量线程长时间等待响应(它正在消耗 JVM 内存)。 我想要一个实现,如果在一定时间后没有响应,主数据源将超时并切换到辅助源并等待响应直到超时,否则会显示一些错误响应。

I have gone through Java concurrent APIs. And found one solution of using ExecutorService#submit(callable) which return a Future and it has a Future#get(5, TimeUnit.SECONDS) method which will block until timeout reached.

  1. Is it recommended using this feature for such critical resource?
  2. Also are there chances that method execution did not start even if timeout reached? Does it guarantee that it would schedule it on priority?

谢谢

确实ExecutorService在这里是正确的选择。

ExecutorService#submit(callable) 其中 return 一个 Future 并且它有一个 Future#get(5, TimeUnit.SECONDS)

尝试和测试。像魅力一样工作。