简体   繁体   English

Spring RabbitMq-队列创建的事件监听器

[英]Spring RabbitMq - Queue created event listener

I would like to know if there are any events published when the queue is actually physically created in Rabbitmq via Spring Api . 我想知道当通过Spring ApiRabbitmq实际创建queue是否发布了任何事件。

The reason I am asking is somehow we have race condition. 我问的原因是某种程度上我们有比赛条件。 The subscription for queue is somehow usually taking longer and while the subscription is happening already a message is sent to this queue from BE and as a result queue is not present in Rabbitmq and the message is lost and never reaches FE. queue的订阅通常以某种方式花费更长的时间,并且在订阅发生时,已经有消息从BE发送到此队列,结果Rabbitmq不存在队列,消息丢失并且永远不会到达FE。

Sorry I cannot provide any code as such, because it wont make much sense by putting a small piece of code. 抱歉,我无法提供任何此类代码,因为通过放置一小段代码就没有多大意义。

But I am pretty sure, problem is because of kind of race condition, message to the queue is being sent even before its created. 但是我很确定,问题是由于某种竞争状况造成的,甚至在队列创建之前就已经向队列发送了消息。 So if there is some event listener I could listen after the queue is created, I could move my logic to this method. 因此,如果在创建队列后可以监听某些event listener ,则可以将逻辑移至该方法。

Here is some piece of code, 这是一些代码,

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.

 }

There is no event published but you can add a second ConnectionListener to the CachingConnectionFactory . 没有发布事件,但是您可以向CachingConnectionFactory添加第二个ConnectionListener If you make sure it is added after the RabbitAdmin you can be sure that the onCreate() method will be called after the admin has declared all the exchanges, queues, bindings. 如果确保在RabbitAdmin之后添加了RabbitAdmin ,则可以确保在管理员声明所有交换,队列和绑定之后调用onCreate()方法。

See AbstractConnectionFactory.addConnectionListener . 请参见AbstractConnectionFactory.addConnectionListener

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM