Domanda

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)
È stato utile?

Soluzione

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

Altri suggerimenti

The key to this is using a standard message format which can be encoded and decoded in many languages. Many people just use JSON in UTF-8-encoded strings, but I have found MessagePack to be almost as flexible as JSON, and more bandwidth efficient. http://msgpack.org/

I like to use an object-style format because it is easy to add data to a message and then send it on to another queue. I have done that to deal with handling errors (by adding an error reason code and dispatching a message to a queue where the handler will examine the reason and either retry or fix and retry). Also, adding timestamps has been useful for charting transit times through the system. After handling a message (which will have timestamps from several previous steps), it is sent to a grapher which records the time in queue and time to handle for each step.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top