-
Notifications
You must be signed in to change notification settings - Fork 5
Event
Event framework provides dispatch event mechanism, it supports asynchronism and synchronism based event dispatch.
Define an event by implement IEvent:
public class MyEvent implements IEvent {
public static final String TOPIC = "MyTopic";
private final String _name;
public MyEvent(String name) {
this._name = name;
}
@Override
public String topic() {
return TOPIC;
}
public String name() {
return this._name;
}
}
All event must provides a event topic which will be used to match a related event handler.
We need define an event handler to handle the event:
@Service(IEventHandler.class)
@Tag("Event Demo")
public class MyEventHandler implements IEventHandler {
@Inject
protected ILogger _logger;
@Override
public String topic() {
return MyEvent.TOPIC;
}
@Override
public void handle(IEvent event) {
this._logger.info("Processing a new event...");
assert event != null;
assert event instanceof MyEvent;
assert ((MyEvent) event).name().equals("new event");
}
}
The event handler must a service which is registered as a IEventHandler type service, because IEventBus service inject IEventHandler automatically. And the event handler must implement IEventHandler interface, the interface has two methods, one is topic method which tell IEventBus service how to match event and event handler, the other method called handle, it provides codes for event handling.
Any service can rise event by IEventBus service:
@Service
@Tag("Event Demo")
public class EventSource {
@Inject
protected IEventBus _eventBus;
public void riseEvent() {
this._eventBus.fire(new MyEvent("new event"));
}
}
The fire method of IEventBus can rise any type event, fire method has two versions, above code fragment shows asynchronism version which means the event handling does not block caller thread, the other fire method version is:
void fire(IEvent event, boolean syncable) throws NoEventHandlerException;
Invoke this method will block caller until the event handling finish.
Configuration Path | Configuration Type | Description | Is Required | Default Value (Behavior) |
---|---|---|---|---|
event.await-time | IntervalTime | Specified waiting time when EventBus service is destroyed | No | 10s |