3

使用反应器(https://github.com/reactor/reactor)我通知一些事件,例如

 commandReactor.notify("CREATE_CUSTOMER", Event.wrap(customer));
 commandReactor.notify("CREATE_ORDER", Event.wrap(order));

我如何实现一个选择器,它选择以“CREATE”开头的所有事件?就像是

@Selector(value = "CREATE*", reactor = "@commandReactor")

提前致谢。

4

1 回答 1

7

您可以使用RegexSelector[1] 来做到这一点:

    commandReactor.notify("CREATE_(.+)", Event.wrap(obj));

或者,使用注释:

    @Selector(value = "CREATE_(.+)", type = SelectorType.REGEX)

然后在您的处理程序中,您可以通过查看 to 的标题来检查捕获group1groupN

new Consumer<Event<Object>>>() {
  public void accept(Event<?> ev) {
    String type = ev.getHeaders().get("group1");
    if("CUSTOMER".equals(type)) {
      // handle customers
    } else if("ORDER".equals(type)) {
      // handle orders
    }
}

[1] - http://reactor.github.io/docs/api/reactor/event/selector/RegexSelector.html

于 2013-11-05T13:51:44.177 回答