编程语言
首页 > 编程语言> > JUC高并发编程(二)之多线程下载支付宝对账文件

JUC高并发编程(二)之多线程下载支付宝对账文件

作者:互联网

1.背景

在实际开发中,经常会遇到支付需求,当然就会有支付对账的需求....

2.项目结构

3.代码

3.1.线程池配置对象

@Configuration
@EnableAsync
public class ExecutorConfig {
    /**
     * @return
     */
    @Bean
    public Executor asyncServiceExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        //配置核心线程数
        executor.setCorePoolSize(30);
        //配置最大线程数
        executor.setMaxPoolSize(40);
        //配置队列大小
        executor.setQueueCapacity(99999);
        //配置线程池中的线程的名称前缀
        executor.setThreadNamePrefix("async-service-");
        // rejection-policy:当pool已经达到max size的时候,如何处理新任务
        // CALLER_RUNS:不在新线程中执行任务,而是有调用者所在的线程来执行
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        //执行初始化
        executor.initialize();
        return executor;
    }
}

 

3.2.controller层代码

@RestController
public class BillController {
    @Autowired
    private IBillService billService;
    /**
     * 下载对账单
     *
     * @return
     */
    @RequestMapping("/api/downBill")
    public Object downBill() {
        Object result = billService.billDown();
        return "ok::" + result;
    }
}

 

3.3.业务层代码-下载

/**
 * @author 姿势帝-博客园
 * @address https://www.cnblogs.com/newAndHui/
 * @WeChat 851298348
 * @create 10/30 11:27
 * @description <p>
 * 以下载支付宝对账单为案例进行演示
 * 接口文档:https://opendocs.alipay.com/open/02ivbw
 * 关于支付支付课程可以查看:https://www.cnblogs.com/newAndHui/p/14258491.html
 * </p>
 */
@Service
@Slf4j
public class BillServiceImpl implements IBillService {
    @Autowired
    private IOrderService orderService;

    @Override
    public Object billDown() {
        // 以下载支付宝对账单为案例进行演示
        // 这里模拟一个对账文件
        ExcelReader reader = ExcelUtil.getReader(FileUtil.file("F:\\test\\t1.xlsx"));
        // 分页取值,每页10条
        boolean flag = true;
        int pageIndex = 1;
        int pageSize = 10;
        int sum = 0;
        while (flag) {
            int startIndex = pageSize * (pageIndex - 1) + 1;
            int endIndex = startIndex + pageSize - 1;
            List<Map<String, Object>> list = reader.read(0, startIndex, endIndex);
            int size = list.size();
            if (size > 0) {
                sum += size;
                // 异步保存数据
                orderService.saveBillOrder(list);
            }
            if (list.size() < pageSize) {
                flag = false;
            } else {
                pageIndex++;
            }
        }
        return "共:" + sum + "交易记录";
    }
}

 

3.4.业务层代码-保存

@Service
@Slf4j
public class IOrderServiceImpl implements IOrderService {
    @Override
    @Async("asyncServiceExecutor")
    public void saveBillOrder(List<Map<String, Object>> list) {
        int size = list.size();
        log.info("保存数据:" + size);
        int i = 1;
        for (Map<String, Object> bIllOrder : list) {
            log.info("保存第:" + i + "/" + size + ",bIllOrder=" + bIllOrder);
            // 模拟保存耗时(0到5秒)
            ThreadUtil.sleepRandom();
            i++;
            log.info("数据保存完成");
        }
    }
}

 

3.5.对账文件模板

4.测试

    @Test
    void downBill() {
        String url = "http://localhost:8001/api/downBill";
        HttpRequest request = HttpUtil.createGet(url);
        String response = request.execute().body();
        System.out.println(response);
    }

 

测试结果:

5.完美!

标签:JUC,int,list,对账,线程,executor,多线程,public,size
来源: https://www.cnblogs.com/newAndHui/p/15730081.html