Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lightweight publish/subscribe framework in java [closed]

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.

like image 720
mdma Avatar asked Jun 13 '10 15:06

mdma


People also ask

How do you implement publish subscribe in Java?

Very simple, just put the OnMessage annotation on the method. Then you need to create a class that represent the message you send. Initialize the subscriber class so you can have a subscriber object. Subscriber subscriber = new Subscriber(1);

What is publisher and subscriber in Java?

In the publish/subscribe domain, message producers are called publishers and message consumers are called subscribers. They exchange messages by means of a destination called a topic: publishers produce messages to a topic; subscribers subscribe to a topic and consume messages from a topic.

What is Pubsub in Java?

In a publish/subscribe (pub/sub) product or application, clients address messages to a topic, which functions somewhat like a bulletin board. Publishers and subscribers are generally anonymous and can dynamically publish or subscribe to the content hierarchy.

Which protocol uses publish subscribe model?

Many standardized messaging protocols that implement a Publish/Subscribe pattern exist. In the area of application level protocols the most interesting ones are: AMQP, Advanced Message Queueing Protocol. MQTT, MQ Telemetry Transport.


2 Answers

It seems this fits the requirements:

EventBus from Google Guava Library - "Publish-subscribe-style communication between components without requiring the components to explicitly register with one another". It can also be an AsyncEventBus that will dispatch events on another thread.

Some extra options to consider:

  1. If it's in same process it's possible the Observer pattern can be used. Subscribers can add listeners and receive event notifications. Observable is already part of the Java API.

  2. FFMQ is a full Java, light-weight, Fast JMS 1.1 Queue implementation.

like image 102
Andrejs Avatar answered Sep 21 '22 04:09

Andrejs


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 ;)

like image 35
Timo Westkämper Avatar answered Sep 19 '22 04:09

Timo Westkämper