1

我无法弄清楚如何使用 Reactor 正确实现发布者/订阅者场景。我有一个可行的解决方案,但实施对我来说似乎不正确:

我的问题是我需要手动实现发布者来注册订阅者并传递事件:

public void publishQuotes(String ticker) throws InterruptedException {

// [..] Here I generate some "lines" to be publisher

for (Subscriber<? super String> subscriber : subscribers) {
    lineList.forEach(line -> subscriber.onNext(line));
}

}

@Override
public void subscribe(Subscriber<? super String> subscriber) {
    subscribers.add(subscriber);

}

然后,我有一个 WorkQueue 处理器(应该是消费者):

WorkQueueProcessor<String> sink = WorkQueueProcessor.create();

// Here I subscribe to my publiser
publisher.subscribe(sink);

// Creates a Reactive Stream from the processor (having converted the lines to Quotations)
Flux<StockQuotation> mappedRS = sink.map(quotationConverter::convertHistoricalCSVToStockQuotation);

// Here I perform a number of stream transformations 

// Each call to consume will be executed in a separated Thread
filteredRS.consume(i -> System.out.println(Thread.currentThread() + " data=" + i));

它工作正常,但丑得要命。在这个取自 Spring Guides 的示例中,他们使用 EventBus 将事件从发布者路由到消费者,但是,当我尝试将它与我的处理器链接时,我得到了这个编译器错误:

eventBus.on($("quotes"),sink);

The method on(Selector, Consumer<T>) in the type EventBus is not applicable for the arguments (Selector<String>, WorkQueueProcessor<String>)

我在这里迷路了,将发布者与处理器联系起来的最佳方式是什么?你会推荐使用 EventBus 吗?如果是这样,正确的调用是什么?

谢谢!

4

1 回答 1

1

如果您使用 EventBus,您将通过以下方式发布您的线路

 eventBus.notify("quotes", Event.wrap(line);

并通过订阅

eventBus.on($("quotes"), e -> System.out.println(Thread.currentThread() + " data=" + e);

其中“e”属于 Event<String> 类型。

于 2016-03-14T13:38:22.337 回答