簡體   English   中英

Spring和AMQP RabbitMQ主題交換不起作用

[英]Spring and AMQP RabbitMQ topic exchange not working

我正在嘗試在我的春季應用程序上設置主題交流。 這是我的上下文配置:



    @Configuration
    public class IntegrationConfig {

        public final static String queueName = "my-queue";

        @Bean
        AmqpAdmin amqpAdmin(ConnectionFactory connectionFactory) {
            return new RabbitAdmin(connectionFactory);

        }

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

        @Bean
        TopicExchange exchange() {
            return new TopicExchange("my-exchange", false, true);
        }

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

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

        @Bean
        ImageUploadReceiver receiver() {
            return new ImageUploadReceiver();
        }

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

    }

這是接收器類:



    public class ImageUploadReceiver {
        private CountDownLatch latch = new CountDownLatch(1);

        public void receiveMessage(String message) {
            System.out.println("Received ");
            latch.countDown();
        }

        public CountDownLatch getLatch() {
            return latch;
        }
    }

這是發件人代碼:



    @RequestMapping("/sendmessage")
    @ResponseBody
    public String sendMessage() {
        rabbitTemplate.convertAndSend("ru.interosite.1", "ttt1233");
        try {
            imageUploadReceiver.getLatch().await(3, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "Msg received";
    }

因此,我正在使用綁定鍵“ ru.interosite.1”將消息發送到主題交換,並使用模式“ ru.interosite。*”將其綁定到隊列。 當嘗試從https://www.rabbitmq.com/tutorials/tutorial-five-java.html進行采樣時,我使用了這些鍵和模式,它們工作正常。

但是在String AMQP內部它不起作用,即接收器永遠不會被調用。 僅當綁定鍵和模式與我在使用DirectExchange時完全相同時才調用它。

我在這里想念什么嗎?

您沒有顯示RabbitTemplate的配置,但我想它是帶有默認選項的。

要將消息發送到my-exchange ,必須直接指定它:

rabbitTemplate.convertAndSend("my-exchange", "ru.interosite.1", "ttt1233");

您還可以在Rabbit模板中設置交換,如下所示:

@Configuration
public class IntegrationConfig {

    // ... as above

    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate template = new RabbitTemplate(connectionFactory);
        template.setExchange("my-exchange");
        return template;
    }

}

因此,您可以通過以下方式發送消息:

public class MyController {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @RequestMapping("/sendmessage")
    @ResponseBody
    public String sendMessage() {
        rabbitTemplate.convertAndSend("ru.interosite.1", "ttt1233");
        // ... as above
    }

}

暫無
暫無

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

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