eventhandler java_简单的Java Event-事件框架

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

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

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

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

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

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

类图如下:

1b22372258aee44b894c44272d533bf7.png

测试:

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) ->回调Callbackimport 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();

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值