Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type Matching Event Bus Messages? #1902

Closed
InfoSec812 opened this issue Mar 27, 2017 · 6 comments
Closed

Type Matching Event Bus Messages? #1902

InfoSec812 opened this issue Mar 27, 2017 · 6 comments
Labels

Comments

@InfoSec812
Copy link
Contributor

I think it would be quite valuable to support an overloaded method for the EventBus consumer to allow specifying the type of the message:

EventBus eb = vertx.eventBus();
eb.consumer(address, Type<? extends Serializable>, handler);

This would allow for type-safe message handling and allow for matching different consumers to different types. I'll try to look into what it would take to make this possible. It obviously wouldn't easily apply to some of the dynamic languages though...

@vietj
Copy link
Member

vietj commented Mar 27, 2017

Hi,

I think it would work better if you use your own static method to do that:

public static <T> Handler<Message<T>> matching(Class<T> match, Handler<Message<T>> consumer) {
  return msg -> {
    if (match.isInstance(msg.body()) {
       handler.handler(msg);
    }
  };
}

used like:

eventBus.consumer("the-address", matching(String.class, msg -> { ... }));

In addition it allows you to come up with a richer API if needed.

@InfoSec812
Copy link
Contributor Author

How would that work with multiple consumers for different types though? I was thinking along the lines of:

String address = "event.update";
EventBus eb = vertx.eventBus();
eb.consumer(address, User.class, handler);
eb.consumer(address, Product.class, handler);
eb.consumer(address, BillOfSale.class, handler);

@vietj
Copy link
Member

vietj commented Mar 27, 2017

in your case handler has to be Handler<Object>

@InfoSec812
Copy link
Contributor Author

Perhaps I'm not explaining it properly... It seems to me that your solution would deliver the message to all of the handlers, regardless of type; then the handler would discard non-matching items. In that case, wouldn't I have to "publish" so that multiple typed handlers could each receive the message? My thoughts on this would be that the eventbus code could have logic added so that it only tries to deliver to an appropriately typed handler. That way the "send" method could be used and the event bus would only deliver to the appropriately typed handler.

@vietj
Copy link
Member

vietj commented Mar 27, 2017

I understand but a message is sent/published regardless to an address, regardless of the recipient.

If you need such semantic I think you should use different addresses instead and use a convention to map the recipient to addresses.

@InfoSec812
Copy link
Contributor Author

Yeah, I certainly see your point. I'll close this and think about it further...

Thanks!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants