Intercept events and block event propagation to the Event Bus

97 views
Skip to first unread message

Pablo Villot Guisán

unread,
Jan 27, 2020, 4:29:56 AM1/27/20
to vert.x
Hi, I am trying to intercept any messages sent through the event bus to add a cache for some petitions. 

I am trying to add logic so that in some cases it returns a cached result without really sending the message or allow the message to be sent if the result is not cached. 

I am trying by using interceptors but i can't find a way to stop the message propagation to the bus (if i dont include the event.next(); the process just gets stuck waiting for the next interceptor to execute, which will never happen). 

This is how I am trying to implement this:

Handler<DeliveryContext<K>> cacheInterceptor = new Handler<DeliveryContext<K>> () {

@Override
public void handle(DeliveryContext<K> event) {
K body = event.message().body();
Object cachedResponse = retrievefromCache(extractId(body));
if (cachedResponse != null) {
event.message().reply(cachedResponse);
//ADD LOGIC TO STOP EVENT PROPAGATION
} else {
event.next();
}

}
};
this.vertx.eventBus().addOutboundInterceptor(cacheInterceptor);

The code would be executed on the verticle initialization so that it intercepts all future calls to the event bus.

Does anyone know any way to do this or maybe another approach for what i am trying to achieve?

Thomas SEGISMONT

unread,
Jan 27, 2020, 5:21:59 AM1/27/20
to vert.x
Sharing this here in case anyone is interested in the future. As discussed on Gitter:

1/ Don't use interceptors for this
2/ Create your EventBus impl that delegates to the builtin one and intercepts send/request/publish methods
3/ Create your Vertx impl that delegates to the builtin one and intercepts the eventBus method
3/ Create your VertxFactory instance that delegates to the default one and intercepts Vertx instance creation to return your VertxImpl

--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/vertx/313bb0c3-add3-4fb5-916c-6527b2686a60%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages