Hasor 事件鏈

2018-10-07 09:49 更新

事件鏈,指的是在事件中引發(fā)另一個(gè)或多個(gè)事件,一個(gè)完整的事件鏈的例子如下:


public class MyListener implements EventListener<Object> {
    public void onEvent(String event, Object eventData) throws InterruptedException {
        Thread.sleep(500);
        System.out.println("Receive Message:" + JSON.toJSONString(eventData));
        throw new NullPointerException();
    }
}

public class EventLinkTest {
    @Test
    public void syncEventTest() throws InterruptedException {
        System.out.println("--->>syncEventTest<<--");
        AppContext appContext = Hasor.createAppContext();
        EventContext ec = appContext.getEnvironment().getEventContext();
        //
        final String EventName = "MyEvent";//事件鏈的終端
        final String SeedEvent = "SeedEvent";//種子事件
        //1.添加事件監(jiān)聽(tīng)器
        ec.addListener(EventName, new MyListener());
        ec.addListener(SeedEvent, new EventListener<AppContext>() {
            public void onEvent(String event, AppContext app) throws Throwable {
                EventContext localEC = app.getEnvironment().getEventContext();
                System.out.println("before MyEvent.");
                localEC.fireAsyncEvent(EventName, 1);
                localEC.fireAsyncEvent(EventName, 2);
            }
        });
        //2.引發(fā)種子事件
        ec.fireAsyncEvent(SeedEvent, appContext);
        //3.由于是異步事件,因此下面這條日志會(huì)在所有事件之前噴出
        System.out.println("before All Event.");
        Thread.sleep(1000);
    }
}


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)