将数据批量插入数据库

核心代码展示:

@Component
public class AsyncExecutorPoolConfig {
    private static final Logger LOG = LoggerFactory.getLogger(AsyncExecutorPoolConfig.class);

    @Value("${peroninfo.async.log.queueSize:30000}")
    private int queueSize;

    @Value("${peroninfo.async.log.queueSize:200}")
    private int batchSize;

    @Value("${peroninfo.async.log.queueSize:10}")
    private int threadPoolSize;

    /**
     * 批量执行标志
     */
    private volatile boolean runBatch = false;

    private ExecutorService threadPool;

    private ArrayBlockingQueue<Count> blockingQueue;

    private final CountManagementService countManagementService;

    @Autowired
    public AsyncExecutorPoolConfig(CountManagementService countManagementService) {
        this.countManagementService = countManagementService;
    }

    @PostConstruct
    public void init() {
        blockingQueue = new ArrayBlockingQueue<>(queueSize);
        threadPool = Executors.newFixedThreadPool(threadPoolSize);
    }

    @PreDestroy
    public void destroy() {
        //  批量保存日志
        batchSave();

        // 关闭线程池
        if (threadPool != null) {
            threadPool.shutdown();
        }
    }

    /**
     * 添加到异步日志队列中
     *
     * @param
     */
    public void put(Count count) {
//        // 如果队列满了,主动调用批量方法
        if (blockingQueue.size() >= queueSize) {
            runBatch();
        }
        try {
            blockingQueue.put(count);
        } catch (InterruptedException e) {
            LOG.warn("Interrupted!", e);
        }
    }

    @Scheduled(cron = "${peroninfo.async.log.cron:0/5 * * * * ?}")
    public void runBatch() {
        if (runBatch) {
            LOG.info("batchSave Is already running.");
            return;
        }

        runBatch = true;
        try {
            // 批量保存日志
            batchSave();

        } catch (Throwable e) {
            runBatch = false;
            throw e;
        }

        runBatch = false;
    }

    /**
     * 批量保存日志
     */
    private void batchSave() {
        List<Count> buffer = new ArrayList<>(batchSize);

        do {
            // 获取日志对象
            Count poll = blockingQueue.poll();
            if (poll == null) {
                break;
            }
            buffer.add(poll);

            if (buffer.size() >= batchSize) {
                // 保存日志
                save(buffer);
                // 构建新的日志缓冲
                buffer = new ArrayList<>(batchSize);
            }

        } while (true);

        // 保存日志
        save(buffer);
    }

    /**
     * 保存日志
     *
     * @param buffer 日志缓冲
     */
    private void save(List<Count> buffer) {
        if (buffer.size() > 0) {
            threadPool.execute(() -> countManagementService.saveLog(buffer));
        }
    }

}

service接口类:

public interface CountManagementService extends IService<Count> {

    void saveLog(List<Count> buffer);

}

实现类:

@Override
public void saveLog(List<Count> buffer) {
    try (SqlSession sqlSession = sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH, false)) {
        // 保存主表 RuleflowExecuteLog
        for (Count tbPersonInterfacelog : buffer) {
            // insert
            sqlSession.insert("cc.xx.dao.CountMapper.saveCount", tbPersonInterfacelog);
        }
        // 提交事务
        sqlSession.commit();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

cc.xx.dao.CountMapper.saveCount:

public interface CountMapper extends MyMapper<Count> {

    int saveCount( Count count);

}

插入语句实现:

<insert id="saveCount" parameterType="cc.xx.domain.Count">

insert into tableA () values ()

</insert>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值