spring boot 整合 RabbitMQ 测试

  作者:记性不好的阁主

0、准备


保证本地RabbitMQ服务已经启动


创建项目,使用spring初始化器添加RabbitMQ依赖





1、配置rabbitmq登录信息


配置文件:applicationn.properties


spring.rabbitmq.host=localhost

spring.rabbitmq.username=guest
spring.rabbitmq.password=guest



2、发送数据给rabbitmq


1) 点对点


发送数据


package com.laoxu.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.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@SpringBootTest
class SpringBoot02AmqpApplicationTests {

@Autowired
RabbitTemplate rabbitTemplate;

/**
* 1、单播(点对点)
*/
@Test
void contextLoads() {
Map<String,Object> map = new HashMap<>();
map.put("msg","第一个消息");
map.put("data", Arrays.asList("helloword",123,true));
rabbitTemplate.convertAndSend("exchange.direct","laoxu",map);
}

}



rabbitmq管理端查看获取到的消息




获取数据


package com.laoxu.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.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@SpringBootTest
class SpringBoot02AmqpApplicationTests {

@Autowired
RabbitTemplate rabbitTemplate;

/**
* 1、单播(点对点)
*/
@Test
void contextLoads() {
Map<String,Object> map = new HashMap<>();
map.put("msg","第一个消息");
map.put("data", Arrays.asList("helloword",123,true));
rabbitTemplate.convertAndSend("exchange.direct","laoxu",map);
}
@Test
void receive(){
Object o = rabbitTemplate.receiveAndConvert("laoxu");
System.out.println(o.getClass());
System.out.println(o);
}
}


控制台




将发送的数据序列化成json格式


创建配置类





package com.laoxu.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;

/**
* rabbitmq 序列化转为json格式
*/
@Configuration
public class MyAMQPConfig {

@Bean
public MessageConverter messageConverter(){
return new Jackson2JsonMessageConverter();
}

}


获取消息测试





控制台




3、测试自定义Bean的获取


创建Book类





package com.laoxu.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;
}
}


测试发送Book对象


package com.laoxu.amqp;

import com.laoxu.amqp.bean.Book;
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.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@SpringBootTest
class SpringBoot02AmqpApplicationTests {

@Autowired
RabbitTemplate rabbitTemplate;

/**
* 1、单播(点对点)
*/
@Test
void contextLoads() {
Map<String,Object> map = new HashMap<>();
map.put("msg","第二个消息");
map.put("data", Arrays.asList("helloword",123,true));
rabbitTemplate.convertAndSend("exchange.direct","laoxu",new Book("西游记","罗贯中"));
}
@Test
void receive(){
Object o = rabbitTemplate.receiveAndConvert("laoxu");
System.out.println(o.getClass());
System.out.println(o);
}
}



获取Book对象




获取对象内容则须重写toString()方法


package com.laoxu.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 + '\'' +
'}';
}
}






2) 广播 


@Test
void sendMessage(){
rabbitTemplate.convertAndSend("exchange.fanout","", new Book("三国","罗贯中"));
}



所有队列都接收到消息



java调用api创建交换器exchange


package com.laoxu.amqp;

import com.laoxu.amqp.bean.Book;
import org.junit.jupiter.api.Test;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.DirectExchange;
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 SpringBoot02AmqpApplicationTests {

@Autowired
AmqpAdmin amqpAdmin;

@Test
void createExchange(){
amqpAdmin.declareExchange(new DirectExchange("amqpAdmin.exchange"));
}


}


执行后,查看web管理端




java调用api创建队列


package com.laoxu.amqp;

import com.laoxu.amqp.bean.Book;
import org.junit.jupiter.api.Test;
import org.springframework.amqp.core.AmqpAdmin;
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 SpringBoot02AmqpApplicationTests {

@Autowired
AmqpAdmin amqpAdmin;


@Test
void createQueue(){
amqpAdmin.declareQueue(new Queue("amqpAdmin.queue"));
}


}


执行后查看web管理端




java调用api绑定队列到交换器


package com.laoxu.amqp;

import com.laoxu.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 SpringBoot02AmqpApplicationTests {

@Autowired
AmqpAdmin amqpAdmin;

@Test
void bindExchangeWithQueue(){
amqpAdmin.declareBinding(
new Binding(
"amqpAdmin.queue",
Binding.DestinationType.QUEUE,
"amqpAdmin.exchange",
"amqp.haha",
null
)
);
}


}


执行后查看web管理端









相关推荐

评论 抢沙发

表情

分类选择