简单的Java Event-事件框架

本文介绍了一个简单的Java事件框架,具备自定义事件、动态事件处理等功能。通过实现IEventHandler接口,用户可以监听特定类型的事件并进行处理。此外,框架支持异步事件消费,并提供回调机制来通知事件发出者。通过测试代码展示了如何使用该框架触发事件、处理事件及回调。


代码: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();
    }
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值