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

pl.allegro.tech.hermes.consumers.queue.MonitoredMpscQueue Maven / Gradle / Ivy

There is a newer version: 2.10.0
Show newest version
package pl.allegro.tech.hermes.consumers.queue;

import org.jctools.queues.MessagePassingQueue;
import org.jctools.queues.MpscArrayQueue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pl.allegro.tech.hermes.common.metric.HermesMetrics;

public class MonitoredMpscQueue {

    private static final Logger logger = LoggerFactory.getLogger(MonitoredMpscQueue.class);

    private final MpscArrayQueue queue;

    private final String name;

    private final HermesMetrics metrics;

    public MonitoredMpscQueue(HermesMetrics metrics, String name, int capacity) {
        this.queue = new MpscArrayQueue<>(capacity);
        this.name = name;
        this.metrics = metrics;
        metrics.registerGauge("queue." + name + ".utilization", () -> queue.size() / queue.capacity());
    }

    public boolean offer(T element) {
        boolean accepted = queue.offer(element);
        if (!accepted) {
            metrics.counter("queue." + name + ".failures").inc();
            logger.error("[Queue: {}] Unable to add item: queue is full. Offered item: {}", name, element);
        }
        return accepted;
    }

    public void drain(MessagePassingQueue.Consumer consumer) {
        queue.drain(consumer);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy