JGrapes

Mastodon Follow

JGrapes Logo

View GitHub Project

Overview

JGrapes

Web Console

Hello World!

Here’s our first component.

import org.jgrapes.core.Component;
import org.jgrapes.core.Components;
import org.jgrapes.core.annotation.Handler;
import org.jgrapes.core.events.Start;

public class Greeter extends Component {

    @Handler
    public void onStart(Start event) {
        System.out.println ("Hello World!");
    }
    
    public static void main(String[] args) 
            throws InterruptedException {
        Components.start(new Greeter());
    }
}

The easiest way to define a new component is to inherit from Component. Components define methods that can be annotated as handlers for events.

A predefined event from the core package is the Start event. This event triggers the event processing by the framework. Other events, that have been fired before the Start event, won’t be delivered to handlers until the Start event has been fired.

The easiest way to fire the Start event at the application is shown in the code: invoking the convenience method from Components1. This method fires a Start event on the broadcast channel of the given application and waits until it is processed.

In our application, the Start event is handled by the method onStart of the class Greeter, which simply outputs “Hello World!”. The method is marked as a handler method using the @Handler annotation. The annotation is evaluated when a new Component is created. By reflection, it finds that the annotated method accepts an event of type Start as first parameter and therefore registers it as handler for such an event.


  1. Note the “s” at the end.