Friday, April 9, 2010

Distributed component events

The application is inspired from the Seam Component Events and uses Spring AOP for event trapping and JGroup for event distribution. The system is divided into two parts Event Generation and Event Consumption. Simple POJOs can be turned into event generator or event consumer.

// Event Generator
@Component("temperature-sensor")
public class TemperatureManager{
    @Out
    private Temperature currentTemperature;

    @RaiseEvent("temperature-updated")
    public void temperatureUpdated(Temperature t){
        this.currentTemperature = t;
    }
}
// Event Consumer
@Component("graph-generator")
public class GraphGenerator{
    @In
    private Temperature currentTemperature;

    @ConsumeEvent("temperature-updated")
    public void updateTemperature(){
        graph.displayTemperature(currentTemperature);
        // refresh graph
    }
}

The above two classes can be part of a single JVM or multiple JVM, with JGroup in place Event Consumer will still consume the events generated by the Event Generator running in different JVM. Spring AOP keeps track of the RaiseEvent annotation and does all the distribution and communication stuff.

Full source code and required libraries with NetBeans project is available at : component-events.rar

No comments:

Post a Comment