pom
首先导入rabbitmq依赖(启动器)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit-test</artifactId>
<scope>test</scope>
</dependency>
配置文件
配置好host(主机地址),
username(账户名),
password(密码),
port(端口号)默认5672,可不写
virtual-host(虚拟主机),默认“ / ”
spring.rabbitmq.host=localhost
spring.rabbitmq.username=123
spring.rabbitmq.password=123
#spring.rabbitmq.port=5672
spring.rabbitmq.virtual-host=/vhost_123
测试
通过自动注入 RabbitTemplate rabbitTemplate , 即可使用。
使用 rabbitTemplate可以发送(convertAndSend)和接收消息(receiveAndConvert)
通过自动注入“AmqpAdmin amqpAdmin” , 可以手动管理队列和交换器。绑定交换器和队列。
amqpAdmin.declareExchange(new DirectExchange("amqp.direct.exchange"));
amqpAdmin.declareQueue(new Queue("amqp.queue"));
amqpAdmin.declareBinding(new Binding("amqp.queue", Binding.DestinationType.QUEUE,"amqp.direct.exchange","amqp.admin",null));
package com.test.amqp;
import com.test.amqp.bean.Book;
import org.junit.jupiter.api.Test;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
@SpringBootTest
class Apringboot02AmqpApplicationTests {
@Autowired
RabbitTemplate rabbitTemplate;
@Autowired
AmqpAdmin amqpAdmin;
@Test
public void createExchange() {
// amqpAdmin.declareExchange(new DirectExchange("amqp.direct.exchange"));
// amqpAdmin.declareQueue(new Queue("amqp.queue"));
amqpAdmin.declareBinding(new Binding("amqp.queue", Binding.DestinationType.QUEUE,"amqp.direct.exchange","amqp.admin",null));
}
@Test
void contextLoads() {
// rabbitTemplate.send();
Map<String, Object> map = new HashMap<>();
map.put("msg", "第一第一");
map.put("data", Arrays.asList(true, 123, "hello"));
rabbitTemplate.convertAndSend("exchange.direct", "test.news", map);
}
@Test
public void receive() {
Object rec = rabbitTemplate.receiveAndConvert("test.news");
System.out.println(rec.getClass());
System.out.println(rec);
}
@Test
public void sendMsg() {
rabbitTemplate.convertAndSend("exchange.fanout", "", new Book("三国", "luo"));
}
}
使用JSON发送
new一个Jackson2JsonMessageConverter返回,即可把传送消息转换为JSON。
使用这个转化器,Bean对象必须有一个空参构造方法,否则会出异常。
package com.test.amqp.config;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author Aoy
* @create 2020-03-24 17:48
*/
@Configuration
public class MyAMQPConfig {
@Bean
public MessageConverter messageConverter(){
return new Jackson2JsonMessageConverter();
}
}
bean
package com.test.amqp.bean;
/**
* @author Aoy
* @create 2020-03-24 18:01
*/
public class Book {
String name;
String author;
public Book() {
}
public Book(String name, String author) {
this.name = name;
this.author = author;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
'}';
}
}
开启
主类必须加上@EnableRabbit ,才能使用
package com.test.amqp;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableRabbit
@SpringBootApplication
public class Apringboot02AmqpApplication {
public static void main(String[] args) {
SpringApplication.run(Apringboot02AmqpApplication.class, args);
}
}
使用
方法加上@RabbitListener(queues = “test.news”),参数queue写上要接收队列。
方法参数写上要接收类型的参数。
参数写上Message message,可以接收message类,message有头信息和消息体。
package com.test.amqp.service;
import com.test.amqp.bean.Book;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;
/**
* @author Aoy
* @create 2020-03-24 18:30
*/
@Service
public class BookService {
@RabbitListener(queues = "test.news")
public void receice(Book book){
System.out.println("收到消息 :" +book);
}
@RabbitListener(queues = "test.news")
public void receice02(Message message){
System.out.println(message.getBody());
System.out.println(message.getMessageProperties());
}
}