pl.allegro.tech.hermes.consumers.queue.MonitoredMpscQueue Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hermes-consumers Show documentation
Show all versions of hermes-consumers Show documentation
Fast and reliable message broker built on top of Kafka.
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