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

相关推荐
Forget the Dream23 分钟前
设计模式之迭代器模式
java·c++·设计模式·迭代器模式
MiniFlyZt30 分钟前
消息队列MQ(RabbitMQ)
spring boot·分布式·微服务·rabbitmq
大丈夫在世当日食一鲲30 分钟前
Java中用到的设计模式
java·开发语言·设计模式
A-Kamen35 分钟前
Spring Boot拦截器(Interceptor)与过滤器(Filter)深度解析:区别、实现与实战指南
java·spring boot·后端
练川37 分钟前
Stream特性(踩坑):惰性执行、不修改原始数据源
java·stream
狂奔小菜鸡41 分钟前
Java运行时数据区
java·jvm·后端
trymoLiu1 小时前
SpringBoot 实现 RSA+AES 自动接口解密!
java·spring boot
ChinaRainbowSea1 小时前
MySQL 索引的数据结构(详细说明)
java·数据结构·数据库·后端·mysql
33三 三like1 小时前
软件工程画图题
java·开发语言·软件工程
去看全世界的云2 小时前
【Kotlin】Kotlin基础笔记
android·java·笔记·kotlin