本文介绍了Java中的超时时,在主要来源到次要来源之间切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am developing an API which is dependent on two third party data sources - One is SOAP API (Primary source) and other is mssql database (secondary source). The problem is these two sources are not stable and which is impacting the API and other APIs in the eco-system due lot of thread waiting for the response for long ( its shooting JVM memory).I want to have an implementation which timeout primary data source after a certain amount of time if not responded and switches to secondary source and wait for response till timeout reaches otherwise show some error response.

  1. 是否建议将此功能用于此类关键资源?
  2. 即使有执行方法也没有开始执行的机会超时了吗?是否保证会优先安排它?

谢谢

推荐答案

在这里Indeed ExecutorService是正确的选择.

Indeed ExecutorService is right choice here.

ExecutorService#submit(callable),它返回一个Future,并且它具有Future#get(5,TimeUnit.SECONDS)

ExecutorService#submit(callable) which return a Future and it has a Future#get(5, TimeUnit.SECONDS)

尝试并测试.像魅力一样工作.

tried and test. Worked like charm.

这篇关于Java中的超时时,在主要来源到次要来源之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-16 12:15