Java 无法使用带有Spring引导的标头交换将消息发布到RabbitMQ上

Java 无法使用带有Spring引导的标头交换将消息发布到RabbitMQ上,java,spring,spring-boot,spring-rabbit,Java,Spring,Spring Boot,Spring Rabbit,我有一个非常基本的springboot应用程序,它使用headersexchange将两条消息发布到RabbitMQ。正在创建交换和队列,但消息未到达队列。我也没有看到任何例外 我在谷歌上搜索了一下,但找不到任何与此相关的例子 BasicApplication.java @SpringBootApplication public class BasicApplication { public static final String QUEUE_NAME = "helloworld.he

我有一个非常基本的
springboot
应用程序,它使用
headers
exchange将两条消息发布到
RabbitMQ
。正在创建
交换
队列
,但消息未到达队列。我也没有看到任何例外

我在谷歌上搜索了一下,但找不到任何与此相关的例子

BasicApplication.java

@SpringBootApplication
public class BasicApplication {

    public static final String QUEUE_NAME = "helloworld.header.red.q";
    public static final String EXCHANGE_NAME = "helloworld.header.x";

    //here the message ==> xchange ==> queue1, queue2
    @Bean
    public List<Object> headerBindings() {
        Queue headerRedQueue = new Queue(QUEUE_NAME, false);
        HeadersExchange headersExchange = new HeadersExchange(EXCHANGE_NAME);
        return Arrays.asList(headerRedQueue, headersExchange,
                bind(headerRedQueue).to(headersExchange).where("color").matches("red"));
    }

    public static void main(String[] args) {
        SpringApplication.run(BasicApplication.class, args).close();
    }

}
@Component
public class Producer implements CommandLineRunner {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public void run(String... args) throws Exception {
        MessageProperties messageProperties = new MessageProperties();

        //send a message with "color: red" header in the queue, this will show up in the queue
        messageProperties.setHeader("color", "red");
        //MOST LIKELY THE PROBLEM IS HERE
        //BELOW MESSAGE IS NOT LINKED TO ABOVE messageProperties OBJECT
        this.rabbitTemplate.convertAndSend(EXCHANGE_NAME, "", "Hello World !");

        //send another message with "color: gold" header in the queue, this will NOT show up in the queue
        messageProperties.setHeader("color", "gold");
        this.rabbitTemplate.convertAndSend(EXCHANGE_NAME, "", "Hello World !");
    }

}

您的正确之处在于,您创建的
MessageProperties
没有被使用

消息转换器的帮助下,尝试构建利用
消息属性的
消息

例如:

MessageProperties messageProperties = new MessageProperties();
messageProperties.setHeader("color", "red");
MessageConverter messageConverter = new SimpleMessageConverter();
Message message = messageConverter.toMessage("Hello World !", messageProperties);
rabbitTemplate.send("helloworld.header.x", "", message);

您还可以将
convertAndSend()
MessagePostProcessor
回调一起使用。