Raise events

You must now raise events at appropriate points in the class's logic. Retrieve an instance of your dispatcher to call the methods on your Event interface:

Figure 1. Raising events
          package curam.mypackage;

import com.google.inject.Inject;

import curam.util.persistence.helper.EventDispatcherFactory;

public class MyEventSource {

  public abstract class Event {
    public void preDoSomething(MyEventSource raiser) {
     // intentionally empty
    }

    public void postDoSomething(MyEventSource raiser) {
     // intentionally empty
    }

  }

  @Inject
  private EventDispatcherFactory<Event> dispatcher;

  public void doSomething() {
           // notify listeners that something is about to happen
    dispatcher.get(Event.class).preDoSomething(this); 
          // do whatever it is that needs to be done
    System.out.println("Do something!");
           // notify listeners that something has just been done
    dispatcher.get(Event.class).postDoSomething(this); 
          }
}

Note how the dispatcher.get method took the class of the Event interface as a parameter. Calling this method returned an event "multiplexer" instance on which any method call will be dispatched to each of the registered listeners for that event interface.

This completes the coding to raise events. You can now move on to create listeners.