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

kg.apc.jmeter.modifiers.FifoMap Maven / Gradle / Ivy

There is a newer version: 1.4.0
Show newest version
package kg.apc.jmeter.modifiers;

import org.apache.jmeter.util.JMeterUtils;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

public class FifoMap extends ConcurrentHashMap> {

    public static final String TIMEOUT_PROP = "kg.apc.jmeter.functions.FifoTimeout";
    public static final String CAPACITY_PROP = "kg.apc.jmeter.functions.FifoCapacity";
    private static FifoMap instance = new FifoMap();

    private FifoMap() {
    }

    public static FifoMap getInstance() {
        return instance;
    }

    private BlockingQueue getFifo(String fifoName) {
        if (super.containsKey(fifoName)) {
            return super.get(fifoName);
        } else {
            BlockingQueue fifo = new LinkedBlockingQueue(JMeterUtils.getPropDefault(FifoMap.CAPACITY_PROP, Integer.MAX_VALUE));
            super.put(fifoName, fifo);
            return fifo;
        }
    }

    public Object get(String fifoName) {
        BlockingQueue fifo = getFifo(fifoName);
        Object value;
        value = fifo.peek();
        return value == null ? "" : value;
    }

    public Object pop(String fifoName, long timeout) throws InterruptedException {
        BlockingQueue fifo = getFifo(fifoName);
        return fifo.poll(timeout, TimeUnit.SECONDS);
    }

    public Object pop(String fifoName) throws InterruptedException {
        return pop(fifoName, Long.MAX_VALUE);
    }

    public int length(String fifoName) {
        BlockingQueue fifo = getFifo(fifoName);
        return fifo.size();
    }

    public void put(String fifoName, Object v) throws InterruptedException {
        getFifo(fifoName).offer(v, Long.MAX_VALUE, TimeUnit.SECONDS);
    }
}