views:

118

answers:

3

Is there a good lightweight framework for java that provides the publish/subscribe pattern?

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.

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.

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.

A: 

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 text heavier costs such a persistence and transactionality are optional.

What do you need that is lighter than this?

djna
A: 

JMS is as light or heavy as you configure it. We use for example HornetQ in 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.

I believe that JMS as an API for Message Pub/Sub is as easy as it gets. (And not easier ;)

Timo Westkämper
Actually... for the publish/subscribe aka observer pattern the simplest and most basic thing there is... is the javax.beans package. ;) (PropertyChangeListener, PropertyChangeEvent, PropertyChangeSupport ...)
@user268396, fair enough. For simple POJO state change pub/sub JavaBeans property listening is even simpler.
Timo Westkämper
@user268396 - I'm afraid PropertyChangeListener won't fit here. See my edit for reasons.
mdma
@Timo - thanks for this suggestion. Perhaps was too hasty in dismissing JMS?
mdma
@mdma, maybe, I thought too before that JMS is all heavyweight J2EE, but it is actually quite lightweight if you pick the right provider. And if interface/provider separation is important for you, then JMS is probably the right approach.
Timo Westkämper
A: 

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.

By default, it's synchronous pub/sub, but you can make it asynchronous using an ApplicationEventMulticaster.

skaffman