@@ RabbitListener(queues =“ MyQueue”)在Spring项目中不起作用?

问题描述 投票:1回答:2

我正在尝试在我的[Spring]应用程序中实现Rabbitmq,而不是spring boot。所以我添加了此配置 import org.springframework.amqp.core.AmqpAdmin; import org.springframework.amqp.core.Queue; import org.springframework.amqp.rabbit.connection.CachingConnectionFactory; import org.springframework.amqp.rabbit.connection.ConnectionFactory; import org.springframework.amqp.rabbit.core.RabbitAdmin; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class RabbitConfiguration { @Bean public ConnectionFactory connectionFactory() { CachingConnectionFactory connectionFactory = new CachingConnectionFactory("localhost"); return connectionFactory; } @Bean public AmqpAdmin amqpAdmin() { return new RabbitAdmin(connectionFactory()); } @Bean public RabbitTemplate rabbitTemplate() { return new RabbitTemplate(connectionFactory()); } @Bean public Queue myQueue() { return new Queue("MyQueue"); } }

然后从我使用过的服务类别中:-

public void sendViaTemplate(String msg){ ApplicationContext context = new AnnotationConfigApplicationContext(RabbitConfiguration.class); RabbitTemplate template = context.getBean(RabbitTemplate.class); template.convertAndSend(QUEUE_NAME,"Hello from template "+msg); } @RabbitListener(queues = "MyQueue") public void ListenToMyQueue( String in){ System.out.println("New Msg arrived"+in); }

convertAndSend似乎按预期工作,但是当将消息推送到队列中时,应该在将新元素插入队列中时自动执行ListenToMyQueue,对吗?为什么这不起作用?

我正在尝试在Spring应用程序中实现Rabbitmq,而不是spring boot。因此,我添加了此配置import org.springframework.amqp.core.AmqpAdmin;导入org.springframework.amqp.core.Queue; ...

java spring rabbitmq
2个回答
0
投票
您需要在SimpleRabbitListenerContainerFactory类中实现RabbitConfiguration

0
投票
为了将来参考,经过数天的尝试,最终该侦听器配置按预期工作。 enter link description here
© www.soinside.com 2019 - 2024. All rights reserved.