批处理任务的通用工具类

1、只要是批处理任务(比如下发几万张优惠券,发京豆),就必定用到线程池、不丢包
2、业务场景:下发五万张优惠券,要求一张都不能少。
3、BatchTaskManager

@Service
public class BatchTaskManager {

    private static final int _1W = 10000;

    @Resource
    private ThreadPoolTaskExecutor threadPool;

    public void batchTaskAction() {
        // 查询业务数据,模拟从数据库查询出1W条记录
        List<String> taskList = simulationQueryDB();
        try {
            // 调用工具类批处理任务,这些优惠卷taskList,放入线程池treadPool,做什么业务disposeTask下发
            TaskDisposeUtils.send(taskList, threadPool, TaskDisposeUtils::disposeTask);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }


    private List<String> simulationQueryDB() {
        List<String> data = new ArrayList<>();
        // 模拟查询数据库
        for (int i = 1; i <= 5 * _1W; i++) {
            data.add(String.valueOf(i));
        }
        return data;
    }
}

4、TaskDisposeUtils

public class TaskDisposeUtils {

    public static <T> void send(List<T> taskList, Executor threadPool, Consumer<? super T> consumer) throws InterruptedException {
        if (CollUtil.isEmpty(taskList)) {
            return;
        }
        if (Objects.isNull(consumer)) {
            return;
        }
        CountDownLatch countDownLatch = new CountDownLatch(taskList.size());
        for (T item : taskList) {
            threadPool.execute(() -> {
                try {
                    consumer.accept(item);
                } finally {
                    countDownLatch.countDown();
                }
            });
        }
        countDownLatch.await();
    }

    public static void disposeTask(String task) {
        System.out.println(String.format("【%s发送成功】", task));
    }
}

5、BatchTaskController

@RestController
public class BatchTaskController {

    @Resource
    private BatchTaskManager batchTaskManager;

    // GET http://localhost:8089/batchTask
    @GetMapping("/batchTask")
    public String batchTask() {
        batchTaskManager.batchTaskAction();
        return "本次活动派发流水:" + IdUtil.simpleUUID() + "派发时间:" + DateUtil.now();
    }
}

6.完整版批处理工具类

/**
 * 批处理工具类
 */
public class TaskDisposeUtils {

    /**
     * 用于有入参无返参的情况
     *
     * @param taskList   入参是个list
     * @param threadPool 线程池
     * @param consumer   消费型函数式接口
     * @param <T>        入参类型
     */
    public static <T> void inputListNoReturn(List<T> taskList, Executor threadPool, Consumer<? super T> consumer) throws InterruptedException {
        if (CollUtil.isEmpty(taskList) || Objects.isNull(consumer)) {
            return;
        }
        CountDownLatch countDownLatch = new CountDownLatch(taskList.size());
        for (T item : taskList) {
            threadPool.execute(() -> {
                try {
                    consumer.accept(item);
                } finally {
                    countDownLatch.countDown();
                }
            });
        }
        countDownLatch.await();
    }

    /**
     * 用于有入参有返参的情况
     *
     * @param taskList   入参是个list
     * @param threadPool 线程池
     * @param function   有入参有返参的函数式接口
     * @param <T>        入参类型
     * @param <R>        返参类型
     * @return 返参是根据入参list中的每个内容,各自返回一个list,并放在一个大list里
     */
    public static <T, R> List<R> inputListReturnList(List<T> taskList, Executor threadPool, Function<T, R> function) throws InterruptedException {
        if (CollUtil.isEmpty(taskList) || Objects.isNull(function)) {
            return ListUtil.empty();
        }
        CountDownLatch countDownLatch = new CountDownLatch(taskList.size());
        List<R> rList = new CopyOnWriteArrayList<>();
        for (T item : taskList) {
            threadPool.execute(() -> {
                try {
                    R r = function.apply(item);
                    rList.add(r);
                } finally {
                    countDownLatch.countDown();
                }
            });
        }
        countDownLatch.await();
        return rList;
    }

    public static void disposeTask(String task) {
        System.out.println(String.format("【%s发送成功】", task));
    }

    public static List<String> doWork(String input) {
        return ListUtil.of(input + "_one", input + "_two");
    }
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值