增加rabbitMQ 用例
This commit is contained in:
@ -0,0 +1,58 @@
|
||||
package com.heibaiying.rabbit;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.heibaiying.bean.ProductManager;
|
||||
import com.heibaiying.bean.Programmer;
|
||||
import com.heibaiying.constant.Type;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.amqp.core.Message;
|
||||
import org.springframework.amqp.core.MessageProperties;
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description : 传输对象
|
||||
*/
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(classes = RabbitBaseConfig.class)
|
||||
public class RabbitSendObjectTest {
|
||||
|
||||
@Autowired
|
||||
private RabbitTemplate rabbitTemplate;
|
||||
|
||||
@Test
|
||||
public void sendProgrammer() throws JsonProcessingException {
|
||||
MessageProperties messageProperties = new MessageProperties();
|
||||
//必须设置 contentType为 application/json
|
||||
messageProperties.setContentType("application/json");
|
||||
// 必须指定类型
|
||||
messageProperties.getHeaders().put("__TypeId__", Type.PROGRAMMER);
|
||||
Programmer programmer = new Programmer("xiaoming", 34, 52200.21f, new Date());
|
||||
// 序列化与反序列化都使用的Jackson
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
String programmerJson = mapper.writeValueAsString(programmer);
|
||||
Message message = new Message(programmerJson.getBytes(), messageProperties);
|
||||
rabbitTemplate.send("objectTopic", "object", message);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void sendProductManager() throws JsonProcessingException {
|
||||
MessageProperties messageProperties = new MessageProperties();
|
||||
messageProperties.setContentType("application/json");
|
||||
messageProperties.getHeaders().put("__TypeId__", Type.MANAGER);
|
||||
ProductManager manager = new ProductManager("xiaohong", 21, new Date());
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
String managerJson = mapper.writeValueAsString(manager);
|
||||
Message message = new Message(managerJson.getBytes(), messageProperties);
|
||||
rabbitTemplate.send("objectTopic", "object", message);
|
||||
}
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
package com.heibaiying.rabbit;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.amqp.core.Message;
|
||||
import org.springframework.amqp.core.MessageProperties;
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description : 传输简单字符串
|
||||
*/
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@ContextConfiguration(classes = RabbitBaseConfig.class)
|
||||
public class RabbitTest {
|
||||
|
||||
@Autowired
|
||||
private RabbitTemplate rabbitTemplate;
|
||||
|
||||
@Test
|
||||
public void sendMessage() {
|
||||
MessageProperties properties = new MessageProperties();
|
||||
|
||||
String allReceived = "我的路由键 quick.orange.rabbit 符合queue1 和 queue2 的要求,我应该被两个监听器接收到";
|
||||
Message message1 = new Message(allReceived.getBytes(), properties);
|
||||
rabbitTemplate.send("topic01", "quick.orange.rabbit", message1);
|
||||
|
||||
String firstReceived = "我的路由键 quick.orange.fox 只符合queue1 的要求,只能被queue 1 接收到";
|
||||
Message message2 = new Message(firstReceived.getBytes(), properties);
|
||||
rabbitTemplate.send("topic01", "quick.orange.fox", message2);
|
||||
|
||||
String secondReceived = "我的路由键 lazy.brown.fox 只符合queue2 的要求,只能被queue 2 接收到";
|
||||
Message message3 = new Message(secondReceived.getBytes(), properties);
|
||||
rabbitTemplate.send("topic01", "lazy.brown.fox", message3);
|
||||
|
||||
String notReceived = "我的路由键 quick.brown.fox 不符合 topic1 任何绑定队列的要求,你将看不到我";
|
||||
Message message4 = new Message(notReceived.getBytes(), properties);
|
||||
rabbitTemplate.send("topic01", "quick.brown.fox", message4);
|
||||
|
||||
/*
|
||||
* SecondQueue收到消息:我的路由键 quick.orange.rabbit 符合queue1 和 queue2 的要求,我应该被两个监听器接收到
|
||||
* FirstQueue收到消息:我的路由键 quick.orange.rabbit 符合queue1 和 queue2 的要求,我应该被两个监听器接收到
|
||||
* FirstQueue收到消息:我的路由键 quick.orange.fox 只符合queue1 的要求,只能被queue 1 接收到
|
||||
* SecondQueue收到消息:我的路由键 lazy.brown.fox 只符合queue2 的要求,只能被queue 2 接收到
|
||||
*/
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user