2017-06-25 41 views
0

今天我试着用下面的例子春天反应器: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. 

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

回答

2

我想你很困惑point to point消息与publish and subscribe。您的直观模型是使用点对点消息传递的情况,消费者将消息从队列中取出,以便其他进程不会使用它。然而,基于事件的模型是不同的,因为需要一个过程向论坛发布消息,同时通知“所有”听众。

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

+0

所以项目反应堆并不意味着是“点对点”消息传递,也不可配置为那样? – Mulgard

+0

我不确定这一点,但鉴于基于“事件”的模型(几乎总是pub-sub架构),似乎并非如此。某些支持的消息总线(如kafka)仅支持pub-sub,而点对点甚至不是一种选择。 –