【问题标题】:Spring and AMQP RabbitMQ topic exchange not workingSpring 和 AMQP RabbitMQ 主题交换不起作用
【发布时间】:2015-04-10 11:03:40
【问题描述】:

我正在尝试在我的 spring 应用程序上设置主题交换。 这是我的上下文配置:

@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 完全相同时才会调用它。

我错过了什么吗?

【问题讨论】:

    标签: java spring rabbitmq spring-amqp


    【解决方案1】:

    您没有显示RabbitTemplate 的配置,但我猜它是默认选项。

    要向my-exchange 发送消息,您必须直接指定它:

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

    【讨论】:

    • 非常感谢!当然我应该猜到模板不知道交换名称。
    【解决方案2】:

    你也可以像这样在兔子模板中设置交换:

    @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
        }
    
    }
    

    【讨论】:

      最近更新 更多