Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / HTML

Linkset Gets An Event Bus Functionality

0.00/5 (No votes)
13 Oct 2010LGPL32 min read 17.2K   107  
LinkSet is a tiny library created in order to relieve programmers from declaring listener interfaces.

LinkSet as described in the previous article is a tiny library created in order to relieve programmers from declaring listener interfaces. It utilizes Java 5 features and was designed to be a drop in replacement for a conventional “listener interface + anonymous class” solution. Now it has an event bus functionality.

Introduction

When I created LinkSet (github.com/lbownik/linkset) I thought about listeners, but after some time I realized that the same mechanism can be used to create simple to use event buses.

Working with Event Buses

If you want to find out how to use a basic Linkset functionality, please read the previous article.

An Event

In order to use an event bus, you need to declare an event type. An event can be of any type. It doesn't need to implement any interface or subclass any class. It can be any Java object, even as simple as this...

Java
package package com.mycompany.project1;

public class Event {

}

Simple Event Bus

The code below shows how to implement a class that can emit events.

Java
package com.mycompany.project1;

import org.linkset.DefaultListenerManager;
import org.linkset.ListenerManager;
import org.linkset.MethodPointer;

public class EventSource {

 	public static EventBus eventBus = new SimpleEventBus();   
	
	public void fireEvents() {
		
		eventBus.fire(new Event());
	}
}

The class declares a static event bus object and then fires an event. It doesn't need any runtime or compilation support to work.

Event Handler

The following code shows how to implement a class that provides methods that listen to events.

Java
package com.mycompany.project1;

import org.linkset.HandlerMethod;
import org.linkset.MethodPointer;

public class EventHandler {

    public EventHandler() {

        EventSource.eventBus.add(Event.class, this, "eventHandler");
    }

    @HandlerMethod(id = "eventHandler")
    private void eventHandler(final Event event) {

        System.out.println("Event handled.");
    }
}

The class declares a method that handles an event and registers itself as a handler of a particular type of events with a single line...

Java
EventSource.eventBus.add(Event.class, this, "eventHandler");

Hierarchy Aware Event Bus

LinkSet makes it possible to utilize an event type hierarchy information. If you want, for example, to monitor some set of event types, you can register an event handler that handles all events that are subtypes of a particular type.

The code below introduces a sub-event...

Java
package package com.mycompany.project1;

public class SubEvent extends Event {

}

... an event source declared an event hierarchy aware event bus ...

Java
package com.mycompany.project1;

import org.linkset.DefaultListenerManager;
import org.linkset.ListenerManager;
import org.linkset.MethodPointer;

public class EventSource {

 	public static EventBus eventBus = new HierarchyAwareEventBus();   
	
	public void fireEvents() {
		
		eventBus.fire(new SubEvent());
	}
}

... and an event target declares separate handlers for sub- and supertype of events.

Java
package com.mycompany.project1;

import org.linkset.HandlerMethod;
import org.linkset.MethodPointer;

public class EventHandler {

    public EventHandler() {

        EventSource.eventBus.add(Event.class, this, "monitor");
        EventSource.eventBus.add(Event.class, this, "subEventHandler");
    }

    @HandlerMethod(id = "subEventHandler")
    private void eventHandler(final SubEvent event) {

        System.out.println("SubEvent handled.");
    }
	
    @HandlerMethod(id = "monitor")
    private void eventMonitor(final Event event) {

        System.out.println("Event monitored");
    }
}

Every time a SubEvent is fired, both handlers will be invoked.

Summary

LinkSet is a little experiment with a light, dynamic approach to Java listeners (and event buses now).
If you like it... enjoy using it... and report bugs at github.com/lbownik/linkset.

History

  • 11th October, 2010: Initial version

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)