我正在尝试从 python 生成一条 amqp 消息并使用来自 java/spring 的相同消息。
这是我的生产者代码(python):
import pika, sys, pickle
sys.path.append("trc/suivi/amqp")
from Person import Person
connection = pika.BlockingConnection()
channel = connection.channel()
me = Person("Juliano", 38)
pickled_me = pickle.dumps(me)
channel.basic_publish(exchange='',
routing_key="myqueue",
body=pickled_me,
properties=pika.BasicProperties(delivery_mode=1))
这是我的消费者代码(java):
ApplicationContext context = new GenericXmlApplicationContext("classpath:/applicationContext.xml");
AmqpTemplate template = context.getBean(AmqpTemplate.class);
Person me = (Person) template.receiveAndConvert("myqueue");
System.out.println("Me: " + me.getName() + ":" + me.getAge());
这是 Person 的 java 类:
package trc.suivi.amqp;
import java.io.Serializable;
public class Person implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
和相应的python类:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
(位于trc/suivi/amqp目录结构中)
我得到一个类转换异常。我很确定这与包/模块名称或一些序列化问题有关......
编辑:我现在正在使用 JSon,我在 java 反序列化时得到了这个:
Exception in thread "main" org.springframework.amqp.support.converter.MessageConversionException: failed to convert Message content. Could not resolve
__TypeId__ in header
at org.springframework.amqp.support.converter.DefaultJavaTypeMapper.retrieveHeader(DefaultJavaTypeMapper.java:104)
at org.springframework.amqp.support.converter.DefaultJavaTypeMapper.toJavaType(DefaultJavaTypeMapper.java:53)
at org.springframework.amqp.support.converter.JsonMessageConverter.fromMessage(JsonMessageConverter.java:118)
at org.springframework.amqp.rabbit.core.RabbitTemplate.receiveAndConvert(RabbitTemplate.java:425)
at trc.suivi.amqp.Consumer.main(Consumer.java:12)