java使用Future实现多线程执行和结果聚合

文章目录

场景

  • 网站智能问答场景,需要对多个分类查询,结果聚合展示
  • 由于每种分类都有自己的业务逻辑,有的需要查询数据库中间库,有的需要查询elasticsearch搜索引擎,有的需要调用第三方接口,数据查询要分开进行,没法一次查询搞定
  • 实际上这几个查询不相关,可以同时进行,现在串行,使该场景下,智能问答返回较慢

解决

  • 最简单的逻辑,肯定就是java多线程,将串行改为并行
  • 这样查询返回时间,就取决于最慢的一个查询,返回时间大大缩短
  • 页面返回一般要求三秒内,实际项目上我们要求1秒内返回,多线程解决了这个问题
  • 下面上代码,部分截取
	@Autowired
    private ThreadPoolTaskExecutor taskExecutor;
// 新闻查询
            SolrPageQueryVO newsQueryVO = new SolrPageQueryVO();
            BeanUtil.copyProperties(vo, newsQueryVO);
            newsQueryVO.setAllSite(vo.getAllSite());
            newsQueryVO.setTypeCode(SolrPageQueryVO.TypeCode.articleNews.toString().concat(",")
                    .concat(SolrPageQueryVO.TypeCode.pictureNews.toString())
                    .concat(",").concat(SolrPageQueryVO.TypeCode.videoNews.toString()));
            Future<?> newsFuture = taskExecutor.submit(()->selectForAsk(map, sumMap, newsQueryVO, "news", context));

            //网上服务
            Future<?> workGuideFuture = taskExecutor.submit(()->selectForAsk(map, sumMap, vo, "workGuide", context));

            //留言
            SolrPageQueryVO messageBoardQueryVO = new SolrPageQueryVO();
            BeanUtil.copyProperties(vo, messageBoardQueryVO);
            messageBoardQueryVO.setAllSite(vo.getAllSite());
            messageBoardQueryVO.setTypeCode(SolrPageQueryVO.TypeCode.messageBoard.toString());
            Future<?> messageBoardFuture = taskExecutor.submit(()->selectForAsk(map, sumMap, messageBoardQueryVO, "messageBoard", context));

            //信息公开(isAllSite为true时,搜索所有集合,不区分集合和站点,只根据dn搜索,有区分需要的项目可以重写SearchEsServiceImpl类)
            SolrPageQueryVO publicContentQueryVO = new SolrPageQueryVO();
            BeanUtil.copyProperties(vo, publicContentQueryVO);
            publicContentQueryVO.setAllSite(vo.getAllSite());
            publicContentQueryVO.setTypeCode(SolrPageQueryVO.TypeCode.public_content.toString());
            Future<?> publicContentFuture = taskExecutor.submit(()->selectForAsk(map, sumMap, publicContentQueryVO, "public_content", context));


            //问答知识库(isAllSite为true时,搜索所有集合,不区分集合和站点,有区分需要的项目可以重写或传false)
            SolrPageQueryVO knowledgeBaseQueryVO = new SolrPageQueryVO();
            BeanUtil.copyProperties(vo, knowledgeBaseQueryVO);
            knowledgeBaseQueryVO.setAllSite(vo.getAllSite());
            knowledgeBaseQueryVO.setTypeCode(SolrPageQueryVO.TypeCode.knowledgeBase.toString());
            Future<?> knowledgeBaseFuture = taskExecutor.submit(()->selectForAsk(map, sumMap, knowledgeBaseQueryVO, "knowledgeBase", context));

            try {
                knowledgeBaseFuture.get();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                messageBoardFuture.get();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                newsFuture.get();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                publicContentFuture.get();
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                workGuideFuture.get();
            } catch (Exception e) {
                e.printStackTrace();
            }

            tabcount = sumMap.values().size();
            map.put("tabcount", tabcount);
            map.put("numMap", sumMap);
 private void selectForAsk(Map<String, Object> map, Map<String, Object> sumMap, SolrPageQueryVO vo, String type, Context context) {
        if ("news".equals(type)) {
            try {
                // do something
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if ("workGuide".equals(type)) {
            try {
                //网上办事查询调用接口
               // do something
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if ("messageBoard".equals(type)) {
            try {
                // do something
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if ("public_content".equals(type)) {
            try {
                Long queryCount = SearchQueryHolder.queryCount(vo);
                // do something
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if ("knowledgeBase".equals(type)) {
            try {
                // do something
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

坚持是一种态度

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值