消费不同的消息类型序列化为json
问题描述:
我有一个PHP中的生产者和Java中的消费者,它们将通过RabbitMQ进行通信。他们将使用三种不同的消息类型。如果制片人也是一个Java应用程序,我可以只序列化对象为原料,然后在消费者做:消费不同的消息类型序列化为json
Consumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
Object deserializedBody = SerializationUtils.deserialize(body);
if (deserializedBody instanceof TypeOne) {
TypeOne typeOne = (TypeOne) deserializedBody;
// process with corresponding code
} else if (deserializedBody instanceof TypeTwo) {
TypeTwo typeTwo = (TypeTwo) deserializedBody;
// process with corresponding code
} else if (deserializedBody instanceof TypeThree) {
TypeThree typeThree = (TypeThre) deserializedBody;
// process with corresponding code
} else {
// throw exception
}
}
};
但由于我的制片人是在PHP中,我将不得不序列化消息JSON字符串。
我该如何区分三种消息类型?
答
AMQP中的消息具有属性,您可以定义一个供您自己使用的指定消息类型的属性。但大多数时候人们选择使用“内容类型”和“内容编码”。