【问题标题】:Select events matching a key using Reactor使用 Reactor 选择与键匹配的事件
【发布时间】:2013-11-05 13:25:36
【问题描述】:

使用 reactor(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")

提前致谢。

【问题讨论】:

    标签: java spring reactor


    【解决方案1】:

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

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

    或者,使用注释:

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

    然后在您的处理程序中,您可以通过查看 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多