2017-06-25 58 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,而點對點甚至不是一種選擇。 –