简体   繁体   English

Java Spring RabbitMQ使队列等待另一个

[英]java spring rabbitmq make a queue wait for another

I have a java spring app and rabbitmq server. 我有一个Java Spring应用程序和Rabbitmq服务器。 I have two queues. 我有两个队列。 The first one check that A is in DB. 第一个检查A是否在数据库中。 The second saves A to db. 第二个将A保存到db。 I mean listeners. 我的意思是听众。 But, if I send many requests with the same A, the first queue listener is trying to check A in DB, even before the second listener saves it to the DB. 但是,如果我用相同的A发送许多请求,则第一个队列侦听器将尝试在数据库中检查A,甚至在第二个侦听器将其保存到DB之前。 How to sync two queues? 如何同步两个队列? So the first listener should know that the second has already saved this A to DB and then the first should check it in DB. 因此,第一个侦听器应该知道第二个侦听器已经将该A保存到DB,然后第一个侦听器应该在DB中进行检查。 So, if I send 1000 requests of A, the first listener checks A, there is no A, then sends to the second listener, it saves A, but on the second iteration the first checks A again, but the second listener may not complete it's job. 因此,如果我发送1000个A的请求,则第一个侦听器将检查A,没有A,然后将其发送给第二个侦听器,它将保存A,但是在第二次迭代中,第一个侦听器将再次检查A,但是第二个侦听器可能无法完成这是工作。 And the first should wait then. 第一个应该等待。 How to solve it? 怎么解决呢?

use threads, maybe with locks. 使用线程,也许带锁。
First thread deques element from Queue A, and passes it to other thread, which locks your DB, checks if A is in DB, if not - writes it to DB, unlocks DB, and accepts next passed element. 第一个线程将队列A中的元素移出队列,然后将其传递给另一个线程,该线程将锁定您的DB,检查A是否在DB中,如果不是-将其写入DB,解锁DB,并接受下一个传递的元素。 Same with Queue B. 与队列B相同。
Just lock the DB before checking and writing to it, and unlock after writing to it. 只需在检查和写入数据库之前将其锁定,然后在写入数据库之后将其解锁。

我没有找到任何解决方案,但是能够通过使用具有不同传播级别的java sql事务来做到这一点。

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

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