How to put a msg to queue as json format using Spring Integration AMQP

767 Views Asked by At

Currently I'm trying to put a message to queue with json format. Below it's my code snippet but it does not work.

return IntegrationFlows.from(Amqp.inboundAdapter(connectionFactory, NOTE_INCOMING_QUEUE)
                .concurrentConsumers(2))
                .transform(new JsonToObjectTransformer(Note.class))
                .handle(Note.class, (note, header) -> {
                    // doing something
                    return note;
                })
                .channel(Amqp.channel(connectionFactory).queueName(NOTE_OCRED_QUEUE).messageConverter(
                        new MappingJackson2MessageConverter()))
                .get();

The message was putted in queue as application/x-java-serialized-object.

1

There are 1 best solutions below

0
On BEST ANSWER

Two problems:

  1. AMQP-backed channels are intended for persistence, not for simply sending messages to RabbitMQ; by default the whole message is serialized (using the RabbitTemplate's converter, not the channel's).
  2. Message converters on channels are only used on channels for converting DataTypes, not for serialization.

Use an outbound channel adapter...

.handle(Amqp.outboundAdapter(rabbitTemplate).routingKey(NOTE_OCRED_QUEUE));

Where the RabbitTemplate is configured with a Jackson2JsonMessageConverter.