I am trying to produce an amqp message from python and consume that same message from java/spring.
Here is my producer code (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))
Here is my consumer code (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());
Here is the java class for Person:
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;
}
}
and the corresponding python class:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
(located in trc/suivi/amqp directory structure)
I get a class cast exception. I am pretty sure this has to do either with the package/module name or with some serialization issue...
EDIT: I am now using JSon and I get this upon java deserialization:
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)
Are you using a common serialization library? Take a look at this answer for more details Is there any library to deserialize with python which is serialized with java.
You could try serializing into json and then it will be easy to deserialize that in your python code