Saturday, January 5, 2019

Correlation data in RabbitMQ

Customising RabbitMQ Data:


rabbitTemplate.convertAndSend("", QUEUE, "foo", new CorrelationData("Correlation for message 1"));

/*
* Replace the correlation data with one containing the converted message in case
* we want to resend it after a nack.
*/
rabbitTemplate.setCorrelationDataPostProcessor((message, correlationData) ->
new CompleteMessageCorrelationData(correlationData != null ? correlationData.getId() : null, message));

rabbitTemplate.setConfirmCallback((correlation, ack, reason) -> {
if (correlation != null) {
System.out.println("Received " + (ack ? " ack " : " nack ") + "for correlation: " + correlation);
}
});
Static class CompleteMessageCorrelationData extends CorrelationData {
private final Message message;

CompleteMessageCorrelationData(String id, Message message) {
super(id);
this.message = message;
}

public Message getMessage() {
return this.message;
}

@Override
public String toString() {
return "CompleteMessageCorrelationData [id=" + getId() + ", message=" + this.message + "]";
}

}

No comments:

Post a Comment