1.2.2 建立spring-boot-MQ工程
spring-boot-rabbitMQ
項目源碼,
pom.xml檔案:
<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<!-- spring boot項目的parent -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
</parent>
<groupId>com.wyait.mq</groupId>
<artifactId>spring-boot-mq</artifactId>
<version>1.0.0-SNAPSHOT</version>
<dependencies>
<dependency>
<!-- spring boot 引入Web子產品。自動配置:tomcat、springmvc、jackson等 -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<artifactId>spring-boot-starter-logging</artifactId>
<groupId>org.springframework.boot</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- test使用 -->
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<artifactId>spring-boot-starter-log4j</artifactId>
<version>1.3.2.RELEASE</version>
<!-- 整合rabbitmq,添加amqp依賴 -->
<artifactId>spring-boot-starter-amqp</artifactId>
</dependencies>
<build>
<plugins>
<plugin>
<!-- 配置spring boot之maven插件 -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Application啟動類:
@Configuration
@SpringBootApplication
public class MqApplication {
public static void main(String[] args) {
SpringApplication.run(MqApplication.class, args);
}
}
添加log4j.properties、application.properties、application-dev.properties配置檔案。
application-dev.properties配置檔案中添加rabbitmq配置:
spring.application.name=springboot-rabbitmq
spring.rabbitmq.host=127.0.0.1
spring.rabbitmq.port=5672
spring.rabbitmq.username=wyait
spring.rabbitmq.password=wyait
spring.rabbitmq.virtual-host=/
# p端收到回調,确認消息發送結果
spring.rabbitmq.publisher-confirms=true
spring.rabbitmq.publisher-returns=true
spring.rabbitmq.template.mandatory=true
編寫RabbitMqConfig配置類:
建立RabbitMQ的配置類RabbitMqConfig,用來配置隊列、交換器、路由等進階資訊。這裡我們以入門為主,先以最小化的配置來定義,以完成一個基本的生産和消費過程。
public class RabbitMqConfig {
@Bean
public Queue helloQueue() {
return new Queue("hello");
編寫Sender類:
建立消息生産者Sender。通過注入AmqpTemplate接口的執行個體來實作消息的發送,AmqpTemplate接口定義了一套針對AMQP協定的基礎操作。在Spring Boot中會根據配置來注入其具體實作。在該生産者,我們會産生一個字元串,并發送到名為hello的隊列中。
@Component
public class Sender {
@Autowired
private AmqpTemplate rabbitMQTemplate;
public void send() {
String context = "hello :" + new Date();
System.out.println("Sender : " + context);
this.rabbitMQTemplate.convertAndSend("hello",context);
編寫Recevier類:
建立消息消費者Receiver。通過@RabbitListener注解定義該類對hello隊列的監聽,并用@RabbitHandler注解來指定對消息的處理方法。是以,該消費者實作了對hello隊列的消費,消費操作為輸出消息的字元串内容。
// 監聽“hello”隊列
@RabbitListener(queues ="hello")
public class Receiver {
@RabbitHandler
// handler注解來指定對消息的處理方法
public void process(String hello) {
System.out.println("Receiver:" + hello);
編寫test類:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes =MqApplication.class)
public class MqApplicationTest{
private Sender send;
@Test
public void test() {
System.out.println("==========發送消息!");
send.send();
完成程式編寫之後,下面開始嘗試運作。首先確定RabbitMQ Server已經開始,然後進行下面的操作:
啟動應用主類,從控制台中,我們看到如下内容,程式建立了一個通路127.0.0.1:5672中wyait的連接配接。
Created newconnection: rabbitConnectionFactory#1e456bc3:0/SimpleConnection@2d428d9b [delegate=amqp://[email protected]:5672/,localPort= 2801]
同時,我們通過RabbitMQ的控制台,可以看到Connection和Channels中包含目前連接配接的條目,Queues中檢視隊列概況。
<a href="https://s2.51cto.com/oss/201710/30/289a7434ec7cf248b080401f87338d95.png" target="_blank"></a>
運作單元測試類,我們可以看到控制台中輸出下面的内容,消息被發送到了RabbitMQ Server的hello隊列中。
==========發送消息!
Sender : hello:Thu Sep 14 16:06:54 CST 2017
<a href="https://s3.51cto.com/oss/201710/30/d93b67b08a2393c8b60220d9d6290440.png" target="_blank"></a>
切換到應用主類的控制台,我們可以看到類似如下輸出,消費者對hello隊列的監聽程式執行了,并輸出了接受到的消息資訊。
<a href="https://s5.51cto.com/oss/201710/30/4be6ff2481d59291309d0cf3d3a2553a.png" target="_blank"></a>
通過上面的示例,我們在Spring Boot應用中引入spring-boot-starter-amqp子產品,進行簡單配置就完成了對RabbitMQ的消息生産和消費的開發内容。然而在實際應用中,還有很多内容沒有示範,下面繼續研究其他消息隊列模式,大家也可以查閱RabbitMQ的官方教程,有更全面的了解。
本文轉自 wyait 51CTO部落格,原文連結:http://blog.51cto.com/wyait/1977527,如需轉載請自行聯系原作者