Ark event mechanism

Updated at:

SOFAArk provides a simple event bus. Plugin and Biz can use the event bus service EventAdminService to register event listeners and deliver and listen to events.

Example:

public interface EventAdminService{

/**
     * Initiate synchronous delivery of an event. This method does not return to
     * the caller until delivery of the event is completed.
     *
     * @param event The event to send to all listeners which subscribe to the
     *        topic of the event.
     */
void sendEvent(ArkEvent event);

/**
     * Register an event handler
     *
     * @param eventHandler
     */
void register(EventHandler eventHandler);

/**
     * un-register an event handler
     * @param eventHandler
     */
void unRegister(EventHandler eventHandler);

/**
     * un-register event handler whose classLoader matches the specified param.
     * @param classLoader
     */
void unRegister(ClassLoader classLoader);
}

The EventAdminSerivce interface defines four methods through which Plugin and Biz can easily register event listeners and send events. Currently, only synchronous events are supported.

Event

Implement a unified event interface type:

public interface ArkEvent{
/**
     * Returns the topic of event
     *
     * @return String return event topic
     */
String getTopic();
}

Call the EventAdminService.sendEvent method to broadcast the event and notify all listeners of the EventHandler.

Event sistener

Implement the unified event listener interface:

public interface EventHandler extends PriorityOrdered{
/**
     * Called by the {@link EventAdminService} service to notify the listener of an
     * event.
     *
     * @param event The event that occurred.
     */
void handleEvent(ArkEvent event);
}

Call the EventAdminService.register method to register a listener to receive broadcast events.

Precautions

Only event listeners can be manually registered inside Plugin. If Biz is a Spring Boot/SOFABoot application, all Beans that implement interface EventHandler will be registered by default, which is also the recommend usage in Biz. When the application is uninstalled, the listener will be automatically unregistered.