use of com.shulie.instrument.simulator.api.listener.EventListener in project LinkAgent by shulieTech.
the class LazyEventListenerProxy method getEventListenerWrapper.
/**
* 获取事件监听器,会涉及到初始化的动作
* <p>
* 非中间件模块使用单实例,不会根据业务类加载器生成多实例
*
* @return 返回事件监听器
*/
public EventListenerWrapper getEventListenerWrapper(ClassLoader bizClassLoader) {
int id = getBizClassLoaderId(bizClassLoader);
EventListenerWrapper eventListenerWrapper = this.eventListeners.get(id);
if (eventListenerWrapper != null) {
return eventListenerWrapper;
}
synchronized (this) {
eventListenerWrapper = this.eventListeners.get(id);
if (eventListenerWrapper != null) {
return eventListenerWrapper;
}
ClassLoader classLoader = coreModule.getClassLoader(bizClassLoader);
try {
Object listener = null;
if (listeners.getArgs() == null || listeners.getArgs().length == 0) {
listener = Reflect.on(listeners.getClassName(), classLoader).create().get();
} else {
listener = Reflect.on(listeners.getClassName(), classLoader).create(listeners.getArgs()).get();
}
/**
* 设置是否可中断
*/
if (isInitInterrupt.compareAndSet(false, true)) {
isInterrupt = ReflectUtils.isInterruptEventHandler(listener);
}
if (listener != null) {
eventListenerWrapper = new EventListenerWrapper();
try {
coreModule.injectResource(listener);
} catch (ModuleException e) {
logger.warn("SIMULATOR: can't inject resource into event listener. by module={} listener:{}", coreModule.getModuleId(), listeners, e);
}
if (listener instanceof InitializingBean) {
((InitializingBean) listener).init();
}
EventListener eventListener = null;
if (listener instanceof EventListener) {
eventListener = (EventListener) listener;
if (listeners.getScopeName() != null && listeners.getEventListenerCallback() != null) {
eventListener = listeners.getEventListenerCallback().onCall(eventListener, listeners.getScopeName(), listeners.getExecutionPolicy());
}
eventListener.setBizClassLoader(bizClassLoader);
} else if (listener instanceof AdviceListener) {
AdviceListener adviceListener = (AdviceListener) listener;
adviceListener = new ExtensionAdviceWrapContainer(adviceListener);
if (listeners.getScopeName() != null && listeners.getAdviceListenerCallback() != null) {
adviceListener = listeners.getAdviceListenerCallback().onCall(adviceListener, listeners.getScopeName(), listeners.getExecutionPolicy());
}
adviceListener.setBizClassLoader(bizClassLoader);
eventListener = new AdviceAdapterListener(adviceListener);
}
eventListenerWrapper.setEventListener(eventListener);
Destroyable destroyable = listener.getClass().getAnnotation(Destroyable.class);
if (destroyable != null) {
Destroyed destroyed = Reflect.on(destroyable.value()).create().get();
eventListenerWrapper.setDestroyed(destroyed);
}
}
if (eventListenerWrapper != null) {
EventListenerWrapper old = this.eventListeners.putIfAbsent(id, eventListenerWrapper);
if (old != null) {
logger.error("SIMULATOR: EventListenerWrapper is exists:{} , {} ", bizClassLoader, old);
eventListenerWrapper = old;
}
}
} catch (Throwable e) {
logger.error("SIMULATOR: event listener onEvent failed, cause by event listener init failed:{}.", listeners.getClassName(), e);
return null;
}
}
return eventListenerWrapper;
}
use of com.shulie.instrument.simulator.api.listener.EventListener in project LinkAgent by shulieTech.
the class LazyEventListenerProxy method isInterrupted.
@Override
public boolean isInterrupted() {
if (isInitInterrupt.get()) {
return this.isInterrupt;
}
try {
if (!eventListeners.isEmpty()) {
EventListener eventListener = eventListeners.values().iterator().next();
boolean isInterrupt = ReflectUtils.isInterruptEventHandler(eventListener);
if (isInitInterrupt.compareAndSet(false, true)) {
this.isInterrupt = isInterrupt;
return this.isInterrupt;
}
}
Class clazz = coreModule.getClassLoaderFactory().getDefaultClassLoader().loadClass(listeners.getClassName());
boolean isInterrupt = ReflectUtils.isInterruptEventHandler(clazz);
if (isInitInterrupt.compareAndSet(false, true)) {
this.isInterrupt = isInterrupt;
}
return this.isInterrupt;
} catch (ClassNotFoundException e) {
logger.error("SIMULATOR: can't found class {} by ModuleClassLoader:{}.", listeners.getClassName(), coreModule.getClassLoaderFactory().getDefaultClassLoader(), e);
return false;
} catch (Throwable e) {
logger.error("SIMULATOR: can't found class {} by ModuleClassLoader:{}.", listeners.getClassName(), coreModule.getClassLoaderFactory().getDefaultClassLoader(), e);
return false;
}
}
use of com.shulie.instrument.simulator.api.listener.EventListener in project LinkAgent by shulieTech.
the class EventListenerHandler method handleEvent.
/**
* 调用出发事件处理&调用执行流程控制
*
* @param listenerId 处理器ID
* @param processId 调用过程ID
* @param invokeId 调用ID
* @param event 调用事件
* @param processor 事件处理器
* @return 处理返回结果
* @throws Throwable 当出现未知异常时,且事件处理器为中断流程事件时抛出
*/
private final Result handleEvent(final int listenerId, final int processId, final int invokeId, final Class clazz, final Event event, final InvokeProcessor processor) throws Throwable {
final EventListener listener = processor.listener;
/**
* 如果当前事件不在事件监听器处理列表中,则直接返回,不处理事件
*/
if (!ArrayUtils.contains(processor.eventEventTypes, event.getType())) {
return Result.RESULT_NONE;
}
try {
if (isDebugEnabled) {
logger.debug("SIMULATOR: on-event: event -> eventType={}, processId={}, invokeId={}, listenerId={}", event.getType(), processId, invokeId, listenerId);
}
/**
* 调用事件处理
*/
ProcessControlEntity pce = listener.onEvent(event);
int state = pce.getState();
if (state != ProcessControlEntity.NONE_IMMEDIATELY) {
return processInterrupt(listenerId, processId, invokeId, clazz, event, processor, pce);
}
} catch (ProcessControlException pce) {
return processInterrupt(listenerId, processId, invokeId, clazz, event, processor, pce.toEntity());
} catch (Throwable throwable) {
/**
* 如果当前事件处理器是可中断的事件处理器,则对外抛出UnCaughtException
* 中断当前方法
*/
if (ReflectUtils.isInterruptEventHandler(listener)) {
throw throwable;
} else {
/**
* 普通事件处理器则可以打个日志后,直接放行
*/
ExceptionHandler exceptionHandler = getExceptionHandler();
if (exceptionHandler != null) {
exceptionHandler.handleException(throwable, String.format("on-event: event -> eventType=%s, processId=%s, invokeId=%s, listenerId=%s occur an error.", EventType.name(event.getType()), processId, invokeId, listenerId), listener);
} else {
logger.warn("SIMULATOR: on-event: event -> eventType={}, processId={}, invokeId={}, listenerId={} occur an " + "error.", event.getType(), processId, invokeId, listenerId, throwable);
}
}
}
/**
* 默认返回不进行任何流程变更
*/
return Result.RESULT_NONE;
}
Aggregations