英文:
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
<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>
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="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();
}
}
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 = "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;
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 = "foo", connectionFactory="myRabbitListenerContainerFactory")
参考: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 = "foo", connectionFactory="myRabbitListenerContainerFactory")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论