java中的轻量级发布/订阅框架
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3032827/
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
lightweight publish/subscribe framework in java
提问by mdma
Is there a good lightweight framework for java that provides the publish/subscribe pattern?
是否有一个很好的轻量级 Java 框架提供发布/订阅模式?
Some ideal features
一些理想的功能
- Support for generics
- Registration of multiple subscribers to a publisher
- API primarily interfaces and some useful implementations
- purely in-memory, persistence and transaction guarantees not required.
- 支持泛型
- 将多个订阅者注册到发布者
- API 主要接口和一些有用的实现
- 完全在内存中,不需要持久性和事务保证。
I know about JMS but that is overkill for my need. The publish/subscribed data are the result of scans of a file system, with scan results being fed to another component for processing, which are then processed before being fed to another and so on.
我知道 JMS 但这对我的需要来说太过分了。发布/订阅数据是文件系统扫描的结果,扫描结果被馈送到另一个组件进行处理,然后在馈送到另一个组件之前进行处理,依此类推。
EDIT: All within the same process. PropertyChangeListener from beans doesn't quite cut it, since it's reporting changes on properties, rather than publishing specific items. I could shoehorn ProprtyChangeListener to work by having a "last published object" property, and so published objects. PropertyChangeListeners don't support generics, and are entrenched in property change semantics, rather than pure publish/subscribe. The java.util Observer/Observable pattern would be good, but Oberver is a concrete class.
编辑:都在同一个过程中。来自 beans 的 PropertyChangeListener 并没有完全削减它,因为它报告属性的更改,而不是发布特定项目。我可以通过具有“上次发布的对象”属性以及发布的对象来硬塞 ProprtyChangeListener 来工作。PropertyChangeListeners 不支持泛型,并且在属性更改语义中根深蒂固,而不是纯粹的发布/订阅。java.util Observer/Observable 模式会很好,但 Oberver 是一个具体的类。
采纳答案by Timo Westk?mper
JMS is as light or heavy as you configure it. We use for example HornetQin one project with an in memory queue. It is easy to setup, doesn't need any JNDI based configuration and is really easy to use.
JMS 的轻重取决于您的配置。例如,我们在一个具有内存队列的项目中使用HornetQ。它易于设置,不需要任何基于 JNDI 的配置并且非常易于使用。
I believe that JMS as an API for Message Pub/Sub is as easy as it gets. (And not easier ;)
我相信 JMS 作为 Message Pub/Sub 的 API 非常简单。(而且并不容易;)
回答by djna
If you are crossing process boundaries then some degree of "weight" is going to be incurred. Why do you say that JMS is heavyweight? The API is quite simple? There are supposedly light-weight implementations, for example link textheavier costs such a persistence and transactionality are optional.
如果您正在跨越流程边界,那么将会产生一定程度的“权重”。为什么说JMS是重量级的?API很简单?据说有轻量级的实现,例如链接文本更重的成本,如持久性和事务性是可选的。
What do you need that is lighter than this?
你需要什么比这更轻?
回答by skaffman
Since you're using Spring, I don't know if you're aware that Spring has its own lightweight event framework. It's used primarily within the framework itself, but it's perfectly usable by application code.
既然你用的是Spring,不知道你知不知道Spring有自己的轻量级事件框架。它主要在框架本身内使用,但它完全可以由应用程序代码使用。
By default, it's synchronous pub/sub, but you can make it asynchronous using an ApplicationEventMulticaster
.
默认情况下,它是同步发布/订阅,但您可以使用ApplicationEventMulticaster
.
回答by Cagatay
Bob Lee has a QueueFile
class at http://snipt.org/GWm/that you might find interesting. It's a simple persistent queue and could be used by multiple consumers. It sounds like you don't need persistence, but since the whole thing is so lightweight it could still be useful.
Bob LeeQueueFile
在http://snipt.org/GWm/ 开设了一门您可能会感兴趣的课程。这是一个简单的持久队列,可以被多个消费者使用。听起来您不需要持久性,但是由于整个事情非常轻巧,因此它仍然很有用。
回答by Andrejs
It seems this fits the requirements:
看来这符合要求:
EventBusfrom Google Guava Library - "Publish-subscribe-style communication between components without requiring the components to explicitly register with one another". It can also be an AsyncEventBusthat will dispatch events on another thread.
来自 Google Guava Library 的EventBus- “组件之间的发布订阅式通信,无需组件显式地相互注册”。它也可以是将在另一个线程上调度事件的AsyncEventBus。
Some extra options to consider:
需要考虑的一些额外选项:
If it's in same process it's possible the Observerpattern can be used. Subscribers can add listeners and receive event notifications. Observableis already part of the Java API.
FFMQis a full Java, light-weight, Fast JMS 1.1 Queue implementation.
如果它在同一个进程中,则可以使用观察者模式。订阅者可以添加侦听器并接收事件通知。Observable已经是 Java API 的一部分。
FFMQ是一个完整的 Java轻量级快速 JMS 1.1 队列实现。
回答by unludo
I think Camelis also a good candidate. Especially with the publish-subscribe pattern
Camel can be embedded and is lightweight. It proposes Enterprise Integration Patterns - many useful tools for integration, inside an application or even with other actors (hence 'Integration').
Camel 可以嵌入并且重量轻。它提出了企业集成模式 - 许多有用的集成工具,用于在应用程序内部甚至与其他参与者(因此称为“集成”)。
It compares to Spring Integrationbut more complete IMO.
它与Spring Integration相比,但更完整的 IMO。