轉(zhuǎn)載請(qǐng)標(biāo)明出處:
http://blog.csdn.net/forezp/article/details/71023692
本文出自方志朋的博客
這篇文章帶你了解怎么整合RabbitMQ服務(wù)器,并且通過它怎么去發(fā)送和接收消息。我將構(gòu)建一個(gè)springboot工程哼转,通過RabbitTemplate去通過MessageListenerAdapter去訂閱一個(gè)POJO類型的消息楚午。
準(zhǔn)備工作
- 15min
- IDEA
- maven 3.0
在開始構(gòu)建項(xiàng)目之前淌友,機(jī)器需要安裝rabbitmq,你可以去官網(wǎng)下載厢破,http://www.rabbitmq.com/download.html ,如果你是用的Mac(程序員都應(yīng)該用mac吧)贾虽,你可以這樣下載:
brew install rabbitmq
安裝完成后開啟服務(wù)器:
rabbitmq-server
開啟服務(wù)器成功,你可以看到以下信息:
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/rabbit@localhost.log
###### ## /usr/local/var/log/rabbitmq/rabbit@localhost-sasl.log
##########
Starting broker... completed with 6 plugins.
構(gòu)建工程
構(gòu)架一個(gè)SpringBoot工程吼鱼,其pom文件依賴加上spring-boot-starter-amqp的起步依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
創(chuàng)建消息接收者
在任何的消息隊(duì)列程序中蓬豁,你需要?jiǎng)?chuàng)建一個(gè)消息接收者,用于響應(yīng)發(fā)送的消息菇肃。
@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;
}
}
消息接收者是一個(gè)簡(jiǎn)單的POJO類地粪,它定義了一個(gè)方法去接收消息,當(dāng)你注冊(cè)它去接收消息琐谤,你可以給它取任何的名字蟆技。其中,它有CountDownLatch這樣的一個(gè)類笑跛,它是用于告訴發(fā)送者消息已經(jīng)收到了付魔,你不需要在應(yīng)用程序中具體實(shí)現(xiàn)它聊品,只需要latch.countDown()就行了飞蹂。
創(chuàng)建消息監(jiān)聽,并發(fā)送一條消息
在spring程序中翻屈,RabbitTemplate提供了發(fā)送消息和接收消息的所有方法陈哑。你只需簡(jiǎn)單的配置下就行了:
- 需要一個(gè)消息監(jiān)聽容器
- 聲明一個(gè)quene,一個(gè)exchange,并且綁定它們
- 一個(gè)組件去發(fā)送消息
代碼清單如下:
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);
}
}
創(chuàng)建一個(gè)測(cè)試方法:
@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();
}
}
啟動(dòng)程序,你會(huì)發(fā)現(xiàn)控制臺(tái)打印:
Sending message...
Received <Hello from RabbitMQ!>
總結(jié)
恭喜惊窖!你剛才已經(jīng)學(xué)會(huì)了如何通過spring raabitmq去構(gòu)建一個(gè)消息發(fā)送和訂閱的程序刽宪。 這僅僅是一個(gè)好的開始,你可以通過spring-rabbitmq做更多的事界酒,點(diǎn)擊這里圣拄。
源碼下載:https://github.com/forezp/SpringBootLearning
參考資料
https://spring.io/guides/gs/messaging-rabbitmq/
優(yōu)秀文章推薦:
- 更多springboot 教程:springBoot非官方教程 | 文章匯總
- 更多springcoud 教程:史上最簡(jiǎn)單的 SpringCloud 教程 | 文章匯總