异步处理数据量大的请求

业务背景
前端发起请求,后端处理完后(约10W+),进行保存,后端还未处理成功,前端就报了超时(这里不对前端的请求时间做修改),但是其实后端一直都在保存,导致用户体验及其不佳。

实施概述
通过@Async注解,使前端发起请求后,立刻【伪完成】(loading也没有,能直接进行其他操作),但是此时后端在处理。

核心代码
参考的是form服务的异步下载,不对的地方望指正。

1、PropertiesEvent

public class PropertiesEvent extends ApplicationEvent {
    public PropertiesEvent(Object source) {
        super(source);
    }
}

2、

/**
 * 异步任务执行配置
 */
@EnableAsync(proxyTargetClass = true)
@EnableConfigurationProperties(FormAsyncTaskProperties.class)
@Configuration
public class FormAsyncTaskConfig implements ApplicationListener<PropertiesEvent> {
    @Autowired
    private FormAsyncTaskProperties formAsyncTaskProperties;

    private boolean isInit=false;

    private void resetExecutor(){
        ThreadPoolTaskExecutor executor= SpringUtil.getBean("compulsoryCourseExecutor");
        String corePoolJson = formAsyncTaskProperties.getCorePoolJson();
        String [] corePoolJsons= corePoolJson.split("[|]");
        executor.setCorePoolSize(Integer.parseInt(corePoolJsons[0]));
        executor.setMaxPoolSize(Integer.parseInt(corePoolJsons[1]));
        executor.setQueueCapacity(Integer.parseInt(corePoolJsons[2]));
        executor.setThreadNamePrefix("compulsoryCourseExecutor_");
    }

    @Bean("compulsoryCourseExecutor")
    public TaskExecutor taskExecutor() {

        ThreadPoolTaskExecutor executor = new CustomThreadPoolTaskExecutor();
        String corePoolJson = formAsyncTaskProperties.getCorePoolJson();
        String [] corePoolJsons= corePoolJson.split("[|]");
        executor.setCorePoolSize(Integer.parseInt(corePoolJsons[0]));
        executor.setMaxPoolSize(Integer.parseInt(corePoolJsons[1]));
        executor.setQueueCapacity(Integer.parseInt(corePoolJsons[2]));
        //线程池前缀
        executor.setThreadNamePrefix("compulsoryCourseExecutor_");
        /*
           rejection-policy:当pool已经达到max size的时候,如何处理新任务
           CALLER_RUNS:不在新线程中执行任务,而是有调用者所在的线程来执行
        */
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
        this.isInit=true;
        return executor;
    }

    @Override
    public void onApplicationEvent(PropertiesEvent propertiesEvent) {
        if(this.isInit){
            this.resetExecutor();
        }
    }
}

3、FormAsyncTaskProperties

@ConfigurationProperties(prefix="compulsorycourseasynctask")
@RefreshScope
public class FormAsyncTaskProperties {
    /**
     *  线程池维护线程的最小数量|最大数量|队列最大长度.
     */
    private String corePoolJson="5|20|50";

    public String getCorePoolJson() {
        return corePoolJson;
    }

    public void setCorePoolJson(String corePoolJson) {
        this.corePoolJson = corePoolJson;
        SpringUtil.publishEvent(new PropertiesEvent(this));
    }
}

4、需要在服务的启动类加上@EnableAsync注解。

5、在具体的业务代码的serviceImpl加上@Async(value = “compulsoryCourseExecutor”)

如 XXXserviceImpl

//具体的方法
@Async(value = "compulsoryCourseExecutor")
    public JsonResult handleCourseCompulsory() {
        JsonResult result = new JsonResult(true);
        List userDtoList = new ArrayList<>();
     }

6、若是大批量保存实体,可使用

this.saveBatch(entity,1000);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

sunywz

~

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

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

打赏作者

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

抵扣说明:

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

余额充值