use of com.shulie.instrument.simulator.api.listener.ext.AdviceAdapterListener 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;
}
Aggregations