Event service (Event)
Event service completes the processing of event messages through event registration, subscription and broadcast. The purpose is to reduce code intrusion and reduce business coupling between modules. Event messages are stored in queues and multi-threaded interface callbacks are used. Realizes the transmission of messages and message context objects, supporting both synchronous and asynchronous processing modes;
Frame event initialization configuration parameters
#------------------------------------- # 框架事件初始化参数 #------------------------------------- # 默认事件触发模式(不区分大小写),取值范围:NORMAL-同步执行,ASYNC-异步执行,默认为ASYNC ymp.event.default_mode= # 事件管理提供者接口实现,默认为net.ymate.platform.core.event.impl.DefaultEventProvider ymp.event.provider_class= # 事件线程池初始化大小,默认为Runtime.getRuntime().availableProcessors() ymp.event.thread_pool_size= # 事件配置扩展参数,xxx表示自定义参数名称,vvv表示参数值 ymp.event.params.xxx=vvv
YMP Core Event Object
ApplicationEvent: Framework Event
APPLICATION_INITED - 框架初始化 APPLICATION_DESTROYED - 框架销毁
ModuleEvent: Module event
MODULE_INITED - 模块初始化 MODULE_DESTROYED - 模块销毁
##Note: The above are only included in the core of the YMP framework Event objects, event objects contained in other modules will be described in their corresponding document descriptions;
Subscription to events
- Method 1: Manually complete the event subscription through code
public static void main(String[] args) throws Exception { YMP.get().init(); try { // 订阅模块事件 YMP.get().getEvents().registerListener(ModuleEvent.class, new IEventListener<ModuleEvent>() { @Override public boolean handle(ModuleEvent context) { switch (context.getEventName()) { case MODULE_INITED: // 注意:这段代码是不会被执行的,因为在我们进行事件订阅时,模块的初始化动作已经完成 System.out.println("Inited :" + context.getSource().getName()); break; case MODULE_DESTROYED: System.out.println("Destroyed :" + context.getSource().getName()); break; } return false; } }); } finally { YMP.get().destroy(); } }
- Method 2: Implement the event through the
@EventRegister
annotation and IEventRegister interface Subscription
// 首先创建事件注册类,通过实现IEventRegister接口完成事件的订阅 // 通过@EventRegister注解,该类将在YMP框架初始化时被自动加载 @EventRegister public class DemoEventRegister implements IEventRegister { public void register(Events events) throws Exception { // 订阅模块事件 events.registerListener(ModuleEvent.class, new IEventListener<ModuleEvent>() { @Override public boolean handle(ModuleEvent context) { switch (context.getEventName()) { case MODULE_INITED: System.out.println("Inited :" + context.getSource().getName()); break; case MODULE_DESTROYED: System.out.println("Destroyed :" + context.getSource().getName()); break; } return false; } }); // // ... 还可以添加更多的事件订阅代码 } } // 框架启动测试 public static void main(String[] args) throws Exception { YMP.get().init(); try { // Do Nothing... } finally { YMP.get().destroy(); } }
Custom event
YMP event object must implement the IEvent interface At the same time, you need to inherit the EventContext object. The following code is a custom event object:- Create a custom event objectNote: The first parameter in the annotation of EventContext represents the event source object type, and the second parameter is the enumeration type that specifies the event name for event monitoring;
public class DemoEvent extends EventContext<Object, DemoEvent.EVENT> implements IEvent { public enum EVENT { CUSTOM_EVENT_ONE, CUSTOM_EVENT_TWO } public DemoEvent(Object owner, Class<? extends IEvent> eventClass, EVENT eventName) { super(owner, eventClass, eventName); } }
- Registration Custom event
YMP.get().getEvents().registerEvent(DemoEvent.class);
- Subscribe to custom eventEvent subscription (or listening) needs to implement the IEventListener interface, which The return value of the handle method of the interface will affect whether the event listening queue terminates execution in asynchronous trigger mode. Please ignore this return value in synchronous trigger mode; Of course , you can also subscribe to custom events through the
YMP.get().getEvents().registerListener(DemoEvent.class, new IEventListener<DemoEvent>() { public boolean handle(DemoEvent context) { switch (context.getEventName()) { case CUSTOM_EVENT_ONE: System.out.println("CUSTOM_EVENT_ONE"); break; case CUSTOM_EVENT_TWO: System.out.println("CUSTOM_EVENT_TWO"); break; } return false; } });
@EventRegister
annotation and IEventRegister interface;
Note: When an event is triggered, subscribe (or listen) The order in which the event interface is executed by callbacks cannot be guaranteed;
- Trigger custom events
// 采用默认模式触发事件 YMP.get().getEvents().fireEvent(new DemoEvent(YMP.get(), DemoEvent.class, DemoEvent.EVENT.CUSTOM_EVENT_ONE)); // 采用异步模式触发事件 YMP.get().getEvents().fireEvent(Events.MODE.ASYNC, new DemoEvent(YMP.get(), DemoEvent.class, DemoEvent.EVENT.CUSTOM_EVENT_TWO));
- Sample test code:
public static void main(String[] args) throws Exception { YMP.get().init(); try { // 注册自定义事件对象 YMP.get().getEvents().registerEvent(DemoEvent.class); // 注册自定义事件监听 YMP.get().getEvents().registerListener(DemoEvent.class, new IEventListener<DemoEvent>() { public boolean handle(DemoEvent context) { switch (context.getEventName()) { case CUSTOM_EVENT_ONE: System.out.println("CUSTOM_EVENT_ONE"); break; case CUSTOM_EVENT_TWO: System.out.println("CUSTOM_EVENT_TWO"); break; } return false; } }); // 采用默认模式触发事件 YMP.get().getEvents().fireEvent(new DemoEvent(YMP.get(), DemoEvent.class, DemoEvent.EVENT.CUSTOM_EVENT_ONE)); // 采用异步模式触发事件 YMP.get().getEvents().fireEvent(Events.MODE.ASYNC, new DemoEvent(YMP.get(), DemoEvent.class, DemoEvent.EVENT.CUSTOM_EVENT_TWO)); } finally { YMP.get().destroy(); } }