import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
public class EventManager {
public static final int Initial_WorkerThread_number = 10;
public static final int Max_WorkerThread_number = 100;
private static EventManager instance = null;
private EventQueue eventQueue = null;
private EventRegistry registry = null;
private WorkerThreadFactory workerThreadfactory = null;
private ExecutorService threadpool = Executors.newCachedThreadPool();
private List<Runnable> workerList = new LinkedList<Runnable>();
private CustomEventFactory eventFactory = null;
/******************
* 方法
* *****************/
private EventManager() {
eventQueue = new EventQueue();
registry = new EventRegistry();
workerThreadfactory = new WorkerThreadFactory();
eventFactory = new CustomEventFactory();
launchWorkerThreads();
}
private void launchWorkerThreads() {
for( int i=0;i<EventManager.Initial_WorkerThread_number;i++ ) {
workerList.add(workerThreadfactory.createWorkerThread());
}
for(Runnable thread:workerList) {
threadpool.execute(thread);
}
}
/*目前先暂定义一个默认的事件类型*/
public CustomEventFactory getEventFactory() {
return eventFactory;
}
/*业务调用此方法推送事件*/
public void postEvent(CustomDefineEventBase event) {
eventQueue.putEvent(event);
}
public void registerEvent(CustomDefineEventBase event,EventObserver observer) {
registry.register(event, observer);
}
public void deRegisterEvent(CustomDefineEventBase event,EventObserver observer) {
registry.deRegister(event, observer);
}
public static EventManager getInstance() {
if( null == instance ) {
synchronized(EventManager.class) {
if( null == instance ) {
instance = new EventManager();
}
}
}
return instance;
}
/***************************
* 私有类
* */
private class WorkerThread implements Runnable {
@Override
public void run() {
// TODO Auto-generated method stub
/*get posted event from the EventQueue,and get the ObserverList attached to the event and notify all observers in the list.*/
while(!Thread.currentThread().isInterrupted()) {
CustomDefineEventBase event = eventQueue.getEvent();
if( null != event ) {
registry.doNotify(event);
}
}
}
}
private class WorkerThreadFactory {
public Runnable createWorkerThread() {
return new WorkerThread();
}
}
private class EventQueue {
private LinkedBlockingQueue<CustomDefineEventBase> eventQueue = new LinkedBlockingQueue<CustomDefineEventBase>();
public CustomDefineEventBase getEvent() {
CustomDefineEventBase event = null;
try {
event = eventQueue.take();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return event;
}
public void putEvent(CustomDefineEventBase event) {
try {
eventQueue.put(event);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private class EventRegistry {
private ConcurrentHashMap<String,List<EventObserver>> registry = new ConcurrentHashMap<String,List<EventObserver>>();
public void register(CustomDefineEventBase event,EventObserver observer) {
if( true == registry.containsKey(event.getEventName()) ) {
List<EventObserver> list = registry.get(event.getEventName());
if( null != list ) {
list.add(observer);
}
} else {
List<EventObserver> list2Add = new LinkedList<EventObserver>();
list2Add.add(observer);
registry.put(event.getEventName(), list2Add);
}
}
public void deRegister(CustomDefineEventBase event,EventObserver observer) {
if( true == registry.containsKey(event.getEventName()) ) {
List<EventObserver> list = registry.get(event.getEventName());
if( null != list ) {
if( list.size() > 0 ) {
list.remove(observer);
} else {
registry.remove(event.getEventName()); /*如果该事件没有observer,则从注册表中清除*/
}
}
}
}
public void doNotify(CustomDefineEventBase event) {
List<EventObserver> list = registry.get(event.getEventName());
if( null != list ) {
for(EventObserver observer:list) {
observer.notifyEvent(event);
}
}
}
}
}
【代码积累】Event handling framrwork
最新推荐文章于 2021-11-14 13:34:40 发布