RabbitMq可以发送对象数据,同一个队列可以发送不同类型的数据对象,同一个队列也可以接受不同类型的数据对象。
可以通过BasicProperties传输类型,如下所示。
AMQP.BasicProperties properties = new AMQP.BasicProperties().builder()
.deliveryMode(2) // 传送方式
.contentEncoding("UTF-8") // 编码方式
.expiration("10000") // 过期时间
.type("user")
.build();
在消费者消费消息的时候,可以根据的类型转换成对应的对象类型。
生产者发送数据代码
import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
public class Producer {
public final static String QUEUE_NAME="rabbitMQtest1";
public static void main(String[] args) throws IOException, TimeoutException {
//创建连接工厂
ConnectionFactory factory = new ConnectionFactory();
//设置RabbitMQ相关信息
factory.setHost("localhost");
factory.setUsername("guest");
factory.setPassword("guest");
factory.setPort(5672);
//创建一个新的连接
Connection connection = factory.newConnection();
//创建一个通道
Channel channel = connection.createChannel();
// 声明一个队列
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
String message = "Hello RabbitMQ";
User user = new User();
user.setUsername("zhangsan");
user.setPassword("123456");
AMQP.BasicProperties properties = new AMQP.BasicProperties().builder()
.deliveryMode(2) // 传送方式
.contentEncoding("UTF-8") // 编码方式
.expiration("10000") // 过期时间
.type("user")
.build();
int i = 0;
while (i < 100) {
channel.basicPublish("", QUEUE_NAME, properties, JSON.toJSONString(user).getBytes("UTF-8"));
i++;
System.out.println("producer send user" + user);
}
Goods goods = new Goods();
goods.setGoodnames("goodsname");
goods.setPrice("100元");
AMQP.BasicProperties props = new AMQP.BasicProperties().builder()
.deliveryMode(2) // 传送方式
.contentEncoding("UTF-8") // 编码方式
.expiration("10000") // 过期时间
.type("goods")
.build();
i = 0;
while (i < 100) {
channel.basicPublish("", QUEUE_NAME, props, JSON.toJSONString(goods).getBytes("UTF-8"));
i++;
System.out.println("producer send goods" + goods);
}
channel.close();
connection.close();
}
}
消费者消费消息
import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.*;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
public class Customer {
private final static String QUEUE_NAME = "rabbitMQtest1";
public static void main(String[] args) throws IOException, TimeoutException {
// 创建连接工厂
ConnectionFactory factory = new ConnectionFactory();
//设置RabbitMQ地址
factory.setHost("localhost");
//创建一个新的连接
Connection connection = factory.newConnection();
//创建一个通道
Channel channel = connection.createChannel();
//声明要关注的队列
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
System.out.println("Customer Waiting Received messages");
//DefaultConsumer类实现了Consumer接口,通过传入一个频道,
// 告诉服务器我们需要那个频道的消息,如果频道中有消息,就会执行回调函数handleDelivery
Consumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope,
AMQP.BasicProperties properties, byte[] body)
throws IOException {
String type = properties.getType();
if ("user".equals(type)) {
String resultBody = new String(body);
System.out.println("type =" + properties.getType());
User user = (User)JSON.parseObject(resultBody, User.class);
System.out.println("customer Received" + user);
}
if ("goods".equals(type)){
String resultBody = new String(body);
System.out.println("type =" + properties.getType());
Goods goods = (Goods)JSON.parseObject(resultBody, Goods.class);
System.out.println("customer Received" + goods);
}
}
};
//自动回复队列应答 -- RabbitMQ中的消息确认机制
channel.basicConsume(QUEUE_NAME, true, consumer);
}
}
主要依赖的maven文件
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>3.6.5</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.22</version>
</dependency>
附上两个对象
public class User {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "user username:" + username + " password:"+ password;
}
}
public class Goods {
private String goodnames;
private String price;
public String getGoodnames() {
return goodnames;
}
public void setGoodnames(String goodnames) {
this.goodnames = goodnames;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
@Override
public String toString() {
return "user goodnames:" + goodnames + " price:" + price;
}
}