com.cyngn.vertx.eventbus.EventBusTools Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of vertx-util Show documentation
Show all versions of vertx-util Show documentation
A library for using opentsdb with vert.x
package com.cyngn.vertx.eventbus;
import io.vertx.core.Handler;
import io.vertx.core.eventbus.EventBus;
import io.vertx.core.eventbus.Message;
import io.vertx.core.eventbus.MessageConsumer;
import java.util.concurrent.atomic.AtomicInteger;
/**
* General functions for interacting with the event bus.
*
* @author [email protected] (Jeremy Truelove) 4/24/15
*/
public class EventBusTools {
/**
* Listen to a message just once
*
* @param bus the event bus to listen on
* @param address the address to listen for
* @param handler callback on message received
* @param the type of object getting passed via the event bus
* @return the consumer created
*/
public static MessageConsumer oneShotConsumer(EventBus bus, String address, Handler> handler) {
return consumeNTimes(bus, address, handler, 1, false);
}
/**
* Listen to a message just once
*
* @param bus the event bus to listen on
* @param address the address to listen for
* @param handler callback on message received
* @param the type of object getting passed via the event bus
* @return the consumer created
*/
public static MessageConsumer oneShotLocalConsumer(EventBus bus, String address, Handler> handler) {
return consumeNTimes(bus, address, handler, 1, true);
}
/**
* Listen for a message N times
*
* @param bus the event bus to listen on
* @param address the address to listen for
* @param handler callback on message(s) received
* @param timesToConsume the number of times to listen for a message
* @param isLocalOnly should you consume just on the local event bus or everywhere
* @param the type of object getting passed via the event bus
* @return the consumer created
*/
public static MessageConsumer consumeNTimes(EventBus bus, String address, Handler> handler,
int timesToConsume, boolean isLocalOnly) {
if(timesToConsume <= 0) {return null;}
MessageConsumer consumer = isLocalOnly ? bus.localConsumer(address) : bus.consumer(address);
AtomicInteger count = new AtomicInteger(0);
consumer.handler(msg -> {
try {
handler.handle(msg);
count.incrementAndGet();
} finally {
if (count.get() == timesToConsume) {
consumer.unregister();
}
}
});
return consumer;
}
}