GeezerVison is showing github.com — SpringBoot RabbitMQ Receive Message · yaozd/SpringBoot-RabbitMQ@13cf1c9
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: README.md +34-1Lines changed: 34 additions & 1 deletion - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
|
140 | 140 | * 它提供了一个内置的依赖解析器来匹配[Spring Boot Dependencies](https://github.com/spring-projects/spring-boot/blob/master/spring-boot-dependencies/pom.xml)依赖版本号,你可以重写任何你希望的版本,但它默认启动时选择的版本集合 |
141 | 141 |
|
142 | 142 | ###使用IDE编译 |
143 | | -####1.简历RabbitMQ沙箱 |
| 143 | +####1.建立RabbitMQ沙箱 |
144 | 144 | 在你可以构建你的消息应用前,你需要建发布和订阅消息的服务器 |
145 | 145 |
|
146 | 146 | RabbitMQ是一个AMQP(Advanced Message Queuing Protocol,一个提供统一消息服务的应用层标准高级消息队列协议,是应用层协议的一个开放标准,为面向消息的中间件设计)服务器,这个服务器是免费的,你可以在[http://www.rabbitmq.com/download.html](http://www.rabbitmq.com/download.html),你可以手动的下载,或者如果你使用的Mac可以自己制作 |
@@ -174,4 +174,37 @@ RabbitMQ是一个AMQP(Advanced Message Queuing Protocol,一个提供统一消息 |
174 | 174 | |
175 | 175 | 如果这个文件在你的当前目录中你可以运行`docker-compose up`来是RabbitMQ运行在容器中 |
176 | 176 |
|
| 177 | +####2.创建RabbitMQ消息订阅 |
| 178 | +任何基于消息的应用程序你都需要创建一个消息订阅来响应消息的发布 |
| 179 | + |
| 180 | +`src/main/java/hello/Receiver.java` |
| 181 | + |
| 182 | +```Java |
| 183 | +package hello; |
| 184 | + |
| 185 | +import java.util.concurrent.CountDownLatch; |
| 186 | +import org.springframework.stereotype.Component; |
| 187 | + |
| 188 | +@Component |
| 189 | +public class Receiver { |
| 190 | + |
| 191 | + private CountDownLatch latch = new CountDownLatch(1); |
| 192 | + |
| 193 | + public void receiveMessage(String message) { |
| 194 | + System.out.println("Received <" + message + ">"); |
| 195 | + latch.countDown(); |
| 196 | + } |
| 197 | + |
| 198 | + public CountDownLatch getLatch() { |
| 199 | + return latch; |
| 200 | + } |
| 201 | + |
| 202 | +} |
| 203 | +``` |
| 204 | + |
| 205 | +定义一个简单的`Receiver`类,类中receiveMessage方法用来接收消息,当你注册接受消息时,你可以随意命名它 |
| 206 | + |
| 207 | +>为了方便,这个POJO类有一个`CountDownLatch`类的属性,它允许当消息接收到时给它一个信号量,这是你在生产环境中你不太可能实现的 |
| 208 | +
|
| 209 | +####3.注册监听并发布消息 |
177 | 210 |
|
Collapse file
Copy file name to clipboard Collapse file
Copy file name to clipboard +20Lines changed: 20 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
|
| 1 | +package hello; |
| 2 | + |
| 3 | +import java.util.concurrent.CountDownLatch; |
| 4 | +import org.springframework.stereotype.Component; |
| 5 | + |
| 6 | +@Component |
| 7 | +public class Receiver { |
| 8 | + |
| 9 | + private CountDownLatch latch = new CountDownLatch(1); |
| 10 | + |
| 11 | + public void receiveMessage(String message) { |
| 12 | + System.out.println("Received <" + message + ">"); |
| 13 | + latch.countDown(); |
| 14 | + } |
| 15 | + |
| 16 | + public CountDownLatch getLatch() { |
| 17 | + return latch; |
| 18 | + } |
| 19 | + |
| 20 | +} |