java 何时使用事件总线?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14742501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When to use an Event Bus?
提问by IAmYourFaja
I'm designing the backend for a new Java web app and am trying to decide whether or not to use an Event Bus; specifically the Guava EventBus
.
我正在为一个新的 Java Web 应用程序设计后端,并试图决定是否使用事件总线;特别是番石榴EventBus
。
Most server-side requests will be synchronous: that is, the user is requesting data and needs a response within a few seconds. However, there are also quite a few number of requests that can be asynchronous and that are "fire and forget" on the client-side. So long as they eventually are processed, the client could care less if it takes 2 secondds to process or 2 hours.
大多数服务器端请求将是同步的:也就是说,用户正在请求数据并且需要在几秒钟内得到响应。然而,也有相当多的请求可以是异步的,并且在客户端“即发即忘”。只要它们最终得到处理,如果处理需要 2 秒或 2 小时,客户就不会那么在意。
For these asynchronous requests, I plan to have the servlet listening at the mapped URL publish the requests to a queue. A consumer will then dequeue each request and route it on to the appropriate handler. This is where the EventBus
may or may not come into play. The business logic for routing the request to the correct handler is pretty complex. Ordinarily a Camel route would be the perfect solution. In my use case, I'm wondering if I can just wire up a bunch of "processors" (event handlers) to the same event bus, and then have each one firing and receiving events to and from each other until an end result is produced.
对于这些异步请求,我计划让侦听映射 URL 的 servlet 将请求发布到队列中。然后,消费者将每个请求出列并将其路由到适当的处理程序。这就是EventBus
可能会或可能不会发挥作用的地方。将请求路由到正确处理程序的业务逻辑非常复杂。通常,骆驼路线将是完美的解决方案。在我的用例中,我想知道是否可以将一堆“处理器”(事件处理程序)连接到同一个事件总线,然后让每个处理器相互触发和接收事件,直到最终结果是产生。
I would like to say that I already explored using Apache Camel, and that I do believe that Camel's the right tool for the job here. Sadly, for reasons outside the scope of this question, I'm not going to use it. So I started conjuring up Camel-like solutions, which is how I arrived at Guava's EventBus
. But it just might not be a suitable replacement.
我想说的是,我已经探索过使用 Apache Camel,而且我确实相信 Camel 是这里工作的正确工具。可悲的是,由于这个问题范围之外的原因,我不会使用它。所以我开始想出类似 Camel 的解决方案,这就是我如何到达 Guava 的EventBus
. 但它可能不是一个合适的替代品。
I guess I'm looking for the problem classification that the Event Bus
pattern solves, and then I need to determine if that matches my use case here.
我想我正在寻找Event Bus
模式解决的问题分类,然后我需要确定这是否与我的用例匹配。
回答by ColinD
Here is the problem statement that EventBus
solves in a nutshell:
以下是EventBus
简而言之解决的问题陈述:
"I want an easy, centralized way to notify code that's interested in specific types of events when those events occur without any direct coupling between the code that publishes an event and the code that receives it."
“我想要一种简单、集中的方式来通知对特定类型事件感兴趣的代码,当这些事件发生时,发布事件的代码和接收事件的代码之间没有任何直接耦合。”
When you say "the business logic for routing the request to the correct handler is pretty complex", I suspectthat EventBus
may not be what you're looking for, as it routes based on Java object class only (though you can do some fancy stuff with implementing interfaces on your event classes and having subscribers subscribe to specific interfaces).
当您说“将请求路由到正确处理程序的业务逻辑非常复杂”时,我怀疑这EventBus
可能不是您想要的,因为它仅基于 Java 对象类进行路由(尽管您可以做一些奇特的事情在您的事件类上实现接口并让订阅者订阅特定接口)。