Camel rabbitmq + convertSendAndReceive() : Could not convert incoming message with content-type [null]

11k views Asked by At

I have a component that sends a message to a worker service waiting back the result.

@Autowired
private RabbitTemplate rabbit;
[...]
Object response = rabbit.convertSendAndReceive("testQ", ...);

The worker service is implemented with Apache Camel rabbitmq route:

from("rabbitmq://localhost/myExchange?declare=false&routingKey=testQ&queue=testQ")
        .routeId("myCamelRoute")
        .process(myProcessor)
        .to("log:myLog");

myProcessor handles the message and logs out the Camel Message headers:

__TypeId__=...
breadcrumbId=...
rabbitmq.CONTENT_ENCODING=UTF-8
rabbitmq.CONTENT_TYPE=application/json
rabbitmq.CORRELATIONID=7e390b6b-d30f-4f26-ba44-33fb887db0e8
rabbitmq.DELIVERY_TAG=4
rabbitmq.EXCHANGE_NAME=
rabbitmq.PRIORITY=0
rabbitmq.REPLY_TO=amq.rabbitmq.reply-to.g2dkABNyYWJiaXRAOWU5ZjkxNDI4ZWRiAAAJgwAAADUC.5+kPXXxaXhoYo7A4T0HSZQ==
rabbitmq.ROUTING_KEY=testQ

The message header apparently contains rabbitmq.CONTENT_TYPE=application/json on the worker side but this info seems to get "lost" when the response message goes back:

o.s.a.s.c.Jackson2JsonMessageConverter : Could not convert incoming message with content-type [null]

Any idea what is wrong here?

3

There are 3 answers

0
user12716850 On

if spring-amqp:

@Bean
public CachingConnectionFactory cachingConnectionFactory() {
    final CachingConnectionFactory factory = new CachingConnectionFactory();
    factory.setUsername(username);
    factory.setPassword(password);
    factory.setHost(host);
    factory.setPort(port);
    return factory;
}

@Bean
@DependsOn("cachingConnectionFactory")
public RabbitTemplate rabbitTemplate(CachingConnectionFactory cachingConnectionFactory) {
    RabbitTemplate rabbitTemplate = new RabbitTemplate(cachingConnectionFactory);
    rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
    return rabbitTemplate;
}
0
Florian Waibel On

I've seen the same error using the RabbitMQ Management console when using a Headers field. Passing "content_type":"application/json" as a message Property worked fine.

0
Thiago Cavalcanti On

EDIT: Actually, it seems that chrome autocomplete is not working properly. I manually typed the property and worked fine too enter image description here


I faced the same issue. It seems to be a problem between RabbitMQ Management console and the Spring consumer.

Based on this, I override the fromMessage method from Jackson2JsonMessageConverter, and forced the contentType to be application/json, and worked fine

My stack was:

  • org.springframework: 4.3.6.RELEASE
  • org.springframework.amqp : 1.7.14.RELEASE
  • com.fasterxml.jackson.core: 2.11.2

import org.springframework.amqp.core.Message;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;

public class JacksonMessageConverter extends Jackson2JsonMessageConverter {
    public JacksonMessageConverter() {
        super();
    }
    
    @Override
    public Object fromMessage(Message message) {
        message.getMessageProperties().setContentType("application/json");
        return super.fromMessage(message);
    }
}

and then I used in my rabbit configuration

    @Bean
public MessageConverter messageConverter() {
    JacksonMessageConverter jsonMessageConverter = new JacksonMessageConverter();
    jsonMessageConverter.setClassMapper(classMapper());
    
    return jsonMessageConverter;
}

Other solution was to create a basic application in GO to publish the message, add the content-type and it worked fine, since the problems appears to be in RabbitMQ Management console.

My script in go:

// MqFactory - Creates a connection with mq
func MqFactory() *amqp.Connection {
    mqURI, err := amqp.ParseURI(createAmqpURI())
    if err != nil {
        fmt.Printf("Failed on parse mq uri: %s", err)
    }
    conn, err := amqp.Dial(mqURI.String())
    if err != nil {
        failOnError(err, "Failed to connect to MQ")
    } else {
        fmt.Println("Connection established with MQ")
    }

    sendMessage(conn)

    return conn
}

func sendMessage(conn *amqp.Connection) {
    channel, err := conn.Channel()
    if err != nil {
        failOnError(err, "Failed 1")
    }

    err2 := channel.Publish(
        "<exchange>",
        "<routin_key>",
        false,
        false,
        amqp.Publishing{
            Headers: amqp.Table{
                "__TypeId__": "<type_id>", // If needed
            },
            ContentType: "application/json",
            Body:        []byte("<body>"),
        },
    )

    if err2 != nil {
        failOnError(err2, "Failed 2")
    }
}

func createAmqpURI() string {
    host := os.Getenv("MQ_HOST")
    port := os.Getenv("MQ_PORT")
    usr := os.Getenv("MQ_USR")
    pwd := os.Getenv("MQ_PWD")
    return "amqp://" + usr + ":" + pwd + "@" + host + ":" + port
}

func failOnError(err error, msg string) {
    println(msg)
    if err != nil {
        fmt.Printf("%s: %s", msg, err)
    }
}