You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
2.1 KiB
80 lines
2.1 KiB
package com.sinosoft.event;
|
|
|
|
import com.google.common.eventbus.AsyncEventBus;
|
|
import com.google.common.eventbus.EventBus;
|
|
import com.sinosoft.event.BussinessEvent;
|
|
import com.sinosoft.event.MonitorEvent;
|
|
import com.sinosoft.event.listener.BussinessEventListener;
|
|
import com.sinosoft.event.listener.DeadEventListener;
|
|
import com.sinosoft.event.listener.MonitorEventListener;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import java.util.concurrent.ExecutorService;
|
|
import java.util.concurrent.Executors;
|
|
|
|
public class EventSender {
|
|
private static final Logger logger = LoggerFactory.getLogger(EventSender.class);
|
|
private static EventBus eventBus = null;
|
|
private static EventBus asyncEventBus = null;
|
|
private static ExecutorService pool = null;
|
|
static {
|
|
init();
|
|
}
|
|
|
|
private static void init() {
|
|
if (eventBus == null) {
|
|
eventBus = new EventBus("EventEngine");
|
|
//registerAllListener();
|
|
|
|
registerListener(new BussinessEventListener());
|
|
registerListener(new MonitorEventListener());
|
|
registerListener(new DeadEventListener());
|
|
}
|
|
|
|
if (asyncEventBus == null) {
|
|
pool = Executors.newFixedThreadPool(50);
|
|
//pool = Executors.newCachedThreadPool();
|
|
asyncEventBus = new AsyncEventBus("AsyncEventEngine",pool);
|
|
//registerAllListener();
|
|
|
|
registerAsyncBusListener(new BussinessEventListener());
|
|
registerAsyncBusListener(new MonitorEventListener());
|
|
registerAsyncBusListener(new DeadEventListener());
|
|
}
|
|
}
|
|
|
|
|
|
public static void postEvent(Event event) {
|
|
eventBus.post(event);
|
|
}
|
|
|
|
public static void postAsyncEvent(Event event) {
|
|
asyncEventBus.post(event);
|
|
}
|
|
|
|
|
|
/**
|
|
* 注册监听
|
|
*/
|
|
public static void registerListener(EventListener listener) {
|
|
try {
|
|
eventBus.register(listener);
|
|
} catch (Exception e) {
|
|
// TODO Auto-generated catch block
|
|
logger.error("程序执行异常",e);
|
|
}
|
|
}
|
|
|
|
|
|
public static void registerAsyncBusListener(EventListener listener) {
|
|
try {
|
|
asyncEventBus.register(listener);
|
|
} catch (Exception e) {
|
|
// TODO Auto-generated catch block
|
|
logger.error("程序执行异常",e);
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
|