RabbitMQ中的@RabbitListener注解在Spring中不起作用(非spring-boot)

huangapple go评论76阅读模式
英文:

RabbitMQ @RabbitListener annotation not working in Spring (not spring-boot)

问题

这是我实现 RabbitMQ 在我的 Spring Core 开发的应用程序中。以下是我的 pom.xml 文件:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>5.2.6.RELEASE</version>
</dependency>
<dependency>
    <groupId>com.rabbitmq</groupId>
    <artifactId>amqp-client</artifactId>
    <version>5.9.0</version>
</dependency>
<dependency>
    <groupId>org.springframework.amqp</groupId>
    <artifactId>spring-amqp</artifactId>
    <version>2.2.7.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.amqp</groupId>
    <artifactId>spring-rabbit</artifactId>
    <version>2.2.7.RELEASE</version>
</dependency>

这是我的 RabbitConfiguration.java 文件:

import org.springframework.amqp.core.*;
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.amqp.rabbit.listener.MessageListenerContainer;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitConfiguration {
    public static final String QUEUE_NAME = "MyQueue";
    public static final String EXCHANGE_NAME = "My_EXCHANGE";

    @Bean
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory("localhost");
        return connectionFactory;
    }

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

    @Bean
    public RabbitTemplate rabbitTemplate() {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory());
        rabbitTemplate.setMessageConverter(jsonMessageConverter());
        return rabbitTemplate;
    }

    @Bean
    public Queue myQueue() {
        return new Queue(QUEUE_NAME, false);
    }

    @Bean
    MessageListenerContainer messageListenerContainer() {
        SimpleMessageListenerContainer simpleMessageListenerContainer = new SimpleMessageListenerContainer();
        simpleMessageListenerContainer.setConnectionFactory(connectionFactory());
        simpleMessageListenerContainer.setQueues(myQueue());
        simpleMessageListenerContainer.setMessageListener(new RabbitMQListner());
        return simpleMessageListenerContainer;
    }

    @Bean
    public MessageConverter jsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }
}

这是我从中尝试使用 Rabbit 的服务类:

import com.rabbitmq.client.*;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Component
public class MessagingService {
    public void send(String msg) {
        template.convertAndSend(QUEUE_NAME, msg);
    }

    @RabbitListener(queues = "foo")
    public void receiveMessage(String car) {
        System.out.println("Hello" + car);
    }

    @Bean
    private ApplicationContext context() {
        return new AnnotationConfigApplicationContext(RabbitConfiguration.class);
    }

    @Bean
    private AmqpTemplate amqpTemplate() {
        return context().getBean(AmqpTemplate.class);
    }

    @Autowired
    protected AmqpTemplate template;
}

send 方法正常工作。但是 @Listener 注释的方法不起作用。它不会监听。
我还尝试过,创建了另一个配置文件,并用 @EnableRabbit 进行了注释,但没有效果。是否有任何秘密方法可以使其正常工作?

以下是我的 AppConfig.java 文件:

import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@EnableRabbit
@Configuration
public class AppConfig extends RabbitConfiguration {
    @Bean
    public SimpleRabbitListenerContainerFactory myRabbitListenerContainerFactory() {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory());
        factory.setMaxConcurrentConsumers(5);
        factory.setMessageConverter(jsonMessageConverter());
        return factory;
    }
}
英文:

I am trying to implement rabbitmq in my spring-core developed application. Below is my pom.xml

    &lt;dependency&gt;
&lt;groupId&gt;org.springframework&lt;/groupId&gt;
&lt;artifactId&gt;spring-core&lt;/artifactId&gt;
&lt;version&gt;5.2.6.RELEASE&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;com.rabbitmq&lt;/groupId&gt;
&lt;artifactId&gt;amqp-client&lt;/artifactId&gt;
&lt;version&gt;5.9.0&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.amqp&lt;/groupId&gt;
&lt;artifactId&gt;spring-amqp&lt;/artifactId&gt;
&lt;version&gt;2.2.7.RELEASE&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework.amqp&lt;/groupId&gt;
&lt;artifactId&gt;spring-rabbit&lt;/artifactId&gt;
&lt;version&gt;2.2.7.RELEASE&lt;/version&gt;
&lt;/dependency&gt;

This is my RabbitConfiguration java file


import org.springframework.amqp.core.*;
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.amqp.rabbit.listener.MessageListenerContainer;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
@Configuration
public class RabbitConfiguration {
public static final String QUEUE_NAME=&quot;MyQueue&quot;;
public static final String EXCHANGE_NAME=&quot;My_EXCHANGE&quot;;
@Bean
public ConnectionFactory connectionFactory() {
CachingConnectionFactory connectionFactory =
new CachingConnectionFactory(&quot;localhost&quot;);
return connectionFactory;
}
@Bean
public AmqpAdmin amqpAdmin() {
return new RabbitAdmin(connectionFactory());
}
@Bean
public RabbitTemplate rabbitTemplate() {
RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory());
rabbitTemplate.setMessageConverter(jsonMessageConverter());
return rabbitTemplate;
}
@Bean
public Queue myQueue() {
return new Queue(QUEUE_NAME,false);
}
@Bean
MessageListenerContainer messageListenerContainer() {
SimpleMessageListenerContainer simpleMessageListenerContainer = new SimpleMessageListenerContainer();
simpleMessageListenerContainer.setConnectionFactory(connectionFactory());
simpleMessageListenerContainer.setQueues(myQueue());
simpleMessageListenerContainer.setMessageListener(new RabbitMQListner());
return simpleMessageListenerContainer;
}
@Bean
public MessageConverter jsonMessageConverter(){
return new Jackson2JsonMessageConverter();
}
}

And here is my service class from where I am trying to use the rabbit

import com.rabbitmq.client.*;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import java.io.IOException;
@Component
public class MessagingService{
public void send(String msg){
template.convertAndSend(QUEUE_NAME,msg);
}
@RabbitListener(queues = &quot;foo&quot;)
public void receiveMessage(String car)
{
System.out.println(&quot;Hello&quot;+car);
}
@Bean
private ApplicationContext context(){
return new AnnotationConfigApplicationContext(RabbitConfiguration.class);
}
@Bean
private AmqpTemplate amqpTemplate(){
return context().getBean(AmqpTemplate.class);
}
@Autowired
protected AmqpTemplate template;

The send method works just fine. But the @Listener annotated method does not work. It does not listen.
What I also tried, I created another config file and annotated with @EnableRabbit, but no benefits. Is there any secret sausage to make it working?

import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@EnableRabbit
@Configuration
public class AppConfig extends RabbitConfiguration {
@Bean
public SimpleRabbitListenerContainerFactory myRabbitListenerContainerFactory() {
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
factory.setConnectionFactory(connectionFactory());
factory.setMaxConcurrentConsumers(5);
factory.setMessageConverter(jsonMessageConverter());
return factory;
}
}

答案1

得分: 1

我希望您已经解决了这个问题,以下是我认为如果问题没有解决应该怎么做。
当您使用 @RabbitListener 时,您无需使用 SimpleRabbitListenerContainerFactory 或 SimpleMessageListenerContainer 类。在 SimpleMessageListenerContainer 配置类中,您映射了 RabbitMQListener 类来进行监听,但您尝试在 MessagingService 类中监听消息。在 SimpleRabbitListenerContainerFactory 中,您没有设置消息监听器本身。

英文:

i hope you already resolved this issue, here is what i think if its not resolved.
You don't need to use SimpleRabbitListenerContainerFactory or SimpleMessageListenerContainer class when you use @RabbitListener. in SimpleMessageListenerContainer config class you mapped RabbitMQListner class for listening but you are trying to listen msg in MessagingService class. in SimpleRabbitListenerContainerFactory you hvnt set the messagelistener itself.

答案2

得分: 0

可能代码正在创建自己的容器工厂,myRabbitListenerContainerFactory,但是默认情况下,@RabbitListener 监听 rabbitListenerContainerFactory

尝试使用 @RabbitListener(queues = &quot;foo&quot;, connectionFactory=&quot;myRabbitListenerContainerFactory&quot;)

参考:https://docs.spring.io/spring-amqp/api/org/springframework/amqp/rabbit/annotation/RabbitListener.html

英文:

It could be that the code is creating its own container factory, myRabbitListenerContainerFactory, but @RabbitListener by default listens to rabbitListenerContainerFactory.

Try with @RabbitListener(queues = &quot;foo&quot;, connectionFactory=&quot;myRabbitListenerContainerFactory&quot;)

Ref: https://docs.spring.io/spring-amqp/api/org/springframework/amqp/rabbit/annotation/RabbitListener.html

huangapple
  • 本文由 发表于 2020年5月29日 15:42:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/62081021.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定