通过 Spring 应用程序事件的生产者消费者

发布于 2024-10-04 22:33:05 字数 858 浏览 4 评论 0原文

我正在尝试通过使用 spring 的应用程序事件和 threadpoolexecutors 来实现参与者模型模式(与生产者消费者有些混合) 我的主要目标是解耦每一层。 我的架构如下: 我部署了一场战争,通过休息 API 接收业务交易请求,在任何给定时刻都可能有 X 数量的交易处于活动状态 其中X是可配置的数字,实际执行必须是异步的,并且每个事务必须位于不同的线程中。 请求本身以 FIFO 方式处理,但它有一些复杂性,因为某些请求必须等待其他请求完成才能处理,但这并不意味着其他请求无法处理,例如:不处理如果在帐户 2 之前请求存款到帐户 2,则从帐户 2 提款,因此如果我收到以下请求:

deposit(2) 提款(2) 存款(3)

,其中数字是帐号,我想按以下顺序处理它们:

存款(2) 存款(3) 撤回(2)

我构建的架构是这样的: 我有一个rest api,它获取命中并将它们写入数据库(必须在数据库中具有状态的分布式系统)并发布 应用程序上下文中的客户端请求事件 我有一个单例bean,负责发布生产者事件并监视他发送了多少事件(即:他负责限制并发进程的数量并实现上述逻辑) 我还有一些其他侦听器,每个侦听器都针对一个操作(提款存款等),侦听后者发布的事件并发布完成事件。

每件事都很好,一切都完成了,所有的流程都很棒,但我有 中间层有问题,负责确定是否有空闲插槽 我不想有同步方法,也不想围绕atomiclong或类似的东西做一些技巧,我宁愿使用一些阻塞队列来发布事件,但我找不到一个好的方法来确定事件何时发生完成后我就可以把新的放回去了。 最大的问题是,为了请求新的工作,我必须去数据库,这是一项繁重的任务,因为这个系统应该在重负载下工作。 我想以某种方式利用阻塞队列和线程池,以便在一个插槽空闲时从带有线程的大小有界队列中获取,

处理此问题的好方法是什么? 提前致谢

I'm trying to implement the actor model pattern (Somewhat mashed toghter with producer consumer) by using spring's application events and threadpoolexecutors
my main objective is to decouple each of the layers .
my architecture is as follows :
i have a war deployed that trough a rest API receives requests for business transactions , at any given moment there can be X number of transactions alive
where X is configureable number , the actual execution must be asynchronous and each transaction must be in a different thread .
the requests themselves are treated in a FIFO manner but there is some complexity to it as some requests must wait for other to complete before they can be processed but that does not mean other requests can't be processed , e.g. : don't process a withdrawal from account 2 if a deposit to account 2 was requested before it , so if i get hits for :

deposit(2)
withdrawal(2)
deposit(3)

where the numbers are account numbers i want to process them in this order:

deposit(2)
deposit(3)
withdrawal(2)

i've built the architecture is this way :
i have a rest api that gets the hits and writes them to the DB (distributed system that has to have the state in DB)and publishes
a clientrequestevent in the application context
i have singleton bean that is in charge of publishing the producer events and monitoring how many events he has sent (i.e. : he is in charge of limiting the number of concurrent processes and implementing the above logic)
and i have a few other listeners each for an action (withdrawal deposit etc..) that listen to the events published by the latter and publish a done event.

every thing works great everything is done is different threads and all flows great but i have
a problem with the middle layer the one in charge of determing whether or not there is a free slot
i don't want to have a synchronous method nor do i want to do some tricks around an atomiclong or something like that i would rather use some blockingqueue to publish the events but i can't find a nice way to determine when an event is done so i can put a new one back in .
the biggest problem is that for requesting a new work i have to go the DB which is a heavy task , as this system should work under heavy load .
i would like to somehow utilize a blockingqueue and a threadpool so that will take from a size bounded queue with threads the minute one slot is free

what would be a good way to handle this ?
thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文