@RabbitListener(queues = "MyQueue") 在 Spring 项目中不起作用? [英] @RabbitListener(queues = "MyQueue") does not work in Spring project?

查看:475
本文介绍了@RabbitListener(queues = "MyQueue") 在 Spring 项目中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的 Spring 应用程序中实现 rabbitmq,而不是 Spring Boot.所以我加了这个配置

<预><代码>导入 org.springframework.amqp.core.AmqpAdmin;导入 org.springframework.amqp.core.Queue;导入 org.springframework.amqp.rabbit.connection.CachingConnectionFactory;导入 org.springframework.amqp.rabbit.connection.ConnectionFactory;导入 org.springframework.amqp.rabbit.core.RabbitAdmin;导入 org.springframework.amqp.rabbit.core.RabbitTemplate;导入 org.springframework.context.annotation.Bean;导入 org.springframework.context.annotation.Configuration;@配置公共类 RabbitConfiguration {@豆角,扁豆公共连接工厂连接工厂(){CachingConnectionFactory connectionFactory =new CachingConnectionFactory("localhost");返回连接工厂;}@豆角,扁豆公共 AmqpAdmin amqpAdmin() {返回新的 RabbitAdmin(connectionFactory());}@豆角,扁豆公共 RabbitTemplate rabbitTemplate() {返回新的 RabbitTemplate(connectionFactory());}@豆角,扁豆公共队列 myQueue() {返回新队列(我的队列");}}

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

public void sendViaTemplate(String msg){应用上下文上下文 =新的 AnnotationConfigApplicationContext(RabbitConfiguration.class);RabbitTemplate 模板 = context.getBean(RabbitTemplate.class);template.convertAndSend(QUEUE_NAME,"你好来自模板"+msg);}@RabbitListener(queues = "MyQueue")公共无效ListenToMyQueue(字符串输入){System.out.println("新消息到达"+in);}

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

解决方案

供日后参考,经过多日的尝试,最终此监听器配置按预期工作.在此处输入链接描述

I am trying to implement rabbitmq in my Spring application, not spring boot. So I added this configuration


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");
    }


}

Then from my service class I have used:-

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);
    }

The convertAndSend seems working as expected, but When the message is pushed into the queue, the ListenToMyQueue should be auto executed as new element is inserted into the queue, right? Why this is not working?

解决方案

For future refference, after many days of try, finally this listener configuration is working as expected. enter link description here

这篇关于@RabbitListener(queues = "MyQueue") 在 Spring 项目中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆