[英]Spring 3 and Rabbit MQ integration (not Spring Boot)
I'm having difficulty getting a Spring 3 application to integrate with RabbitMQ, in order to receive messages from a queue (I do not need to send messages).我很难让 Spring 3 应用程序与 RabbitMQ 集成,以便从队列接收消息(我不需要发送消息)。
Part of the challenge is much of the documentation now relates to Spring Boot.部分挑战在于现在的大部分文档都与 Spring 引导有关。 The related Spring guide is helpful, but following the steps does not seem to work in my case.
相关的 Spring 指南很有帮助,但按照这些步骤在我的情况下似乎不起作用。 For instance, the guide includes the text:
例如,该指南包括以下文字:
The message listener container and receiver beans are all you need to listen for messages.
消息侦听器容器和接收器 bean 是您侦听消息所需的全部内容。
So I have setup the listener container and receiver beans with the following code.因此,我使用以下代码设置了侦听器容器和接收器 bean。
Setting up message handler设置消息处理程序
@Component
public class CustomMessageHandler {
public void handleMessage(String text) {
System.out.println("Received: " + text);
}
}
Setting up configuration设置配置
@Configuration
public class RabbitConfig {
@Bean
public RabbitTemplate rabbitTemplate(final ConnectionFactory connectionFactory){
final RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
rabbitTemplate.setRoutingKey("queue-name");
return rabbitTemplate;
}
@Bean
public ConnectionFactory connectionFactory() {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
connectionFactory.setHost("...host...");
connectionFactory.setPort(5671);
connectionFactory.setVirtualHost("...virtual host..");
connectionFactory.setUsername("...username...");
connectionFactory.setPassword("...password...");
return connectionFactory;
}
@Bean
public MessageListenerAdapter messageListenerAdapter(CustomMessageHandler messageHandler) {
return new MessageListenerAdapter(messageHandler, "handleMessage");
}
@Bean
public SimpleMessageListenerContainer listenerContainer(ConnectionFactory connectionFactory,
MessageListenerAdapter messageListenerAdapter) {
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
container.setQueueNames("queue-name");
container.setConnectionFactory(connectionFactory);
container.setMessageListener(messageListenerAdapter);
return container;
}
}
Unfortunately with this setup, the application will start up, but it never triggers the message handler.不幸的是,使用此设置,应用程序将启动,但它永远不会触发消息处理程序。 The queue it is trying to read from also has one message sitting in it, waiting to be consumed.
它尝试读取的队列中也有一条消息,等待被消费。
Any ideas on something that is missing, or appears misconfigured?关于丢失或配置错误的任何想法?
Thanks to some dependency management assistance from @GaryRussell, I was able to see that the version of spring-rabbit and spring-amqp were too recent.感谢@GaryRussell 提供的一些依赖管理帮助,我能够看到 spring-rabbit 和 spring-amqp 的版本太新了。 Using the older 1.3.9.RELEASE unfortunately proved to add additional challenges.
不幸的是,使用较旧的 1.3.9.RELEASE 会增加额外的挑战。
Some other assistance came in the form of using an actual RabbitMQ Java client.其他一些帮助以使用实际 RabbitMQ Java 客户端的形式出现。 This option was much simpler to implement, and avoided the dependency problems.
这个选项实现起来要简单得多,并且避免了依赖问题。 Ultimately I needed to include the following dependency:
最终我需要包含以下依赖项:
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>5.7.3</version>
</dependency>
And then I simply followed their documentation on creating a connection , and consuming messages .然后我只是按照他们关于创建连接和使用消息的文档进行操作。
Voila, it works!瞧,它有效!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.