【问题标题】:Spring RabbitMq - Queue created event listenerSpring RabbitMq - 队列创建的事件监听器
【发布时间】:2017-07-30 06:42:33
【问题描述】:

我想知道当queue实际上是通过Spring ApiRabbitmq中物理创建时是否发布了任何事件。

我问的原因是我们有竞争条件。 queue 的订阅通常需要更长的时间,并且在订阅发生时,已经从 BE 向此队列发送了一条消息,因此Rabbitmq 中不存在队列,并且消息丢失并且永远不会到达 FE。

对不起,我不能提供任何这样的代码,因为放一小段代码没有多大意义。

但我很确定,问题是由于某种竞争条件,发送到队列的消息甚至在创建之前就已发送。所以如果有一些event listener我可以在队列创建后监听,我可以将我的逻辑转移到这个方法。

这是一段代码,

stomp-client.js

 stompClient.subscribe(destination, function(msg) {});

WebsocketConnectionListener.java

@EventListener
public void handleWebSocketSessionSubscribeEvent(final SessionSubscribeEvent event) {

 // here I think this event is fired before the queue is actual created
 // this event is fired when u send subscription from stomp-client.js

 ...
 ...
 ...

 // trying to send message to this subscribed queue

 simpMessagingTemplate.convertAndSend(TOPIC_PREFIX + destination, data, headers);
 // now this message is lost as sometimes the queue creation takes longer.
 // unfortunately I want to move the convert and send method, when I could listen to queue created event.

 }

【问题讨论】:

    标签: java spring events listener spring-rabbit


    【解决方案1】:

    没有发布事件,但您可以将第二个 ConnectionListener 添加到 CachingConnectionFactory。如果您确保在 RabbitAdmin 之后添加它,则可以确保在管理员声明所有交换、队列、绑定之后调用 onCreate() 方法。

    AbstractConnectionFactory.addConnectionListener

    【讨论】:

    • 我明白你的意思,但并不是我所有的队列都是在服务器启动时创建的。大多数队列是通过从前端发送订阅动态创建的。根据我的理解,这个新创建的侦听器不会收到您稍后创建/订阅的每个队列的通知,或者我错了。
    • 不清楚你的意思——如果你自己动态创建队列,那么你就会知道它们是被创建的——这些操作是阻塞的——不是异步的——使用RabbitAdmin.declareQueue(queue)
    猜你喜欢
    • 1970-01-01
    • 2018-10-27
    • 2016-03-12
    • 2021-10-15
    • 1970-01-01
    • 2021-06-07
    • 1970-01-01
    • 2020-11-07
    • 2015-02-22
    相关资源
    最近更新 更多