W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
事件鏈,指的是在事件中引發(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);
}
}
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: