ExecutorService超时响应问题

yingping1898 2016-04-29 02:57:37
基本的场景是这样的:

一个上游请求进来,我们需要同时向外部合作平台(N个)发起N个请求,同时需要在一定时间内得到响应,若超时则弃用。同时将这些响应选择回应给最初的上游请求。

所以选择使用ExecutorService,用
List<Future<Object>> result = es.invokeAll(tasks, TIME_OUT, TimeUnit.MILLISECONDS);

得到执行结果;同时http请求也设置了超时参数:

urlConnection.setConnectTimeout(100);
urlConnection.setReadTimeout(100);


在并发不高的情况下,程序没有问题。但在并发达到1000左右的时候,程序就出问题了。具体表现为:
在设置了TIME_OUT的情况下,偶尔也会出现超时但ExecutorService并没有放弃任务,并在大于TIME_OUT的时间返回执行结果。由于时效性非常高,超时的响应不但无效,而且直接影响了上游请求的响应时效。

现在困惑的是,代码都是在方法内部完成,没有共享变量,因而没有线程安全问题。那么,问题究竟出现在哪里?求解。
...全文
1712 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
dsiori 2016-11-29
  • 打赏
  • 举报
回复
楼主的问题解决了么,我现在也差不多出现这个问题,使用executor.execute方法后,有的时候连接池会没有连接。
tianfang 2016-05-01
  • 打赏
  • 举报
回复
只能建议采用异步方式处理,因为通过超时控制是不好的异常控制机制,因为超时时间不是很准确
  • 打赏
  • 举报
回复
代码最好贴一下,debug看看超时的callable或者runnable是不是有异常影响execute的超时回收。介绍另外一种方式给LZ试试: ExecutorService threadExecutor = Executors.newFixedThreadPool(3); CountDownLatch countDownLatch = new CountDownLatch(3); AuthorisationHistoryTask task1 = new AuthorisationHistoryTask(commonDataThread, countDownLatch ); PreAuthHistoryTask task2 = new PreAuthHistoryTask(userID,sessionID, commonDataThread, countDownLatch ); SettlementHistTask task4 = new SettlementHistTask(commonDataThread,countDownLatch); Future<Map<String, Object>> futureAuthHistory = threadExecutor.submit (task1); Future<Map<String, Object>> futurePreAuthHist = threadExecutor.submit (task2); Future<Map<String, Object>> futureSettleHist = threadExecutor.submit(task4); threadExecutor.shutdown(); try { countDownLatch.await(120L, TimeUnit.SECONDS); }catch (InterruptedException e) { logger.logCommon("InterruptedException",CLASS_NAME); }catch (ExecutionException ex) { logger.logCommon("InterruptedException",CLASS_NAME); }

25,985

社区成员

发帖
与我相关
我的任务
社区描述
高性能WEB开发
社区管理员
  • 高性能WEB开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧