Java中的超时时,在主要来源到次要来源之间切换 [英] Switch between Primary to Secondary source on Timeout in Java

查看:64
本文介绍了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.

我经历了Java并发API.并找到了一种解决方案使用 ExecutorService#submit(callable)返回 Future (将来),并且具有 Future#get(5,TimeUnit.SECONDS)方法,该方法将一直阻塞直到超时.

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. 是否建议将此功能用于此类关键资源?
  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中的超时时,在主要来源到次要来源之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆