Spring Boot教程(4)-日志配置-logback和使用。
Spring Boot教程(5)-web应用开发-模板引擎Thymeleaf
Spring Boot教程(8)-使用SQL关系型数据库-JdbcTemplate
Spring Boot教程(9)-Spring-data-jpa
Spring Boot教程(10)-使用SQL关系型数据库-h2嵌入式数据库的使用
Spring Boot教程(12)-redis缓存设置,幂等性防重复提交
Spring Boot教程(13)-使用异步消息服务-JMS(ActiveMQ)
Spring Boot教程(14)-使用异步消息服务-AMQP(RabbitMQ)
Spring Boot教程(16)-集成Druid阿里数据库连接池
Spring Boot教程(17)-Spring Boot集成Swagger
需要安装RabbitMQ服务要不然会报错链接失败
RabbitMQ下载地址:http://www.rabbitmq.com/download.html
一、 添加依赖
<!-- amqp -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
二、配置文件
默认注释,调用本地服务。
# RABBIT (RabbitProperties)
#spring.rabbitmq.host=localhost
#spring.rabbitmq.port=5672
#spring.rabbitmq.password=
#spring.rabbitmq.username=
三、main方法加注解
@EnableRabbit
四、配置类
package com.wsj.springbootdemo.config;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 项目名称:AmqpConfiguration;
* 类 名 称:AmqpConfiguration;
* 类 描 述:TODO ;
* 创 建 人:Angus;
* 创建时间:2020/5/7 15:58;
*
* @version:1.0
**/
@Configuration
public class AmqpConfiguration {
@Bean
public Queue queue() {
return new Queue("wsj.queue");
}
}
package com.wsj.springbootdemo.component;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* 项目名称:WsjAmqpComponent;
* 类 名 称:WsjAmqpComponent;
* 类 描 述:TODO ;
* 创 建 人:Angus;
* 创建时间:2020/5/7 15:59;
*
* @version:1.0
**/
@Component
public class WsjAmqpComponent {
@Autowired
private AmqpTemplate amqpTemplate;
public void send(String msg) {
this.amqpTemplate.convertAndSend("wsj.queue", msg);
}
@RabbitListener(queues = "wsj.queue")
public void receiveQueue(String text) {
System.out.println("接受到:" + text);
}
}
五、测试
package com.wsj.springbootdemo.component;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest
class WsjAmqpComponentTest {
@Autowired
private WsjAmqpComponent wsjAmqpComponent;
@Test
void send() {
wsjAmqpComponent.send("hello world");
}
}