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

com.alibaba.schedulerx.worker.batch.ReqQueue Maven / Gradle / Ivy

There is a newer version: 1.12.2
Show newest version
package com.alibaba.schedulerx.worker.batch;

import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import com.alibaba.schedulerx.worker.log.LogFactory;
import com.alibaba.schedulerx.worker.log.Logger;

import com.google.common.collect.Lists;

/**
 * Queue-based load leveling buffer, used for parallel/grid reqs buffer.
 * Can buffer any reqs.
 * @author yanxun on 2019/1/5.
 */
public class ReqQueue {
    private long jobInstanceId;
    private int capacity;
    private static final Logger LOGGER = LogFactory.getLogger(ReqQueue.class);

    private BlockingQueue requests;

    public ReqQueue(long jobInstanceId, int capacity) {
        this.jobInstanceId = jobInstanceId;
        this.capacity = capacity;
    }

    public void init() {
        requests = new LinkedBlockingQueue<>(capacity);
    }

    public void submitRequest(T request) throws Exception {
        try {
            if (request != null) {
                requests.put(request);
            }
        } catch (Throwable e) {
            LOGGER.error("add task status request to queue error, jobInstanceId:{}",
                jobInstanceId, e);
            throw e;
        }
    }

    /**
     * return requests with no more than #batchSize elements.
     * @param batchSize max number of elements attempt to retrieve
     * @return requests list
     */
    public List retrieveRequests(int batchSize) {
        List res = Lists.newLinkedList();
        T request;
        for (int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy