1 | |
package kg.apc.jmeter.modifiers; |
2 | |
|
3 | |
import org.apache.jmeter.util.JMeterUtils; |
4 | |
|
5 | |
import java.util.concurrent.BlockingQueue; |
6 | |
import java.util.concurrent.ConcurrentHashMap; |
7 | |
import java.util.concurrent.LinkedBlockingQueue; |
8 | |
import java.util.concurrent.TimeUnit; |
9 | |
|
10 | |
public class FifoMap extends ConcurrentHashMap<String, BlockingQueue<Object>> { |
11 | |
|
12 | |
public static final String TIMEOUT_PROP = "kg.apc.jmeter.functions.FifoTimeout"; |
13 | |
public static final String CAPACITY_PROP = "kg.apc.jmeter.functions.FifoCapacity"; |
14 | 1 | private static FifoMap instance = new FifoMap(); |
15 | |
|
16 | 1 | private FifoMap() { |
17 | 1 | } |
18 | |
|
19 | |
public static FifoMap getInstance() { |
20 | 32 | return instance; |
21 | |
} |
22 | |
|
23 | |
private BlockingQueue<Object> getFifo(String fifoName) { |
24 | 21 | if (super.containsKey(fifoName)) { |
25 | 8 | return super.get(fifoName); |
26 | |
} else { |
27 | 13 | BlockingQueue<Object> fifo = new LinkedBlockingQueue<Object>(JMeterUtils.getPropDefault(FifoMap.CAPACITY_PROP, Integer.MAX_VALUE)); |
28 | 13 | super.put(fifoName, fifo); |
29 | 13 | return fifo; |
30 | |
} |
31 | |
} |
32 | |
|
33 | |
public Object get(String fifoName) { |
34 | 3 | BlockingQueue<Object> fifo = getFifo(fifoName); |
35 | |
Object value; |
36 | 3 | value = fifo.peek(); |
37 | 3 | return value == null ? "" : value; |
38 | |
} |
39 | |
|
40 | |
public Object pop(String fifoName, long timeout) throws InterruptedException { |
41 | 5 | BlockingQueue<Object> fifo = getFifo(fifoName); |
42 | 5 | return fifo.poll(timeout, TimeUnit.SECONDS); |
43 | |
} |
44 | |
|
45 | |
public Object pop(String fifoName) throws InterruptedException { |
46 | 0 | return pop(fifoName, Long.MAX_VALUE); |
47 | |
} |
48 | |
|
49 | |
public int length(String fifoName) { |
50 | 2 | BlockingQueue<Object> fifo = getFifo(fifoName); |
51 | 2 | return fifo.size(); |
52 | |
} |
53 | |
|
54 | |
public void put(String fifoName, Object v) throws InterruptedException { |
55 | 11 | getFifo(fifoName).offer(v, Long.MAX_VALUE, TimeUnit.SECONDS); |
56 | 11 | } |
57 | |
} |