I am attempting to send messages via a Spring Cloud Stream Source. The application receiving the message to requires the id
header to be present. I am unable to change this requirement, so I need to find a way to ensure that the id
header is mapped to my outgoing message.
I noticed that the id
header was being mapped to a message_id
property. After a bit more digging I realised that it is because the id
header is declared as transient here.
Is there a way to ensure that the id
header persists when the message is sent?
I was unable to solve this using Spring Cloud Stream. Instead I reverted to using Spring AMQP.
Here is my solution:
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.UUID;
@Service
public class ExampleService {
String exchange = "exchangeName";
String queue = "queueName";
@Autowired
RabbitTemplate rabbitTemplate;
public void sendMessage() {
final var jsonToQueue = "{\"message\":\"test message\"}";
final var message = MessageBuilder.withBody(jsonToQueue.getBytes())
.setContentType(MessageProperties.CONTENT_TYPE_JSON)
.setHeader("id", UUID.randomUUID())
.build();
rabbitTemplate.send(exchange, queue, message);
}