tags:

views:

607

answers:

9
+3  Q: 

Events in Java?

I'm a bit confused from what I've heard Java doesn't do events.

But I know that it does GUI events.

Am I missing something? Does java have an event handling mechanism?

I'm aware that I can implement a publisher subscriber pattern, but I'm lookign for native support within Java.

I seem to remember something about Java Adding events in either Java 5 or 6 but I can't remember where I heard this and I may be making it up.

Basically I'm wrapping a device in a java class the device throws events, and I'm looking for the most logical way of exposing this. I come mostly from a .Net Background and I'm looking for something like the events in .Net (C#)

Any help would be appreciated.

+10  A: 

As you already stated, you can do the exact same thing with the publisher-subscriber/Observer pattern. It just requires a bit more legwork.

And no, Java does not have native support for events, like C# does with delegates.

Simon Jensen
A: 

You need threads. The GUI in java has events, because there is a dedicated thread for UI events to dispatch. There is also something calle "Observer" and "Observable" (this is the taste neutral version of the event listeners from the GUI stuff) but is just the design pattern...

Harald Schilly
+1  A: 

From what I remember of Java (haven't worked with it in > 6 months), I don't think there are events ala .NET. Your best bet will probably be to make use of the publisher/subscriber pattern. It's pretty easy to implement and pretty reliable.

Matthew Brubaker
+1  A: 

Swing or AWT, which are the Java UI toolkits, both handle events - check out the official Swing event tutorial for examples. You will need to have written your UI in Swing of course to be able to register listeners for these events.

Andrzej Doyle
I'm not working with a UI unfortunately.
Omar Kooheji
Does this mean that for every event in a particular swing object their exists an observable pattern? That seems like a lot of redundant code. The would mean a subscriber/publisher for each event. Is it not possible just to have a generic observable class and use composition to implement the obervable pattern.
Shiftbit
A: 

I'm not knowledgeable about C#, but Java Beans that are not UI components can certainly notify Listeners about state changes using Events:

http://java.sun.com/developer/onlineTraining/Beans/beans02/

This has been part of the Java Beans spec since it first came out. Note the date on the tutorial: it's vintage 2000.

The source and listener have to be running in the same JVM. You'd have to use a proxy to communicate with something running in another JVM, but it could be done.

I Googled for the C# events tutorial and found this. Java's registering of listeners and firing of property changed events was reminiscent of what I saw when I skimmed the C# tutorial. Did I miss something important? (I'll admit that I didn't read deeply - no time right now.)

duffymo
A: 

There is no native support in Java - you will have to implement the Observer Pattern (aka Subcribe/Publish). Its pretty straight forward - not more than a a dozen lines of code.

tellme
+3  A: 

The Subscribe/Publish mechanism proposed by others here will let you implement synchronous events. For asynchronous event loops (fire and forget) You may want to look at "actors". An Actor<A> consists of a handler for messages (events) of type A, as well as a threading strategy for executing the handler. This lets you do concurrent, asynchronous event handling like the following:

public class EventExample {
  public static void main(String[] args) {
    while ((char c = System.in.read()) >= 0) {
      eventHandler.act(c);
    }
  }

  public static Actor<Character> eventHandler =
    Actor.actor(Strategy.simpleThreadStrategy(), new Effect<Character>() {
      public void e(Character c) {
        // Do something with c here.
      }
    });
}

Get the actors library here. See the Javadoc here.

Apocalisp
A: 

You might also want to take a look at the open source EventBus library, which, contrarily to the standard publisher/subscriber pattern uses a mediator class (the event bus) in order to reduce the tight coupling required in publisher/subscriber pattern (where subscribers must know about all publishers to register to them, which is not always possible and is often cumbersome).

I have used it in GUI applications but it can deal with any kinds of event.

jfpoilpret