Search code examples
rabbitmqpriority-queuespring-cloud-stream

How to send a message with priority with Spring cloud stream binder


I'm using rabbitmq. I've defined a queue with priority, but when I have to send a message with priority, I don't know how to specify the priority on it.

This is my code

StreamProcessor.java

public interface StreamProcessor {
  public static final String TEST_JOB_OUTPUT = "test-job-output";

  @Output(StreamProcessor.TEST_JOB_OUTPUT)
  MessageChannel testJobOutput();

}

MessageSender.java

@Autowired
@Qualifier(StreamProcessor.TEST_JOB_OUTPUT)
private MessageChannel testJobOutput;

public void sendMessage(String s, MessagePriority priority) {
    testJobOutput.send(MessageBuilder.withPayload(s).build());      
}

I've tried to specify an header, "x-priority" on the message, but don't seems to work.

public void sendMessage(String s, MessagePriority priority) {
        testJobOutput.send(MessageBuilder.withPayload(s).setHeader("x-priority", 10).build());
    }

Solution

  • Found the solution by try and failure... It's priority not x-priority

    public class MessageSender {
    
        public static final String MESSAGE_HEADER_PRIORITY = "priority";
    
        @Autowired
        @Qualifier(StreamProcessor.TEST_JOB_OUTPUT)
        private MessageChannel testJobOutput;
    
        public void sendMessage(String s, MessagePriority priority) {
            testJobOutput.send(MessageBuilder.withPayload(s).setHeader(MESSAGE_HEADER_PRIORITY, priority.value()).build());
        }
    
    }