java利用Future实现多线程执行与结果聚合实例代码
作者:坚持是一种态度 发布时间:2023-09-24 11:07:18
标签: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();
}
}
}
来源:https://blog.csdn.net/u010882234/article/details/122127152
0
投稿
猜你喜欢
- 本文实例讲述了Java Lambda表达式。分享给大家供大家参考,具体如下:一 点睛Lambda表达式支持将代码块作为方法参数,Lambda
- 前言想使用ffmpeg打开摄像头,需要输入摄像头的名称,而ffmpeg本身的枚举摄像头列表功能不是接口,所以需要用其他方式获取到设备列表。C
- 目录1.前言2.不同进制的特点3.进制之间的转换3.1 二进制转十进制:3.2 十进制转二进制:3.3 二进制转八进制:3.4 十六进制转二
- 前言通过此篇文章,你将了解到:Flutter windows和Android桌面应用屏幕适配的解决方案;屏幕适配的相关知识和原理;flutt
- 每个使用Android手机的人应该对Android中的通知不陌生,下面我们就学习一下怎么使用Android中的通知。一、通知的基本用法活动、
- 使用了RecyclerView嵌套RecyclerView的方案。购物车的第一个界面为RecyclerView,每个Item里面包含一个店铺
- 引言Random类是非常值得学习的一个类,所以我们今天一起学习一下Random这个类,对于模拟数据这个是随机类可是一个好东西,我们可以用这个
- 今天就给大家分享android实现支付宝手势密码,很常见,像现在用微信支付,支付宝支付的时候都要自己设置的4位PIN码,然后输入PIN码后立
- 前言一个简单的单机小游戏:flypybird ,用来巩固java基础。涉及主要知识点:JFrame 、 JPanel 、 继承、 键盘/鼠标
- 算法分析一个排序算法的好坏,一般是通过下面几个关键信息来分析的,下面先介绍一下这几个关键信息,然后再将常见的排序算法的这些关键信息统计出来。
- 本文介绍如何通过C#程序代码方法将XML文件转换为Word文档,包括转为.doc /.docx等格式。并附VB.NET代码,有需要可供参考。
- 我们在使用SpringData JPA框架时,进行条件查询,如果是固定条件的查询,我们可以使用符合框架规则的自定义方法以及@Query注解实
- 直接用javaw.exe想打开aspectj-1.9.4.jar安装aspectJ选Java™ Platform SE binary提示JV
- 方法的重写(Overriding)和重载(Overloading)是Java多态性的不同表现。重写(Overriding)是父类与子类之间多
- 概述线上项目发布一般有以下几种方案:机发布蓝绿部署滚动部署灰度发布停机发布 这种发布一般在夜里或者进行大版本升级的时候发布,因为需要停机,所
- 负载均衡使用微服务后,为了能够承担高并发的压力,同一个服务可能会启动多个实例。这时候消费者就需要负载均衡,把请求分散到各个实例。负载均衡主要
- 目录一、单例模式饿汉模式懒汉模式懒汉模式二、堵塞队列实现BlockingQueue三、定时器总结一、单例模式单例模式是一种设计模式,针对一些
- 1 读取操作系统和CLR的版本OperatingSystem os = System.Environment.OSVersion; Cons
- 一、背景说明由于以前在项目中一直使用sqlmap.xml进行mybatis语句的编写和实现,其xml实现动态更新和查询较为方便,而目前由于技
- 一般而言,在传统的VB6中父子窗体(诸如“闪屏”窗体是“父窗体”加载内容,然后启动自动关闭,启动主窗体,是子窗体)的交互中,我们只需按照这样