码迷,mamicode.com
首页 > 编程语言 > 详细

简单的Java Event-事件框架

时间:2015-03-18 20:38:00      阅读:559      评论:0      收藏:0      [点我收藏+]

标签:java   event   


代码:http://git.oschina.net/jmpp/CommonEvent

自己写的一个简单的Java事件框架。目前具备以下功能:

1.通过继承Event类,用户可自定义事件。

2.通过EventService 的fireEvent(Event e) 发出一个事件。

3.通过实现IEventHandler接口,监听某类事件。EventHandler可以动态注册到EventServer,也可以在配置文件中配置。

4.通过实现IEventConsumedCallback接口,在事件被处理后,通知事件发出者。IEventConsumedCallback可在fireEvent时指定,也可不指定。

5.fireEvent  和 Consume Event都是异步进行,Consume Event时采用线程池处理。


类图如下:

技术分享


代码:http://git.oschina.net/jmpp/CommonEvent(包含测试代码)


测试:

1.自定义Event

import com.lenovo.commonevent.Event;

/**
 * Project:      CommonEvent
 * FileName:     TestEvent.java
 * @Description: TODO
 * @author:      jmpp 
 * Createdate:   2015年3月16日 下午5:57:12
 * Copyright:    Copyright(C) 2014-2015
 * Company       Lenovo LTD.
 * All rights Reserved, Designed By Lenovo CIC.
 */

/**
 * 类 TestEvent 的实现描述:TODO 类实现描述
 * 
 * @author jmpp 2015年3月16日下午5:57:12
 */
public class TestEvent extends Event {

    public TestEvent() {
        super(TestEvent.class.getSimpleName());
    }
}


2.定义一个EventHandler

import java.util.Date;

import com.lenovo.commonevent.Event;
import com.lenovo.commonevent.IEventHandler;

/**
 * Project:      CommonEvent
 * FileName:     TestEventHandler.java
 * @Description: TODO
 * @author:      jmpp
 * @version      V1.0 
 * Createdate:   2015年3月16日 下午6:00:34
 * Copyright:    Copyright(C) 2014-2015
 * Company       Lenovo LTD.
 * All rights Reserved, Designed By Lenovo CIC.
 */

/**
 * 类 TestEventHandler 的实现描述:TODO 类实现描述
 * 
 * @author jmpp 2015年3月16日下午6:00:34
 */
public class TestEventHandler implements IEventHandler {

    /**
     * @author jmpp 2015年3月16日下午6:00:48
     */
    @Override
    public Object onEvent(Event event) {

        System.out.println("On event  " + event.getId() + " Type:" + event.getType());
        return new Date();
    }

}


3.定义EventConsumedCallback

import java.util.Date;

import com.lenovo.commonevent.Event;
import com.lenovo.commonevent.EventService;
import com.lenovo.commonevent.IEventConsumedCallback;

/**
 * Project:      CommonEvent
 * FileName:     TestEventInvoker.java
 * @Description: TODO
 * @author:      jmpp
 * @version      V1.0 
 * Createdate:   2015年3月16日 下午6:03:47
 * Copyright:    Copyright(C) 2014-2015
 * Company       Lenovo LTD.
 * All rights Reserved, Designed By Lenovo CIC.
 */

/**
 * 类 TestEventInvoker 的实现描述:TODO 类实现描述
 * 
 * @author jmpp 2015年3月16日下午6:03:47
 */
public class TestEventInvoker implements IEventConsumedCallback {

    /**
     * @author jmpp 2015年3月16日下午6:04:02
     */
    @SuppressWarnings("deprecation")
    @Override
    public void onEventFinished(Event event, Object result) {
        System.out.println("Event callback " + event.getId() + " at "
                + ((Date) result).toLocaleString());

    }
}


4.测试调用  触发事件-〉处理事件(Handle) ->回调Callback

import java.util.Date;

import com.lenovo.commonevent.Event;
import com.lenovo.commonevent.EventService;
import com.lenovo.commonevent.IEventConsumedCallback;

/**
 * Project:      CommonEvent
 * FileName:     TestEventInvoker2.java
 * @Description: TODO
 * @author:      jmpp
 * @version      V1.0 
 * Createdate:   2015年3月16日 下午6:03:47
 * Copyright:    Copyright(C) 2014-2015
 * Company       Lenovo LTD.
 * All rights Reserved, Designed By Lenovo CIC.
 */

/**
 * 类 TestEventInvoker2 的实现描述:TODO 类实现描述
 * 
 * @author jmpp 2015年3月16日下午6:03:47
 */
public class TestEventInvoker2  {

    public static void main(String args[]) throws Exception {

        EventService.init(null);

        EventService.registerEventHandler(TestEvent.class.getSimpleName(), new TestEventHandler());

        for (int i = 0; i < 10; i++) {
            TestEvent event = new TestEvent();
            EventService.fireEvent(event, new TestEventInvoker());
        }

        Thread.sleep(5000);

        EventService.stop();
    }
}


简单的Java Event-事件框架

标签:java   event   

原文地址:http://blog.csdn.net/jmppok/article/details/44417275

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!