简体   繁体   English

使用 Guava EventBus 的条件订阅

[英]Conditional subscriptions with Guava EventBus

I am looking at using the Guava EventBus in my application to distribute data ( Double s for example) from one or more data creators through to data consumer.我正在考虑在我的应用程序中使用Guava EventBus将数据(例如Double s)从一个或多个数据创建者分发到数据消费者。

I know in my consumer class, I need to annotate my data handler with @Subscribe .我知道在我的消费者 class 中,我需要用@Subscribe注释我的数据处理程序。 Is there a way in which to make this subscription conditional?有没有办法让这个订阅有条件? So for example所以例如

@Subscribe {newValue > 0.0} public void valueUpdated(Double newValue)

I could add the check within my valueUpdated method, but is there a way to stop the EventBus from dispatching values that my Subscriber is not interested in?我可以在我的valueUpdated方法中添加检查,但是有没有办法阻止EventBus调度我的订阅者不感兴趣的值?

Is there a product similar to EventBus that may provide this sort of function?有没有类似EventBus的产品可以提供这种function?

Spring Expression Language (SpEL) may help. Spring Expression Language(SpEL)可能有所帮助。

Plus: after checking the EventBus, I think it's type-based dispatch, no way to apply condition-based dispatch. 另外:在检查EventBus之后,我认为它是基于类型的调度,无法应用基于条件的调度。 If you insist on using Expression Language, you could put it in subscriber as Louis comment. 如果您坚持使用表达式语言,您可以将其作为路易斯评论添加到订阅者中。 But Expression Language is designed for flexibility rather than speed. 但表达语言是为灵活而非速度而设计的。

you could achieve this using with Guava EventBus using your own Dispatcher implementation where you dispatch an event of type MyEvent only if it is interesting ie its newValue is greater than 0.您可以使用Guava EventBus使用您自己的 Dispatcher 实现来实现此目的,其中仅在有趣的情况下调度 MyEvent 类型的事件,即它的 newValue 大于 0。

package com.google.common.eventbus;

import com.google.common.util.concurrent.MoreExecutors;
import lombok.Data;
import java.util.Iterator;
import static com.google.common.base.Preconditions.checkNotNull;

public class MyDispatcher extends Dispatcher {

    private static final MyDispatcher INSTANCE = new MyDispatcher();

    @Override
    void dispatch(Object event, Iterator<Subscriber> subscribers) {
        checkNotNull(event);
        if (event instanceof MyEvent) {
            Double newValue = ((MyEvent) event).getNewValue();
            if (null != newValue && newValue > 0.0d) { //dispatch events only if newValue > 0.0d
                while (subscribers.hasNext()) {
                    subscribers.next().dispatchEvent(event);
                }
            }
        } else {
            while (subscribers.hasNext()) {
                subscribers.next().dispatchEvent(event);
            }
        }
    }

    @Data
    public static class MyEvent {
        private Object sender;
        private Double newValue;
    }

    public static class MyEventBus extends EventBus {
        public MyEventBus() {
            super("myEventBys", MoreExecutors.newDirectExecutorService(), MyDispatcher.INSTANCE,
                LoggingHandler.INSTANCE);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM