天天看點

事件架構使用執行個體

使用執行個體

10.6.1 定義事件類型

public class ExampleEventType extends AbstractEventType  {

    public static EventType type1 = new ExampleEventType("type1");

    public static EventType type2 = new ExampleEventType("type2");

    public static EventType type2withtarget = new ExampleEventType("type2 with target");   

    public ExampleEventType(String eventtype)

    {

       super.eventtype = eventtype;

    }

10.6.2定義事件監聽器,實作事件處理方法并注冊在事件激發器中

public class ExampleListener implements Listener{

    /**

     * 注冊監聽器,監聽ExampleEventType.type1和ExampleEventType.type2兩種類型的事件

     * 事件消息可以是本地事件,可以是遠端本地消息,也可以是遠端消息

     */

    public void init()

       List eventtypes = new ArrayList();

       eventtypes.add(ExampleEventType.type1);

       eventtypes.add(ExampleEventType.type2);

       eventtypes.add(ExampleEventType.type2withtarget);

       //監聽器監聽的事件消息可以是本地事件,可以是遠端本地消息,也可以是遠端消息

       //如果不指定eventtypes則監聽所有類型的事件消息

       NotifiableFactory.getNotifiable().addListener(this, eventtypes);

       /**

        * 隻監聽本地消息和本地釋出的本地遠端消息

        * NotifiableFactory.getNotifiable().addListener(this, eventtypes,Listener.LOCAL);

        * 隻監聽本地消息和從遠端消息廣播器釋出的遠端消息和遠端本地消息

        * NotifiableFactory.getNotifiable().addListener(this, eventtypes,Listener.LOCAL_REMOTE);

        * 隻監聽從遠端消息廣播器釋出的遠端消息和遠端本地消息

        * NotifiableFactory.getNotifiable().addListener(this, eventtypes,Listener.REMOTE);

        */      

     * 處理監聽到的消息

    public void handle(Event e) {

       if(e == null)

           return;

       if(e.getType().equals(ExampleEventType.type1))

       {

           System.out.println("Receive event type is " + e.getType());          

       }

       else if(e.getType().equals(ExampleEventType.type2))

           System.out.println("Receive event type is " + e.getType());

       else if(e.getType().equals(ExampleEventType.type2withtarget))

           System.out.println("Receive event type is " + e.getType() + " from "  + e.getEventTarget());

       else

           System.out.println("Unknown event type :" + e.getType());

       }      

    } 

10.6.3 釋出具體的事件消息 

public class ExampleEventPublish {

    public static void publishEventtype1()

        * 建構事件消息【hello world type2.】,指定了事件的類型為ExampleEventType.type2

        * 預設的事件消息廣播途徑為Event.REMOTELOCAL,你可以指定自己的事件廣播途徑

        *  消息隻在本地傳播,消息被發送給所有對ExampleEventType.type2類型消息感興趣的本地消息監聽器和本地遠端事件監聽器

         * Event event = new EventImpl("hello world type2.",ExampleEventType.type2,Event.LOCAL);

        *  消息在網絡上傳播,消息被發送給所有對ExampleEventType.type2類型消息感興趣的遠端消息監聽器和本地遠端事件監聽器,同時也會直接發送給本地監聽器

        *  Event event = new EventImpl("hello world type2.",ExampleEventType.type2,Event.REMOTELOCAL);

        *  消息在網絡上傳播,消息被發送給所有對ExampleEventType.type2類型消息感興趣的遠端消息監聽器和本地遠端事件監聽器

        *  Event event = new EventImpl("hello world type2.",ExampleEventType.type2,Event.REMOTE);

        */

       Event event = new EventImpl("hello world type1.",ExampleEventType.type1);      

        * 事件以同步方式傳播

       EventHandle.getInstance().change(event);      

    }   

    public static void publishEventtype2()

        * Event event = new EventImpl("hello world type2.",ExampleEventType.type2,Event.LOCAL);

        */   

       Event event = new EventImpl("hello world type2.",ExampleEventType.type2);

        * 消息以異步方式傳遞*/        EventHandle.getInstance().change(event,false);      

    public static void publishEventtype2Withtarget()

        * 建構事件消息【hello world type2.】,指定了事件的類型為ExampleEventType.type2withtarget

        */EventTarget target = new EventTarget("149.16.20.177",8088);

       Event event = new EventImpl("hello world type2 with target[" + target +"].",

                            ExampleEventType.type2withtarget,

                            target,

                            Event.REMOTE);EventHandle.getInstance().change(event);    }

}

10.6.4運作ExampleEventPublish中釋出事件的方法

運作ExampleEventPublish中釋出事件的方法,ExampleListener将會監聽到其釋出的事件,并且調用handle方法處理到監聽到的消息。    

public class TestRun {

    public static void main(String args[])

       ExampleListener listener = new ExampleListener();

       //調用init方法注冊監聽器,這樣就能收到事件釋出器釋出的事件

       listener.init();      

       ExampleEventPublish.publishEventtype1();

       ExampleEventPublish.publishEventtype2();

       ExampleEventPublish.publishEventtype2Withtarget();    }

繼續閱讀