簡體   English   中英

如何在Spring Boot Rabbitmq中識別消息路由到的交換機?

[英]How to identify the exchange to which the message is routed to in Spring boot rabbitmq?

我是Spring AMQP的新手,只是想了解使用注釋的RabbitMQ Java配置。 這里是示例代碼。

發件人代碼-

@Component
public class Runner implements CommandLineRunner {
    private final RabbitTemplate rabbitTemplate;
    private final Receiver receiver;
    private final ConfigurableApplicationContext context;

    public Runner(Receiver receiver, RabbitTemplate rabbitTemplate,
            ConfigurableApplicationContext context) {
        this.receiver = receiver;
        this.rabbitTemplate = rabbitTemplate;
        this.context = context;
    }

    @Override
    public void run(String... args) throws Exception {
        System.out.println("Sending message...");
        rabbitTemplate.convertAndSend(Application.queueName, "Hello from RabbitMQ!");
        receiver.getLatch().await(10000, TimeUnit.MILLISECONDS);
        context.close();
    }

}

因此,在這里,在convertAndSend()中,僅指定了路由密鑰。 沒有給出交易所名稱。

配置文件如下圖所示-

@SpringBootApplication
public class Application {

    final static String queueName = "spring-boot";

    final static String HOST = "120.27.114.229";

    final static String USERNAME = "root";

    final static String PASSWORD = "root";

    final static int PORT = 5672;

    @Bean
    Queue queue() {
        return new Queue(queueName, false);
    }

    @Bean
    TopicExchange exchange() {
        return new TopicExchange("spring-boot-exchange");
    }

    @Bean
    Binding binding(Queue queue, TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(queueName);
    }

    @Bean  
    public ConnectionFactory connectionFactory() {  
          CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
          connectionFactory.setHost(HOST);
          connectionFactory.setPort(PORT);
          connectionFactory.setUsername(USERNAME);
          connectionFactory.setPassword(PASSWORD);
          connectionFactory.setVirtualHost("/");
          //����Ҫ����,��Ϣ�Ļص�
          connectionFactory.setPublisherConfirms(true); 
          return connectionFactory;
    } 

    @Bean
    SimpleMessageListenerContainer container(ConnectionFactory connectionFactory,
            MessageListenerAdapter listenerAdapter) {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.setQueueNames(queueName);
        container.setMessageListener(listenerAdapter);
        return container;
    }

    @Bean
    MessageListenerAdapter listenerAdapter(Receiver receiver) {
        return new MessageListenerAdapter(receiver, "receiveMessage");
    }

    public static void main(String[] args) throws InterruptedException {
        SpringApplication.run(Application.class, args);
    }

}

此處定義了TopicExchange bean,名稱為“ spring-boot-exchange”。 因此,發件人將消息發送到此交換機? 如果有兩個交換,發件人將消息發送到哪個交換? 請幫忙。

在這種情況下,您將發送到默認交換機""

rabbitTemplate.convertAndSend(Application.queueName, "Hello from RabbitMQ!");

不是您的主題交流。

要發送給明確的交易所,請使用

rabbitTemplate.convertAndSend("someExchange", "routingKey", "Hello from RabbitMQ!");

編輯

或者,您可以將默認交換配置為默認交換以外的其他內容。 template.setExchange("someExchange")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM