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分钟可以更新完毕。希望能够帮到有需要的人 谢谢。

相关推荐
FYKJ_201042 分钟前
springboot高校实验室综合管理系统86531-计算机毕业设计、课程设计
java·vue.js·spring boot·后端·python·spring·课程设计
钱栈up1 小时前
AND t.unit_code = 'SM1':一行写死的 SQL 让配料计划页永远拿不到化验值
java·sql·微服务
金銀銅鐵7 小时前
类实例里还有什么——putfield、getfield 与对象传递
java·jvm·后端
织码weavecodes7 小时前
Spring Boot 接入 AI 图像生成:模型适配、图片转存与 Redis 清理
人工智能·spring boot·redis
默 语7 小时前
HarmonyOS7 事件分发控制(二):onTouchIntercept 镂空玻璃卡——圆内自己接,圆外放行
android·java·开发语言
sharemylove9 小时前
Java 异常与常用类 学习笔记
java·开发语言·经验分享·笔记·学习
炸薯条!11 小时前
Python基础学习:文件操作详解(打开、读取、写入与路径)
java·服务器·python
Dreams_l11 小时前
基于SpringBoot实现的抽奖系统(四)
java·spring boot·后端
这个昵称不容易11 小时前
SpringBoot 网上订餐系统设计与实现:80 个 Java 文件、91 个接口,附真实运行截图
java·springboot·毕设
Helix25011 小时前
JDK异常处理最佳实践:受检异常、非受检异常、try-catch-finally与全局异常处理完整指南
java·开发语言·jdk·异常处理·全局异常·自定义异常·try-catch