Alex
Alex

Reputation: 7491

How to properly create RabbitMQ custom message from Java bean with Spring Integration?

I am new in Spring Integration and maybe this question is trivial. I am looking at the example (https://github.com/spring-projects/spring-integration-samples/tree/master/basic/amqp) creating RabbitMq message from stdin:

<int-stream:stdin-channel-adapter id="consoleIn"
    channel="toRabbit">
    <int:poller fixed-delay="1000" max-messages-per-poll="1" />
</int-stream:stdin-channel-adapter>
<int:channel id="toRabbit" />
<int-amqp:outbound-channel-adapter
    channel="toRabbit" amqp-template="amqpTemplate"
    exchange-name-expression="payload.toLowerCase() == 'nack' ? 'badExchange' : 'si.test.exchange'"
    routing-key-expression="payload.toLowerCase() == 'fail' ? 'badKey' : 'si.test.binding'"
    confirm-correlation-expression="payload"
    confirm-ack-channel="good"
    confirm-nack-channel="errors"
    return-channel="returns" />

What if we need to have a custom message, produced in Java code. What will be the proper elegant code? The bean to populate the message is simplified:

package com.mycompany.domain.price;    
public class UpdateMessage implements Serializable {
Date effStartDate;
Date effEndDate;
Long orderId = -1;
String customerFullName;
...
}

Upvotes: 1

Views: 449

Answers (1)

Gary Russell
Gary Russell

Reputation: 174554

See this answer; although it's talking about Axis instead of RabbitMQ; the same techniques apply.

Since your UpdateMessage implements Serializable, the standard message converter will take care of the conversion to a byte[] for you.

Sending a message with no reply, your gateway interface method might be

public void send(UpdateMessage msg);

In which case you'd you use an outbound channel adapter. If you want to get a reply, use an amqp outbound gateway and the service interface might look like

public UpdateResult send(UpdateMessage msg);

If you're not using Serializable objects, use of a json converter might be appropriate instead.

Upvotes: 1

Related Questions