Spring Reactor Consumer不消耗

时间:2017-06-25 17:28:23

标签: spring reactor

今天我尝试使用以下示例的弹簧反应器:messaging-reactor

它基本上是从给定的URL请求随机引用并将其打印到控制台。这是一个很好的例子,展示出版和消费如何与弹簧反应堆一起工作,而不会阻挡任何东西。

但我有一个小问题。在该示例中,定义了一个接收器(消费者),并且输出具有以下内容:

Quote 4: Really loving Spring Boot, makes stand alone Spring apps easy.
Quote 6: So easy it is to switch container in #springboot.
Quote 7: Working with Spring Boot is like pair-programming with the Spring developers.
Quote 8: Really loving Spring Boot, makes stand alone Spring apps easy.
Quote 5: I have two hours today to build an app from scratch. @springboot to the rescue!
Quote 2: The real benefit of Boot, however, is that it's just Spring. That means any direction the code takes, regardless of complexity, I know it's a safe bet.
Quote 1: With Boot you deploy everywhere you can find a JVM basically.
Quote 3: With Boot you deploy everywhere you can find a JVM basically.
Quote 6: I don't worry about my code scaling. Boot allows the developer to peel back the layers and customize when it's appropriate while keeping the conventions that just work.
Quote 8: I have two hours today to build an app from scratch. @springboot to the rescue!
Elapsed time: 841ms
Average time per quote: 84ms

我添加了第二个接收者(消费者):

@Autowired
private Receiver receiver;

@Autowired
private Receiver receiver2;

@Override
public void run(String... args) throws Exception {
    this.eventBus.on($("quotes"), this.receiver);
    this.eventBus.on($("quotes"), this.receiver2);

    this.publisher.publishQuotes(NUMBER_OF_QUOTES);
}

所以现在我有两个接收器,输出是:

Quote 4: Really loving Spring Boot, makes stand alone Spring apps easy.
Quote 6: So easy it is to switch container in #springboot.
Quote 7: Working with Spring Boot is like pair-programming with the Spring developers.
Quote 8: Really loving Spring Boot, makes stand alone Spring apps easy.
Quote 5: I have two hours today to build an app from scratch. @springboot to the rescue!
Quote 2: The real benefit of Boot, however, is that it's just Spring. That means any direction the code takes, regardless of complexity, I know it's a safe bet.
Quote 1: With Boot you deploy everywhere you can find a JVM basically.
Quote 3: With Boot you deploy everywhere you can find a JVM basically.
Quote 6: I don't worry about my code scaling. Boot allows the developer to peel back the layers and customize when it's appropriate while keeping the conventions that just work.
Quote 8: I have two hours today to build an app from scratch. @springboot to the rescue!
Elapsed time: 841ms
Average time per quote: 84ms
2017-06-25 19:15:25.137  INFO 2700 --- [           main] d.hof.fronetic.demo.reactor.Application  : Started Application in 4.103 seconds (JVM running for 4.374)
Quote 7: It embraces convention over configuration, providing an experience on par with frameworks that excel at early stage development, such as Ruby on Rails.
Quote 2: I have two hours today to build an app from scratch. @springboot to the rescue!
Quote 5: Spring has come quite a ways in addressing developer enjoyment and ease of use since the last time I built an application using it.
Quote 3: So easy it is to switch container in #springboot.
Quote 1: I have two hours today to build an app from scratch. @springboot to the rescue!
Quote 10: With Boot you deploy everywhere you can find a JVM basically.
Quote 9: Previous to Spring Boot, I remember XML hell, confusing set up, and many hours of frustration.
Quote 4: Spring Boot solves this problem. It gets rid of XML and wires up common components for me, so I don't have to spend hours scratching my head just to figure out how it's all pieced together.
Quote 9: It embraces convention over configuration, providing an experience on par with frameworks that excel at early stage development, such as Ruby on Rails.
Quote 10: Spring Boot solves this problem. It gets rid of XML and wires up common components for me, so I don't have to spend hours scratching my head just to figure out how it's all pieced together.

所以你可以看到,我现在得到20个引号而不是10个引号。但在我看来,这不是消费者的工作方式。请告诉我,如果我错了,但我认为如果消费者收到通知,则consuming此通知,以便其他接收者无法收到此通知。在上面的例子中,每个消费者都在执行相同的工作。如果我想在这两个消费者之间共享所有工作(打印10个引号),以便在最佳情况下每个消费将消耗5个通知,该怎么办?这不是发布商/消费者反应堆的主要任务之一吗?

1 个答案:

答案 0 :(得分:2)

我认为您将point to point消息与publish and subscribe混淆。您的直观模型是点对点消息传递所发生的情况,消费者从队列中接收消息,以便其他进程不会消耗它。基于事件的模型是不同的,因为需要一个过程向论坛发布消息,通知所有人'听众同时。

你有后者 - 一个基于事件的系统。您的听众订阅了相同的频道并同时回复相同的消息。