SpringBoot 业务中 通过嵌套异步的方式 提高业务表数据同步效率

前言

之前给公安行业做过一个项目,其中有一个需求是建设了一个人员库,这个人员库由各单位录入一些需要长期关注的人员,例如情绪不稳定、游手好闲、曾扬言报复社会等等之类的。当时还应客户的需求,做了一些人员标签功能,共有六类,用来供民警来判断该人员危险程度。这些标签的值,有相关部门提供了接口,我们传入身份证号,接口返回对应数据。最近客户有一个要求,要求整个人员库,至少每天更新一次标签信息,经协商 决定每天0点 通过定时任务刷新一次,因为这时候也没有人使用系统。在这个过程中遇到一些问题,在这里分享下。

表结构

我将表结构业务字端脱敏后分享下,也没有什么复杂的

思路

当时该项目的人员库已经有大概12000+数据了,然后相关部门提供的接口响应时间大概是1-1.5秒,这样如果单线程执行的话,单个人员就大概需要消耗 6 * 1.5 = 9 秒,再进行一些计算及数据库更新,相当于每个人员消耗10秒。刷新一遍大概33+小时,所以这里要进行异步处理。

一开始想到,每个人员的6个接口 使用 CompletableFuture 异步处理,算上网络延迟、数据更新等等,可能单个人员需要2秒,算下来更新一遍也需要6-7个小时,这样肯定不行。而且后面人员库的规模肯定会继续扩大。

最后我的方案是 异步套异步,创建两个线程池:线程池1用于提交每个人员数据,线程池2用于提交每个人员调用接口时的 CompletableFuture,并且 网络请求一般为IO密集型任务,即线程大部分时间是在等待接口响应,所以这里尽量将线程池2的最大线程数设置大一些。

由于业务上,线程池1每提交1个任务,线程池2就提交6个任务,所以我将两个线程池的部分配置项设置为了1:6。代码如下

代码

java 复制代码
/**
 * 人员标签刷新job 每天0点执行一次
 */
@Component
public class PersonTagsRefreshJob {

    @Autowired
    private PersonService personService;

    @Autowired
    private PersonInterfaceService personInterfaceService;

    private static final ThreadPoolExecutor executor1 = new ThreadPoolExecutor(
            Runtime.getRuntime().availableProcessors() * 2,
            Runtime.getRuntime().availableProcessors() * 4,
            1, TimeUnit.MINUTES, new LinkedBlockingQueue<>(100),
            new ThreadPoolExecutor.CallerRunsPolicy()
    );

    private static final ThreadPoolExecutor executor2 = new ThreadPoolExecutor(
            Runtime.getRuntime().availableProcessors() * 12,
            Runtime.getRuntime().availableProcessors() * 24,
            1, TimeUnit.MINUTES, new LinkedBlockingQueue<>(600),
            new ThreadPoolExecutor.CallerRunsPolicy()
    );

    public void execute() {
        for (PersonEntity person : personService.list()) {
            executor1.execute(() -> invokePersonInterface(person));
        }
    }

    private void invokePersonInterface(PersonEntity person) {
        String idCard = person.getIdCard();

        //异步调用人员接口
        CompletableFuture<Boolean> focusFuture = CompletableFuture.supplyAsync(() -> personInterfaceService.isFocus(idCard), executor2);
        CompletableFuture<Boolean> criminalFuture = CompletableFuture.supplyAsync(() -> personInterfaceService.isCriminal(idCard), executor2);
        CompletableFuture<Boolean> fugitiveFuture = CompletableFuture.supplyAsync(() -> personInterfaceService.isFugitive(idCard), executor2);
        CompletableFuture<Boolean> wantedFuture = CompletableFuture.supplyAsync(() -> personInterfaceService.isWanted(idCard), executor2);
        CompletableFuture<Boolean> involvedCaseFuture = CompletableFuture.supplyAsync(() -> personInterfaceService.isInvolvedCase(idCard), executor2);
        CompletableFuture<Boolean> drugFuture = CompletableFuture.supplyAsync(() -> personInterfaceService.isDrug(idCard), executor2);

        //等待所有接口调用完成
        CompletableFuture<Void> futures = CompletableFuture.allOf(focusFuture, criminalFuture, fugitiveFuture, wantedFuture, involvedCaseFuture, drugFuture);

        try {
            futures.get();

            person.setIsFocus(focusFuture.get());
            person.setIsCriminal(criminalFuture.get());
            person.setIsFugitive(fugitiveFuture.get());
            person.setIsWanted(wantedFuture.get());
            person.setIsInvolvedCase(involvedCaseFuture.get());
            person.setIsDrug(drugFuture.get());
            person.setUpdateTime(LocalDateTime.now());

            personService.updateById(person);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

总结

以上就是我的解决方案,运行了一段时间感觉效率还可以,当时12000+的数据10-15分钟可以更新完毕。希望能够帮到有需要的人 谢谢。

相关推荐
蜀山雪松17 分钟前
Windows中Idea或者其他开发工具如何使用Google Sans Code - 码农开源等宽字体
java·ide·intellij-idea
写bug写bug17 分钟前
如何应用服务器端的防御式编程
java·后端·代码规范
_码农121381 小时前
Spring IoC容器与Bean管理
java·后端·spring
元气少女小圆丶1 小时前
Mirror学习笔记
java·开发语言·学习
haruma sen1 小时前
Spring面试
java·spring·面试
孫治AllenSun1 小时前
【Java】使用模板方法模式设计EasyExcel批量导入导出
java·python·模板方法模式
天机️灵韵2 小时前
开源医院信息管理系统:基于若依框架的智慧医疗解决方案
java·开发语言·spring boot·spring cloud·github·开源项目
野生程序员y2 小时前
day23-线程篇(一)
java·开发语言
刃神太酷啦2 小时前
C++ 容器适配器与核心数据结构精解:栈、队列、deque 底层实现与实战应用----《Hello C++ Wrold!》(17)--(C/C++)
java·c语言·数据结构·c++·qt·算法·leetcode
Resean02233 小时前
SpringMVC 6+源码分析(三)DispatcherServlet实例化流程 2--(url 与contrller类如何进行映射)
java·spring boot·spring