Click here to Skip to main content
Click here to Skip to main content

Tagged as

Linkset Gets An Event Bus Functionality

, 13 Oct 2010 LGPL3
Rate this:
Please Sign up or sign in to vote.
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 (http://code.google.com/p/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...

package package com.mycompany.project1;

public class Event {

}

Simple Event Bus

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

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.

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...

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...

package package com.mycompany.project1;

public class SubEvent extends Event {

}

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

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.

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 http://code.google.com/p/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)

Share

About the Author

No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 13 Oct 2010
Article Copyright 2010 by Łukasz Bownik
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid