我开发了简单的 Grails REST Web 服务,它使用 Grails RabbitMQ 插件并将消息发送到队列。
Grails - Config.groovy
queues = {
exchange name: 'my.topic', type: topic, durable: false, {
foo durable: true, binding: 'test.#'
}
}
在我的 Java 桌面应用程序上,我尝试使用 Spring AMQP 监听该消息
Main.java
public static void main(String[] args) {
CachingConnectionFactory cf = new CachingConnectionFactory("localhost");
cf.setUsername("guest");
cf.setPassword("guest");
RabbitAdmin admin = new RabbitAdmin(cf);
SimpleMessageListenerContainer container =
new SimpleMessageListenerContainer(cf);
Object listener = new Object() {
public void handleMessage(String foo) {
System.out.println(foo);
}
};
MessageListenerAdapter adapter = new MessageListenerAdapter(listener);
container.setMessageListener(adapter);
container.setQueueNames("foo");
container.start();
}
当我运行多个客户端(比如两个)时,有时一个会收到“消息”,有时另一个会收到“消息”。 两个客户端如何在每次“发送”时接收“消息”(我通过重新加载页面从索引 Grails Controller 发送消息)。
执行此操作的首选方法是什么?我是新手:)
最佳答案
每个消费者都需要将自己的队列绑定(bind)到交换器。
同一队列上的多个消费者竞争消息。
关于java - Grails、Grails RabbitMQ 插件、Java 、Spring AMQP - 多个客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26428498/