轉載:SpringBoot非官方教程 | 第十五篇:Springboot整合RabbitMQ
阿新 • • 發佈:2018-12-14
這篇文章帶你瞭解怎麼整合RabbitMQ伺服器,並且通過它怎麼去傳送和接收訊息。我將構建一個springboot工程,通過RabbitTemplate去通過MessageListenerAdapter去訂閱一個POJO型別的訊息。
準備工作
- 15min
- IDEA
- maven 3.0
在開始構建專案之前,機器需要安裝rabbitmq,你可以去官網下載,http://www.rabbitmq.com/download.html ,如果你是用的Mac(程式設計師都應該用mac吧),你可以這樣下載:
brew install rabbitmq
- 1
- 2
安裝完成後開啟伺服器:
rabbitmq-server
- 1
開啟伺服器成功,你可以看到以下資訊:
RabbitMQ 3.1.3. Copyright (C) 2007-2013 VMware, Inc. ## ## Licensed under the MPL. See http://www.rabbitmq.com/ ## ## ########## Logs: /usr/local/var/log/rabbitmq/[email protected] ###### ## /usr/local/var/log/rabbitmq/[email protected] ########## Starting broker... completed with 6 plugins.
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
構建工程
構架一個SpringBoot工程,其pom檔案依賴加上spring-boot-starter-amqp的起步依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
- 1
- 2
- 3
- 4
- 5
建立訊息接收者
在任何的訊息佇列程式中,你需要建立一個訊息接收者,用於響應傳送的訊息。
@Component public class Receiver { private CountDownLatch latch = new CountDownLatch(1); public void receiveMessage(String message) { System.out.println("Received <" + message + ">"); latch.countDown(); } public CountDownLatch getLatch() { return latch; } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
訊息接收者是一個簡單的POJO類,它定義了一個方法去接收訊息,當你註冊它去接收訊息,你可以給它取任何的名字。其中,它有CountDownLatch這樣的一個類,它是用於告訴傳送者訊息已經收到了,你不需要在應用程式中具體實現它,只需要latch.countDown()就行了。
建立訊息監聽,併發送一條訊息
在spring程式中,RabbitTemplate提供了傳送訊息和接收訊息的所有方法。你只需簡單的配置下就行了:
- 需要一個訊息監聽容器
- 宣告一個quene,一個exchange,並且繫結它們
- 一個元件去傳送訊息
程式碼清單如下:
package com.forezp;
import com.forezp.message.Receiver;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class SpringbootRabbitmqApplication {
final static String queueName = "spring-boot";
@Bean
Queue queue() {
return new Queue(queueName, false);
}
@Bean
TopicExchange exchange() {
return new TopicExchange("spring-boot-exchange");
}
@Bean
Binding binding(Queue queue, TopicExchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with(queueName);
}
@Bean
SimpleMessageListenerContainer container(ConnectionFactory connectionFactory,
MessageListenerAdapter listenerAdapter) {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
container.setQueueNames(queueName);
container.setMessageListener(listenerAdapter);
return container;
}
@Bean
MessageListenerAdapter listenerAdapter(Receiver receiver) {
return new MessageListenerAdapter(receiver, "receiveMessage");
}
public static void main(String[] args) {
SpringApplication.run(SpringbootRabbitmqApplication.class, args);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
建立一個測試方法:
@Component
public class Runner implements CommandLineRunner {
private final RabbitTemplate rabbitTemplate;
private final Receiver receiver;
private final ConfigurableApplicationContext context;
public Runner(Receiver receiver, RabbitTemplate rabbitTemplate,
ConfigurableApplicationContext context) {
this.receiver = receiver;
this.rabbitTemplate = rabbitTemplate;
this.context = context;
}
@Override
public void run(String... args) throws Exception {
System.out.println("Sending message...");
rabbitTemplate.convertAndSend(Application.queueName, "Hello from RabbitMQ!");
receiver.getLatch().await(10000, TimeUnit.MILLISECONDS);
context.close();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
啟動程式,你會發現控制檯列印:
Sending message...
Received <Hello from RabbitMQ!>
- 1
- 2
- 3
總結
恭喜!你剛才已經學會了如何通過spring raabitmq去構建一個訊息傳送和訂閱的程式。 這僅僅是一個好的開始,你可以通過spring-rabbitmq做更多的事,點選這裡。