参照自己写的上篇:https://blog.csdn.net/weixin_39868387/article/details/102961125
搭建步骤:
1、使用初始化Spring Initializr快速搭建工程
工程目录,引入了两个jar文件
2、在resource目录的application.properties全局配置文件做以下参数的配置
application.properties
spring.rabbitmq.host=192.168.219.5
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
3、在test目录下的SpringbootAmqpApplicationTests类编写
package com.study.amqp;
import org.junit.jupiter.api.Test;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.awt.print.Book;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
@SpringBootTest
class SpringbootAmqpApplicationTests {
@Autowired
RabbitTemplate rabbitTemplate;
/**
* 1、单播(点对点)
*/
@Test
public void contextLoads() {
//Message需要自己构造一个;定义消息体内容和消息头
//rabbitTemplate.send(exchage,routeKey,message);
//object默认当成消息体,只需要传入要发送的对象,自动序列化发送给rabbitmq;
//rabbitTemplate.convertAndSend(exchage,routeKey,object);
Map<String,Object> map = new HashMap<>();
map.put("msg","这是第一个消息");
map.put("data", Arrays.asList("helloworld",123,true));
//对象被默认序列化以后发送出去
rabbitTemplate.convertAndSend("exchange.direct","china.news",map);
}
}
4、运行,日志打印信息:
在rabbitmq的信息队列多了一条消息
5、接受数据,如何将数据自动的转为json发送出去
//接受数据,如何将数据自动的转为json发送出去
@Test
public void receive(){
Object o = rabbitTemplate.receiveAndConvert("china.news");
System.out.println(o.getClass());
System.out.println(o);
}
单播(点对点),运行,日志打印信息
6、在java目录下的config创建MyAMQPConfig类
package com.study.amqp.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
@Configuration
public class MyAMQPConfig {
@Bean
public MessageConverter messageConverter(){
return new Jackson2JsonMessageConverter();
}
}
package com.study.amqp.bean;
public class Book {
private String bookName;
private String author;
public Book() {
}
public Book(String bookName, String author) {
this.bookName = bookName;
this.author = author;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
@Override
public String toString() {
return "Book{" +
"bookName='" + bookName + '\'' +
", author='" + author + '\'' +
'}';
}
}
接着,在SpringbootAmqpApplicationTests类添加book对象信息
rabbitTemplate.convertAndSend("exchange.direct","china.news",new Book("西游记","吴承恩"));
运行contextLoads单元测试,打印日志信息:
广播:都能收到消息
代码编写:
/**
* 广播
*/
@Test
public void sendMsg(){
rabbitTemplate.convertAndSend("exchange.fanout","",new Book("红楼梦","曹雪芹"));
}
BookService.java
package com.study.amqp.service;
import com.study.amqp.bean.Book;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;
@Service
public class BookService {
@RabbitListener(queues = "china.news")
public void receive(Book book){
System.out.println("收到消息:"+book);
}
@RabbitListener(queues = "china.one")
public void receive02(Message message){
System.out.println(message.getBody());
System.out.println(message.getMessageProperties());
}
}
接着,当我们想创建交互机对象,添加代码:
@Autowired
AmqpAdmin amqpAdmin;
@Test
public void createExchange(){
amqpAdmin.declareExchange(new DirectExchange("amqpadmin.exchange"));
System.out.println("创建完成");
}