如何正确使用Reactor Publisher

时间:2016-03-12 16:07:49

标签: java spring reactor project-reactor

我无法弄清楚如何使用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));

它工作得很好但是很丑陋。 In this example取自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吗?如果是这样,那么适当的调用是什么?

谢谢!

1 个答案:

答案 0 :(得分:1)

如果您使用EventBus,您将通过

发布您的行
THREE.Mesh

通过

订阅
intersectedFace.acc

其中&#34; e&#34;是类型事件&lt; String&gt;。