Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java equivalent of Cocoa NSNotification?

I am writing a Java application using SWT widgets. I would like to update the state of certain widgets upon a certain event happening (for example, updating the data model's state).

Is there something in Java similar to Cocoa's NSNotificationCenter, where I can register an object to listen for notification events and respond to them, as well as have other objects "fire off" a notification?

like image 653
Alex Reynolds Avatar asked Jan 28 '09 19:01

Alex Reynolds


3 Answers

What sort of notifications are you looking for? If all you want is for one object to be able to tell anybody else "hey, I've changed, update accordingly", the easiest way is to use the existing Observer interface and Observable class. Or write your own with an interface that defines what you want to get called on the listeners from the one that's changed.

like image 151
Paul Tomblin Avatar answered Nov 15 '22 15:11

Paul Tomblin


There's no pre-existing per-process service that dispatches events in java that's equivalent to the default NSNotificationCenter. In java, the type of the event is specified by the event object being a particular type (which also means that the notification method depends on that type) rather than using a string. Prior to generics, writing a general event dispatcher and receiver that is also typesafe isn't really possible (witness the proliferation of *Event classes and *EventListener interfaces in the AWT and Spring libraries).

There are some facilities for event dispatch. As Paul mentioned, there's java.util.Observable, which as you point out, requires subclassing. There's also java.beans.PropertyChangeSupport, which could be useful depending on your situation.

You could also write one yourself. The source for PropertyChangeSupport is likely available in the openjdk, and you could look at the abandoned Apache Commons Event project. Depending on your needs, you may have to worry about stuff like threading, seralization, memory leaks (ensuring deregistration or using weak references), and concurrent modification (iterate over a copy of your list of listeners, as a listener may decide to unregister itself in response to a change).

Now that generics exist in Java, a generic event dispatch library would be possible; however, I haven't come across any. Anyone?

like image 38
Warren Blanchet Avatar answered Nov 15 '22 17:11

Warren Blanchet


There's actually a facility built in to Java that does exactly what you want, but it's not something you may have considered, and, to be honest, it is likely a bit heavyweight for what you want.

That said, however, it does exist.

It's JMX.

You create MBeans, and then others can register for events from those MBeans. The MBean can then send of a Notification.

I personally wouldn't consider using it for this case (I'd just pound out my own), but the facility is there and it well defined and documented.

like image 23
Will Hartung Avatar answered Nov 15 '22 17:11

Will Hartung