Clarifying the difference between registerHandler() and registerLocalHandler()

179 views
Skip to first unread message

Oren Shvalb

unread,
May 10, 2014, 12:17:28 AM5/10/14
to ve...@googlegroups.com
Hi,

Can someone clarify the differences between them and state when to use each of them, cons&pros...

Thanks.

Jordan Halterman (kuujo)

unread,
May 10, 2014, 1:09:06 AM5/10/14
to ve...@googlegroups.com
Hey,

registerLocalHandler is obviously a local version of registerHandler. But what that means is when you register a handler with registerLocalHandler, Vert.x will not propagate the handler around the cluster. That means handlers that are registered with registerLocalHandler can only be accessed by sending messages on the event bus in the Vert.x instance in which it was registered. So, if for instance your Vert.x instance is never clustered then registerLocalHandler will behave exactly the same way as registerHandler since the registrations don't need to be propagated anyways.

You'll notice that registerLocalHandler doesn't accept an AsyncResult handler. Since the registration doesn't have to be propagated to other nodes in the cluster, the registration can happen synchronously. That can be considered one of the benefits of registerLocalHandler. Also I suppose you can use equivalent event bus addresses in separate nodes without the handlers clashing with one another. For instance, you could register a handler at "foo" on node 1 and a handler at "foo" on node 2. In that case, if you sent a message to "foo" from node 1 then it would go to the first handler, and if you sent a message to "foo" from node 2 it would go to the second handler since neither node knows about the other handler. However, I wouldn't recommend relying on this as it could make for some confusing codez.

Jordan Halterman

Jordan Halterman (kuujo)

unread,
May 10, 2014, 1:14:06 AM5/10/14
to ve...@googlegroups.com
By the way, I think the case where this is most useful is when you're deploying some sort of helper verticle. In that case, you know you're only communicating with an address within the current Vert.x instance. For example:

public class MyVerticle extends Verticle {

 
public static class HelperVerticle extends Verticle {
   
@Override
   
public void start() {
      vertx
.eventBus().registerLocalHandler("helper", new Handler<Message<JsonObject>>() {
       
public void handle(Message<JsonObject> message) {
         
// Do something helpful
       
}
     
});
   
}
  }

  @Override
 
public void start() {
    container
.deployWorkerVerticle(HelperVerticle.class.getName(), new Handler<AsyncResult<String>>() {
     
public void handle(AsyncResult<String> result) {
       
// Send a message to the helper verticle
        vertx
.eventBus().send("helper", new JsonObject().putString("do", "Help me!"));
     
}
   
});
 
}

}

In this case the verticle is a helper specific to this current verticle so there is no need for the handler to be propagated around the cluster and made available to other nodes. That is where it is useful. Does that make sense?


On Friday, May 9, 2014 9:17:28 PM UTC-7, Oren Shvalb wrote:

Oren Shvalb

unread,
May 10, 2014, 1:19:13 AM5/10/14
to ve...@googlegroups.com
Well explained!

Thank you

Reply all
Reply to author
Forward
0 new messages