All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.bingoohuang.utils.batch.BatcherBuilder Maven / Gradle / Ivy

package com.github.bingoohuang.utils.batch;

import java.util.concurrent.ScheduledExecutorService;

import static java.util.concurrent.Executors.newScheduledThreadPool;

/**
 * @author bingoohuang [[email protected]] Created on 2017/1/20.
 */
public class BatcherBuilder {
    private final BatcherJob batcherJob;

    ScheduledExecutorService service;
    int maxWaitItems = 10; // 达到多少个就开工
    long maxWaitMillis = 300; // 或者累计满多少毫秒也开工
    int maxBatchNum = 0; // 一批最多多少个,<=0 不限制

    public BatcherBuilder(BatcherJob batcherJob) {
        this.batcherJob = batcherJob;
    }

    public static  BatcherBuilder newBuilder(
            BatcherJob batcherJob) {
        return new BatcherBuilder(batcherJob);
    }

    public BatcherBuilder executor(ScheduledExecutorService service) {
        this.service = service;
        return this;
    }

    public BatcherBuilder maxWaitMillis(long maxWaitMillis) {
        this.maxWaitMillis = maxWaitMillis;
        return this;
    }

    public BatcherBuilder maxWaitItems(int maxWaitItems) {
        this.maxWaitItems = maxWaitItems;
        return this;
    }

    public BatcherBuilder maxBatchNum(int maxBatchNum) {
        this.maxBatchNum = maxBatchNum;
        return this;
    }

    public Batcher build() {
        createServiceIfNotSet();
        return new Batcher(service, batcherJob, maxWaitMillis, maxWaitItems, maxBatchNum);
    }

    private void createServiceIfNotSet() {
        if (service != null) return;
        service = newScheduledThreadPool(5);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy