【发布时间】:2026-02-07 04:45:01
【问题描述】:
我有一个 PHP 生产者和一个 Java 消费者,它们将通过 RabbitMQ 进行通信。他们将使用三种不同的消息类型。如果生产者也是 Java 应用程序,我可以将对象序列化为原始对象,然后在消费者中执行:
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 字符串。
然后我如何区分这三种消息类型?
【问题讨论】: