FinnTheHuman FinnTheHuman - 3 months ago 16
Java Question

How to create some sort of event framework in java?

I don't have a GUI (my classes are part of a Minecraft Mod). I wanted to be able to mimic C# event framework: A class declares events and lets others subscribe to them.

My first approach was to create a class called

EventArgs
and then do something like this:

public class EventArgs
{
public boolean handled;
}

@FunctionalInterface
public interface IEventHandler<TEvtArgs extends EventArgs>
{
public void handle(Object source, TEvtArgs args);
}

public class Event<TEvtArgs extends EventArgs>
{
private final Object owner;
private final LinkedList<IEventHandler<TEvtArgs>> handlers = new LinkedList<>();

public Event(Object owner)
{
this.owner = owner;
}

public void subscribe(IEventHandler<TEvtArgs> handler)
{
handlers.add(handler);
}

public void unsubscribe(IEventHandler<TEvtArgs> handler)
{
while(handlers.remove(handler));
}

public void raise(TEvtArgs args)
{
for(IEventHandler<TEvtArgs> handler : handlers)
{
handler.handle(owner, args);
if(args.handled)
break;
}
}
}


Then a class would do something like this:

public class PropertyChangedEvtArgs extends EventArgs
{
public final Object oldValue;
public final Object newValue;

public PropertyChangedEvtArgs(final Object oldValue, final Object newValue)
{
this.oldValue = oldValue;
this.newValue = newValue;
}
}

public class SomeEventPublisher
{
private int property = 0;
private final Random rnd = new Random();
public final Event<PropertyChangedEvtArgs> PropertyChanged = new Event<>(this);

public void raiseEventOrNot(int value)
{
if(rnd.nextBoolean())//just to represent the fact that the event is not always raised
{
int old = property;
property = value;
PropertyChanged.raise(new PropertyChangedEvtArgs("old(" + old + ")", "new(" + value + ")"));
}
}
}

public class SomeSubscriber
{
private final SomeEventPublisher eventPublisher = new SomeEventPublisher();

public SomeSubscriber()
{
eventPublisher.PropertyChanged.subscribe(this::handlePropertyAChanges);
}

private void handlePropertyAChanges(Object source, PropertyChangedEvtArgs args)
{
System.out.println("old:" + args.oldValue);
System.out.println("new:" + args.newValue + "\n");
}

public void someMethod(int i)
{
eventPublisher.raiseEventOrNot(i);
}
}

public class Main
{
private static final SomeSubscriber subscriber = new SomeSubscriber();

public static void main(String[] args)
{
for(int i = 0; i < 10; ++i)
{
subscriber.someMethod(i);
}
}
}


The biggest problem with this naïve approach is that it breaks proper encapsullation by exposing
raise
as public. I can't see a way around it, and maybe my whole pattern is wrong. I would like some ideas.

There's also a related problem: I would like the events to be raised immediately after the method raising them returns. Is there a way to synchronize this using threads or some other construct? The caller code, of course, can't be involved in the task of synchronization. It has to be completely transparent to it.

Answer

The best thing to do here is to avoid implementing your own event framework in the first place, and instead rely on some existing service. Out of the box Java provides EventListener, and at a minimum you can follow the patterns documented there. Even for non-GUI applications most of this advice applies.

Going beyond the JDK Guava provides several possible options, depending on your exact use case.

The most likely candidate is EventBus, which:

allows publish-subscribe-style communication between components without requiring the components to explicitly register with one another (and thus be aware of each other).

Or ListenableFuture (and ListeningExecutorService) which:

allows you to register callbacks to be executed once [a task submitted to an Executor] is complete, or if the computation is already complete, immediately. This simple addition makes it possible to efficiently support many operations that the basic Future interface cannot support.

Or the Service API which:

represents an object with an operational state, with methods to start and stop. For example, webservers, RPC servers, and timers can implement the Service interface. Managing the state of services like these, which require proper startup and shutdown management, can be nontrivial, especially if multiple threads or scheduling is involved.

This API similarly lets you register listeners to respond to state changes in your services.

Even if none of these options directly work for your use case, take a look at Guava's source code for examples of event-driven behavior and listeners you can try to emulate.