Here are the examples of the java api org.springframework.context.ApplicationListener taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
85 Examples
19
Source : EventListenerMethodProcessor.java
with MIT License
from Vip-Augus
with MIT License
from Vip-Augus
private void processBean(final String beanName, final Clreplaced<?> targetType) {
if (!this.nonAnnotatedClreplacedes.contains(targetType) && AnnotationUtils.isCandidateClreplaced(targetType, EventListener.clreplaced) && !isSpringContainerClreplaced(targetType)) {
Map<Method, EventListener> annotatedMethods = null;
try {
annotatedMethods = MethodIntrospector.selectMethods(targetType, (MethodIntrospector.MetadataLookup<EventListener>) method -> AnnotatedElementUtils.findMergedAnnotation(method, EventListener.clreplaced));
} catch (Throwable ex) {
// An unresolvable type in a method signature, probably from a lazy bean - let's ignore it.
if (logger.isDebugEnabled()) {
logger.debug("Could not resolve methods for bean with name '" + beanName + "'", ex);
}
}
if (CollectionUtils.isEmpty(annotatedMethods)) {
this.nonAnnotatedClreplacedes.add(targetType);
if (logger.isTraceEnabled()) {
logger.trace("No @EventListener annotations found on bean clreplaced: " + targetType.getName());
}
} else {
// Non-empty set of methods
ConfigurableApplicationContext context = this.applicationContext;
replacedert.state(context != null, "No ApplicationContext set");
List<EventListenerFactory> factories = this.eventListenerFactories;
replacedert.state(factories != null, "EventListenerFactory List not initialized");
for (Method method : annotatedMethods.keySet()) {
for (EventListenerFactory factory : factories) {
if (factory.supportsMethod(method)) {
Method methodToUse = AopUtils.selectInvocableMethod(method, context.getType(beanName));
ApplicationListener<?> applicationListener = factory.createApplicationListener(beanName, targetType, methodToUse);
if (applicationListener instanceof ApplicationListenerMethodAdapter) {
((ApplicationListenerMethodAdapter) applicationListener).init(context, this.evaluator);
}
context.addApplicationListener(applicationListener);
break;
}
}
}
if (logger.isDebugEnabled()) {
logger.debug(annotatedMethods.size() + " @EventListener methods processed on bean '" + beanName + "': " + annotatedMethods);
}
}
}
}
19
Source : HierarchyAwareEventListenerMethodProcessor.java
with Apache License 2.0
from spot-next
with Apache License 2.0
from spot-next
private void processBean(final String beanName, final Clreplaced<?> targetType) {
if (!this.nonAnnotatedClreplacedes.contains(targetType) && !isSpringContainerClreplaced(targetType)) {
Map<Method, EventListener> annotatedMethods = null;
try {
annotatedMethods = MethodIntrospector.selectMethods(targetType, (MethodIntrospector.MetadataLookup<EventListener>) method -> AnnotatedElementUtils.findMergedAnnotation(method, EventListener.clreplaced));
} catch (Throwable ex) {
// An unresolvable type in a method signature, probably from a lazy bean - let's ignore it.
if (logger.isDebugEnabled()) {
logger.debug("Could not resolve methods for bean with name '" + beanName + "'", ex);
}
}
if (CollectionUtils.isEmpty(annotatedMethods)) {
this.nonAnnotatedClreplacedes.add(targetType);
if (logger.isTraceEnabled()) {
logger.trace("No @EventListener annotations found on bean clreplaced: " + targetType.getName());
}
} else {
// Non-empty set of methods
ConfigurableApplicationContext context = this.applicationContext;
replacedert.state(context != null, "No ApplicationContext set");
List<EventListenerFactory> factories = this.eventListenerFactories;
replacedert.state(factories != null, "EventListenerFactory List not initialized");
for (Method method : annotatedMethods.keySet()) {
for (EventListenerFactory factory : factories) {
if (factory.supportsMethod(method)) {
Method methodToUse = AopUtils.selectInvocableMethod(method, context.getType(beanName));
ApplicationListener<?> applicationListener = new ApplicationListenerMethodAdapter(beanName, targetType, methodToUse);
// CODE CHANGE
// we only want to register the listener on the root
// context. We cannot create it as the evaluator is
// package protected (duh ...)
// therefore we fetch it using reflection from the
// super clreplaced
// register the listener in the root context
ClreplacedUtil.invokeMethod(applicationListener, "init", context, getEvaluator());
// and add the listener to the root context, instead
// of the current one!
getRootContext(context).addApplicationListener(applicationListener);
// CODE CHANGE
break;
}
}
}
if (logger.isDebugEnabled()) {
logger.debug(annotatedMethods.size() + " @EventListener methods processed on bean '" + beanName + "': " + annotatedMethods);
}
}
}
}
19
Source : EventListenerMethodProcessor.java
with MIT License
from mindcarver
with MIT License
from mindcarver
private void processBean(final String beanName, final Clreplaced<?> targetType) {
if (!this.nonAnnotatedClreplacedes.contains(targetType) && !isSpringContainerClreplaced(targetType)) {
Map<Method, EventListener> annotatedMethods = null;
try {
annotatedMethods = MethodIntrospector.selectMethods(targetType, (MethodIntrospector.MetadataLookup<EventListener>) method -> AnnotatedElementUtils.findMergedAnnotation(method, EventListener.clreplaced));
} catch (Throwable ex) {
// An unresolvable type in a method signature, probably from a lazy bean - let's ignore it.
if (logger.isDebugEnabled()) {
logger.debug("Could not resolve methods for bean with name '" + beanName + "'", ex);
}
}
if (CollectionUtils.isEmpty(annotatedMethods)) {
this.nonAnnotatedClreplacedes.add(targetType);
if (logger.isTraceEnabled()) {
logger.trace("No @EventListener annotations found on bean clreplaced: " + targetType.getName());
}
} else {
// Non-empty set of methods
ConfigurableApplicationContext context = this.applicationContext;
replacedert.state(context != null, "No ApplicationContext set");
List<EventListenerFactory> factories = this.eventListenerFactories;
replacedert.state(factories != null, "EventListenerFactory List not initialized");
for (Method method : annotatedMethods.keySet()) {
for (EventListenerFactory factory : factories) {
if (factory.supportsMethod(method)) {
Method methodToUse = AopUtils.selectInvocableMethod(method, context.getType(beanName));
ApplicationListener<?> applicationListener = factory.createApplicationListener(beanName, targetType, methodToUse);
if (applicationListener instanceof ApplicationListenerMethodAdapter) {
((ApplicationListenerMethodAdapter) applicationListener).init(context, this.evaluator);
}
context.addApplicationListener(applicationListener);
break;
}
}
}
if (logger.isDebugEnabled()) {
logger.debug(annotatedMethods.size() + " @EventListener methods processed on bean '" + beanName + "': " + annotatedMethods);
}
}
}
}
19
Source : GenericApplicationListenerAdapter.java
with Apache License 2.0
from langtianya
with Apache License 2.0
from langtianya
/**
* {@link GenericApplicationListener} adapter that determines supported event types
* through introspecting the generically declared type of the target listener.
*
* @author Juergen Hoeller
* @author Stephane Nicoll
* @since 3.0
* @see org.springframework.context.ApplicationListener#onApplicationEvent
*/
public clreplaced GenericApplicationListenerAdapter implements GenericApplicationListener, SmartApplicationListener {
private final ApplicationListener<ApplicationEvent> delegate;
private final ResolvableType declaredEventType;
/**
* Create a new GenericApplicationListener for the given delegate.
* @param delegate the delegate listener to be invoked
*/
@SuppressWarnings("unchecked")
public GenericApplicationListenerAdapter(ApplicationListener<?> delegate) {
replacedert.notNull(delegate, "Delegate listener must not be null");
this.delegate = (ApplicationListener<ApplicationEvent>) delegate;
this.declaredEventType = resolveDeclaredEventType(this.delegate);
}
@Override
public void onApplicationEvent(ApplicationEvent event) {
this.delegate.onApplicationEvent(event);
}
@Override
@SuppressWarnings("unchecked")
public boolean supportsEventType(ResolvableType eventType) {
if (this.delegate instanceof SmartApplicationListener) {
Clreplaced<? extends ApplicationEvent> eventClreplaced = (Clreplaced<? extends ApplicationEvent>) eventType.getRawClreplaced();
return ((SmartApplicationListener) this.delegate).supportsEventType(eventClreplaced);
} else {
return (this.declaredEventType == null || this.declaredEventType.isreplacedignableFrom(eventType));
}
}
@Override
public boolean supportsEventType(Clreplaced<? extends ApplicationEvent> eventType) {
return supportsEventType(ResolvableType.forType(eventType));
}
@Override
public boolean supportsSourceType(Clreplaced<?> sourceType) {
if (this.delegate instanceof SmartApplicationListener) {
return ((SmartApplicationListener) this.delegate).supportsSourceType(sourceType);
} else {
return true;
}
}
@Override
public int getOrder() {
return (this.delegate instanceof Ordered ? ((Ordered) this.delegate).getOrder() : Ordered.LOWEST_PRECEDENCE);
}
static ResolvableType resolveDeclaredEventType(Clreplaced<?> listenerType) {
ResolvableType resolvableType = ResolvableType.forClreplaced(listenerType).as(ApplicationListener.clreplaced);
if (resolvableType == null || !resolvableType.hasGenerics()) {
return null;
}
return resolvableType.getGeneric();
}
private static ResolvableType resolveDeclaredEventType(ApplicationListener<ApplicationEvent> listener) {
ResolvableType declaredEventType = resolveDeclaredEventType(listener.getClreplaced());
if (declaredEventType == null || declaredEventType.isreplacedignableFrom(ResolvableType.forClreplaced(ApplicationEvent.clreplaced))) {
Clreplaced<?> targetClreplaced = AopUtils.getTargetClreplaced(listener);
if (targetClreplaced != listener.getClreplaced()) {
declaredEventType = resolveDeclaredEventType(targetClreplaced);
}
}
return declaredEventType;
}
}
19
Source : DummyApplicationEventMulticaster.java
with Apache License 2.0
from eclipse
with Apache License 2.0
from eclipse
public void removeApplicationListener(ApplicationListener arg0) {
}
19
Source : DummyApplicationEventMulticaster.java
with Apache License 2.0
from eclipse
with Apache License 2.0
from eclipse
public void addApplicationListener(ApplicationListener listener) {
}
19
Source : DefaultPropertyBackedBeanRegistry.java
with GNU Lesser General Public License v3.0
from Alfresco
with GNU Lesser General Public License v3.0
from Alfresco
/*
* (non-Javadoc)
* @see
* org.alfresco.repo.management.PropertyBackedBeanRegistry#addListener(org.springframework.context.ApplicationListener
* )
*/
public void addListener(ApplicationListener listener) {
this.listeners.add(listener);
}
19
Source : SafeApplicationEventMulticaster.java
with GNU Lesser General Public License v3.0
from Alfresco
with GNU Lesser General Public License v3.0
from Alfresco
public void addApplicationListener(ApplicationListener listener) {
synchronized (this.defaultRetriever) {
this.defaultRetriever.applicationListeners.add(listener);
this.retrieverCache.clear();
}
}
19
Source : SafeApplicationEventMulticaster.java
with GNU Lesser General Public License v3.0
from Alfresco
with GNU Lesser General Public License v3.0
from Alfresco
public void removeApplicationListener(ApplicationListener listener) {
synchronized (this.defaultRetriever) {
this.defaultRetriever.applicationListeners.remove(listener);
this.retrieverCache.clear();
}
}
18
Source : DelegatingApplicationListener.java
with Apache License 2.0
from yuanmabiji
with Apache License 2.0
from yuanmabiji
@Override
public void onApplicationEvent(ApplicationEvent event) {
// 这里监听ApplicationEnvironmentPreparedEvent事件,说明环境变量已经准备好,
// 然后再把配置文件配置的context.listener.clreplacedes监听器加载出来放到multicaster对象的Listerner集合中
if (event instanceof ApplicationEnvironmentPreparedEvent) {
List<ApplicationListener<ApplicationEvent>> delegates = getListeners(((ApplicationEnvironmentPreparedEvent) event).getEnvironment());
if (delegates.isEmpty()) {
return;
}
// 这里新建一个SimpleApplicationEventMulticaster对象来派发事件
this.multicaster = new SimpleApplicationEventMulticaster();
for (ApplicationListener<ApplicationEvent> listener : delegates) {
this.multicaster.addApplicationListener(listener);
}
}
// 这里直接委托配置文件配置的监听器来监听该事件即将事件委托给通过context.listener.clreplacedes配置的自定义的监听器处理。
if (this.multicaster != null) {
this.multicaster.multicastEvent(event);
}
}
18
Source : GenericApplicationListenerAdapterTests.java
with MIT License
from Vip-Augus
with MIT License
from Vip-Augus
private void supportsEventType(boolean match, Clreplaced<? extends ApplicationListener> listenerType, ResolvableType eventType) {
ApplicationListener<?> listener = mock(listenerType);
GenericApplicationListenerAdapter adapter = new GenericApplicationListenerAdapter(listener);
replacedertEquals("Wrong match for event '" + eventType + "' on " + listenerType.getClreplaced().getName(), match, adapter.supportsEventType(eventType));
}
18
Source : ApplicationContextEventTests.java
with MIT License
from Vip-Augus
with MIT License
from Vip-Augus
private void multicastEvent(boolean match, Clreplaced<?> listenerType, ApplicationEvent event, ResolvableType eventType) {
@SuppressWarnings("unchecked")
ApplicationListener<ApplicationEvent> listener = (ApplicationListener<ApplicationEvent>) mock(listenerType);
SimpleApplicationEventMulticaster smc = new SimpleApplicationEventMulticaster();
smc.addApplicationListener(listener);
if (eventType != null) {
smc.multicastEvent(event, eventType);
} else {
smc.multicastEvent(event);
}
int invocation = match ? 1 : 0;
verify(listener, times(invocation)).onApplicationEvent(event);
}
18
Source : ApplicationContextEventTests.java
with MIT License
from Vip-Augus
with MIT License
from Vip-Augus
@Test
public void simpleApplicationEventMulticasterWithException() {
@SuppressWarnings("unchecked")
ApplicationListener<ApplicationEvent> listener = mock(ApplicationListener.clreplaced);
ApplicationEvent evt = new ContextClosedEvent(new StaticApplicationContext());
SimpleApplicationEventMulticaster smc = new SimpleApplicationEventMulticaster();
smc.addApplicationListener(listener);
RuntimeException thrown = new RuntimeException();
willThrow(thrown).given(listener).onApplicationEvent(evt);
try {
smc.multicastEvent(evt);
fail("Should have thrown RuntimeException");
} catch (RuntimeException ex) {
replacedertSame(thrown, ex);
}
}
18
Source : ApplicationContextEventTests.java
with MIT License
from Vip-Augus
with MIT License
from Vip-Augus
@Test
public void simpleApplicationEventMulticasterWithTaskExecutor() {
@SuppressWarnings("unchecked")
ApplicationListener<ApplicationEvent> listener = mock(ApplicationListener.clreplaced);
ApplicationEvent evt = new ContextClosedEvent(new StaticApplicationContext());
SimpleApplicationEventMulticaster smc = new SimpleApplicationEventMulticaster();
smc.setTaskExecutor(new Executor() {
@Override
public void execute(Runnable command) {
command.run();
command.run();
}
});
smc.addApplicationListener(listener);
smc.multicastEvent(evt);
verify(listener, times(2)).onApplicationEvent(evt);
}
18
Source : ProxyFactoryBeanTests.java
with MIT License
from Vip-Augus
with MIT License
from Vip-Augus
/**
* Checks that globals get invoked,
* and that they can add aspect interfaces unavailable
* to other beans. These interfaces don't need
* to be included in proxiedInterface [].
*/
@Test
public void testGlobalsCanAddAspectInterfaces() {
AddedGlobalInterface agi = (AddedGlobalInterface) factory.getBean("autoInvoker");
replacedertTrue(agi.globalsAdded() == -1);
ProxyFactoryBean pfb = (ProxyFactoryBean) factory.getBean("&validGlobals");
// Trigger lazy initialization.
pfb.getObject();
// 2 globals + 2 explicit
replacedertEquals("Have 2 globals and 2 explicit advisors", 3, pfb.getAdvisors().length);
ApplicationListener<?> l = (ApplicationListener<?>) factory.getBean("validGlobals");
agi = (AddedGlobalInterface) l;
replacedertTrue(agi.globalsAdded() == -1);
try {
agi = (AddedGlobalInterface) factory.getBean("test1");
fail("Aspect interface should't be implemeneted without globals");
} catch (ClreplacedCastException ex) {
}
}
18
Source : GenericApplicationListenerAdapter.java
with MIT License
from Vip-Augus
with MIT License
from Vip-Augus
/**
* {@link GenericApplicationListener} adapter that determines supported event types
* through introspecting the generically declared type of the target listener.
*
* @author Juergen Hoeller
* @author Stephane Nicoll
* @since 3.0
* @see org.springframework.context.ApplicationListener#onApplicationEvent
*/
public clreplaced GenericApplicationListenerAdapter implements GenericApplicationListener, SmartApplicationListener {
private static final Map<Clreplaced<?>, ResolvableType> eventTypeCache = new ConcurrentReferenceHashMap<>();
private final ApplicationListener<ApplicationEvent> delegate;
@Nullable
private final ResolvableType declaredEventType;
/**
* Create a new GenericApplicationListener for the given delegate.
* @param delegate the delegate listener to be invoked
*/
@SuppressWarnings("unchecked")
public GenericApplicationListenerAdapter(ApplicationListener<?> delegate) {
replacedert.notNull(delegate, "Delegate listener must not be null");
this.delegate = (ApplicationListener<ApplicationEvent>) delegate;
this.declaredEventType = resolveDeclaredEventType(this.delegate);
}
@Override
public void onApplicationEvent(ApplicationEvent event) {
this.delegate.onApplicationEvent(event);
}
@Override
@SuppressWarnings("unchecked")
public boolean supportsEventType(ResolvableType eventType) {
if (this.delegate instanceof SmartApplicationListener) {
Clreplaced<? extends ApplicationEvent> eventClreplaced = (Clreplaced<? extends ApplicationEvent>) eventType.resolve();
return (eventClreplaced != null && ((SmartApplicationListener) this.delegate).supportsEventType(eventClreplaced));
} else {
return (this.declaredEventType == null || this.declaredEventType.isreplacedignableFrom(eventType));
}
}
@Override
public boolean supportsEventType(Clreplaced<? extends ApplicationEvent> eventType) {
return supportsEventType(ResolvableType.forClreplaced(eventType));
}
@Override
public boolean supportsSourceType(@Nullable Clreplaced<?> sourceType) {
return !(this.delegate instanceof SmartApplicationListener) || ((SmartApplicationListener) this.delegate).supportsSourceType(sourceType);
}
@Override
public int getOrder() {
return (this.delegate instanceof Ordered ? ((Ordered) this.delegate).getOrder() : Ordered.LOWEST_PRECEDENCE);
}
@Nullable
private static ResolvableType resolveDeclaredEventType(ApplicationListener<ApplicationEvent> listener) {
ResolvableType declaredEventType = resolveDeclaredEventType(listener.getClreplaced());
if (declaredEventType == null || declaredEventType.isreplacedignableFrom(ApplicationEvent.clreplaced)) {
Clreplaced<?> targetClreplaced = AopUtils.getTargetClreplaced(listener);
if (targetClreplaced != listener.getClreplaced()) {
declaredEventType = resolveDeclaredEventType(targetClreplaced);
}
}
return declaredEventType;
}
@Nullable
static ResolvableType resolveDeclaredEventType(Clreplaced<?> listenerType) {
ResolvableType eventType = eventTypeCache.get(listenerType);
if (eventType == null) {
eventType = ResolvableType.forClreplaced(listenerType).as(ApplicationListener.clreplaced).getGeneric();
eventTypeCache.put(listenerType, eventType);
}
return (eventType != ResolvableType.NONE ? eventType : null);
}
}
18
Source : GenericApplicationListenerAdapterTests.java
with Apache License 2.0
from SourceHot
with Apache License 2.0
from SourceHot
@SuppressWarnings("rawtypes")
private void supportsEventType(boolean match, Clreplaced<? extends ApplicationListener> listenerType, ResolvableType eventType) {
ApplicationListener<?> listener = mock(listenerType);
GenericApplicationListenerAdapter adapter = new GenericApplicationListenerAdapter(listener);
replacedertThat(adapter.supportsEventType(eventType)).as("Wrong match for event '" + eventType + "' on " + listenerType.getClreplaced().getName()).isEqualTo(match);
}
18
Source : ApplicationContextEventTests.java
with Apache License 2.0
from SourceHot
with Apache License 2.0
from SourceHot
@Test
public void simpleApplicationEventMulticasterWithException() {
@SuppressWarnings("unchecked")
ApplicationListener<ApplicationEvent> listener = mock(ApplicationListener.clreplaced);
ApplicationEvent evt = new ContextClosedEvent(new StaticApplicationContext());
SimpleApplicationEventMulticaster smc = new SimpleApplicationEventMulticaster();
smc.addApplicationListener(listener);
RuntimeException thrown = new RuntimeException();
willThrow(thrown).given(listener).onApplicationEvent(evt);
replacedertThatExceptionOfType(RuntimeException.clreplaced).isThrownBy(() -> smc.multicastEvent(evt)).satisfies(ex -> replacedertThat(ex).isSameAs(thrown));
}
18
Source : GenericApplicationListenerAdapter.java
with Apache License 2.0
from SourceHot
with Apache License 2.0
from SourceHot
/**
* {@link GenericApplicationListener} adapter that determines supported event types
* through introspecting the generically declared type of the target listener.
*
* @author Juergen Hoeller
* @author Stephane Nicoll
* @since 3.0
* @see org.springframework.context.ApplicationListener#onApplicationEvent
*/
public clreplaced GenericApplicationListenerAdapter implements GenericApplicationListener, SmartApplicationListener {
/**
* key: 监听器类型
* value: 监听器的事件描述符
*/
private static final Map<Clreplaced<?>, ResolvableType> eventTypeCache = new ConcurrentReferenceHashMap<>();
/**
* 监听器列表
*/
private final ApplicationListener<ApplicationEvent> delegate;
@Nullable
private final ResolvableType declaredEventType;
/**
* Create a new GenericApplicationListener for the given delegate.
* @param delegate the delegate listener to be invoked
*/
@SuppressWarnings("unchecked")
public GenericApplicationListenerAdapter(ApplicationListener<?> delegate) {
replacedert.notNull(delegate, "Delegate listener must not be null");
this.delegate = (ApplicationListener<ApplicationEvent>) delegate;
this.declaredEventType = resolveDeclaredEventType(this.delegate);
}
@Override
public void onApplicationEvent(ApplicationEvent event) {
this.delegate.onApplicationEvent(event);
}
@Override
@SuppressWarnings("unchecked")
public boolean supportsEventType(ResolvableType eventType) {
if (this.delegate instanceof SmartApplicationListener) {
Clreplaced<? extends ApplicationEvent> eventClreplaced = (Clreplaced<? extends ApplicationEvent>) eventType.resolve();
return (eventClreplaced != null && ((SmartApplicationListener) this.delegate).supportsEventType(eventClreplaced));
} else {
return (this.declaredEventType == null || this.declaredEventType.isreplacedignableFrom(eventType));
}
}
@Override
public boolean supportsEventType(Clreplaced<? extends ApplicationEvent> eventType) {
return supportsEventType(ResolvableType.forClreplaced(eventType));
}
@Override
public boolean supportsSourceType(@Nullable Clreplaced<?> sourceType) {
return !(this.delegate instanceof SmartApplicationListener) || ((SmartApplicationListener) this.delegate).supportsSourceType(sourceType);
}
@Override
public int getOrder() {
return (this.delegate instanceof Ordered ? ((Ordered) this.delegate).getOrder() : Ordered.LOWEST_PRECEDENCE);
}
@Nullable
private static ResolvableType resolveDeclaredEventType(ApplicationListener<ApplicationEvent> listener) {
ResolvableType declaredEventType = resolveDeclaredEventType(listener.getClreplaced());
if (declaredEventType == null || declaredEventType.isreplacedignableFrom(ApplicationEvent.clreplaced)) {
Clreplaced<?> targetClreplaced = AopUtils.getTargetClreplaced(listener);
if (targetClreplaced != listener.getClreplaced()) {
declaredEventType = resolveDeclaredEventType(targetClreplaced);
}
}
return declaredEventType;
}
@Nullable
static ResolvableType resolveDeclaredEventType(Clreplaced<?> listenerType) {
ResolvableType eventType = eventTypeCache.get(listenerType);
if (eventType == null) {
eventType = ResolvableType.forClreplaced(listenerType).as(ApplicationListener.clreplaced).getGeneric();
eventTypeCache.put(listenerType, eventType);
}
return (eventType != ResolvableType.NONE ? eventType : null);
}
}
18
Source : BeanFactoryUtils.java
with Apache License 2.0
from smallFive55
with Apache License 2.0
from smallFive55
public static boolean addApplicationListener(ApplicationContext applicationContext, ApplicationListener listener) {
try {
// backward compatibility to spring 2.0.1
Method method = applicationContext.getClreplaced().getMethod("addApplicationListener", ApplicationListener.clreplaced);
method.invoke(applicationContext, listener);
return true;
} catch (Throwable t) {
if (applicationContext instanceof AbstractApplicationContext) {
try {
// backward compatibility to spring 2.0.1
Method method = AbstractApplicationContext.clreplaced.getDeclaredMethod("addListener", ApplicationListener.clreplaced);
if (!method.isAccessible()) {
method.setAccessible(true);
}
method.invoke(applicationContext, listener);
return true;
} catch (Throwable t2) {
// ignore
}
}
}
return false;
}
18
Source : PersistentApplicationEventMulticaster.java
with Apache License 2.0
from odrotbohm
with Apache License 2.0
from odrotbohm
@SuppressWarnings({ "rawtypes", "unchecked" })
private void invokeTargetListener(EventPublication publication) {
for (ApplicationListener listener : getApplicationListeners()) {
if (publication.isIdentifiedBy(PublicationTargetIdentifier.forListener(listener))) {
executeListenerWithCompletion(publication, listener);
return;
}
}
log.debug("Listener {} not found!", publication.getTargetIdentifier());
}
18
Source : AbstractApplicationContext.java
with Apache License 2.0
from langtianya
with Apache License 2.0
from langtianya
/**
* Add beans that implement ApplicationListener as listeners.
* Doesn't affect other listeners, which can be added without being beans.
*/
protected void registerListeners() {
// Register statically specified listeners first.
for (ApplicationListener<?> listener : getApplicationListeners()) {
getApplicationEventMulticaster().addApplicationListener(listener);
}
// Do not initialize FactoryBeans here: We need to leave all regular beans
// uninitialized to let post-processors apply to them!
String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.clreplaced, true, false);
for (String listenerBeanName : listenerBeanNames) {
getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName);
}
// Publish early application events now that we finally have a multicaster...
Set<ApplicationEvent> earlyEventsToProcess = this.earlyApplicationEvents;
this.earlyApplicationEvents = null;
if (earlyEventsToProcess != null) {
for (ApplicationEvent earlyEvent : earlyEventsToProcess) {
getApplicationEventMulticaster().multicastEvent(earlyEvent);
}
}
}
18
Source : EventListenerMethodProcessor.java
with Apache License 2.0
from langtianya
with Apache License 2.0
from langtianya
protected void processBean(final List<EventListenerFactory> factories, final String beanName, final Clreplaced<?> targetType) {
if (!this.nonAnnotatedClreplacedes.contains(targetType)) {
Map<Method, EventListener> annotatedMethods = MethodIntrospector.selectMethods(targetType, new MethodIntrospector.MetadataLookup<EventListener>() {
@Override
public EventListener inspect(Method method) {
return AnnotationUtils.findAnnotation(method, EventListener.clreplaced);
}
});
if (annotatedMethods.isEmpty()) {
this.nonAnnotatedClreplacedes.add(targetType);
if (logger.isTraceEnabled()) {
logger.trace("No @EventListener annotations found on bean clreplaced: " + targetType);
}
} else {
// Non-empty set of methods
for (Method method : annotatedMethods.keySet()) {
for (EventListenerFactory factory : factories) {
if (factory.supportsMethod(method)) {
Method methodToUse = MethodIntrospector.selectInvocableMethod(method, this.applicationContext.getType(beanName));
ApplicationListener<?> applicationListener = factory.createApplicationListener(beanName, targetType, methodToUse);
if (applicationListener instanceof ApplicationListenerMethodAdapter) {
((ApplicationListenerMethodAdapter) applicationListener).init(this.applicationContext, this.evaluator);
}
this.applicationContext.addApplicationListener(applicationListener);
break;
}
}
}
if (logger.isDebugEnabled()) {
logger.debug(annotatedMethods.size() + " @EventListener methods processed on bean '" + beanName + "': " + annotatedMethods);
}
}
}
}
18
Source : DelegatingApplicationListener.java
with Apache License 2.0
from hello-shf
with Apache License 2.0
from hello-shf
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ApplicationEnvironmentPreparedEvent) {
List<ApplicationListener<ApplicationEvent>> delegates = getListeners(((ApplicationEnvironmentPreparedEvent) event).getEnvironment());
if (delegates.isEmpty()) {
return;
}
this.multicaster = new SimpleApplicationEventMulticaster();
for (ApplicationListener<ApplicationEvent> listener : delegates) {
this.multicaster.addApplicationListener(listener);
}
}
if (this.multicaster != null) {
this.multicaster.multicastEvent(event);
}
}
18
Source : SpringScenario.java
with Apache License 2.0
from gy2006
with Apache License 2.0
from gy2006
@After
public void cleanListeners() {
for (ApplicationListener listener : listenersForTest) {
applicationEventMulticaster.removeApplicationListener(listener);
}
}
18
Source : AbstractApplicationContext.java
with Apache License 2.0
from DerekYRC
with Apache License 2.0
from DerekYRC
/**
* 注册事件监听器
*/
protected void registerListeners() {
Collection<ApplicationListener> applicationListeners = getBeansOfType(ApplicationListener.clreplaced).values();
for (ApplicationListener applicationListener : applicationListeners) {
applicationEventMulticaster.addApplicationListener(applicationListener);
}
}
18
Source : ExclusiveViewResolverApplicationListenerTest.java
with GNU General Public License v3.0
from alibaba
with GNU General Public License v3.0
from alibaba
@Test(expected = NoSuchBeanDefinitionException.clreplaced)
public void testOnApplicationContextOnNoSuchBeanDefinitionException() {
// Register ViewResolver Beans
applicationContext.register(ContentNegotiatingViewResolver.clreplaced);
ApplicationListener<ContextRefreshedEvent> applicationListener = new ExclusiveViewResolverApplicationListener("not-existed-view-resolver");
applicationContext.addApplicationListener(applicationListener);
applicationContext.refresh();
}
18
Source : SafeApplicationEventMulticaster.java
with GNU Lesser General Public License v3.0
from Alfresco
with GNU Lesser General Public License v3.0
from Alfresco
/**
* Determine whether the given listener supports the given event.
* <p>
* The default implementation detects the {@link SmartApplicationListener}
* interface. In case of a standard {@link ApplicationListener}, a
* {@link GenericApplicationListenerAdapter} will be used to introspect the
* generically declared type of the target listener.
*
* @param listener
* the target listener to check
* @param eventType
* the event type to check against
* @param sourceType
* the source type to check against
* @return whether the given listener should be included in the candidates
* for the given event type
*/
protected boolean supportsEvent(ApplicationListener listener, Clreplaced<? extends ApplicationEvent> eventType, Clreplaced sourceType) {
SmartApplicationListener smartListener = (listener instanceof SmartApplicationListener ? (SmartApplicationListener) listener : new GenericApplicationListenerAdapter(listener));
return (smartListener.supportsEventType(eventType) && smartListener.supportsSourceType(sourceType));
}
17
Source : EventPublishingRunListener.java
with Apache License 2.0
from yuanmabiji
with Apache License 2.0
from yuanmabiji
// 》》》》》发射【ApplicationFailedEvent】事件
@Override
public void failed(ConfigurableApplicationContext context, Throwable exception) {
ApplicationFailedEvent event = new ApplicationFailedEvent(this.application, this.args, context, exception);
if (context != null && context.isActive()) {
// Listeners have been registered to the application context so we should
// use it at this point if we can
context.publishEvent(event);
} else {
// An inactive context may not have a multicaster so we use our multicaster to
// call all of the context's listeners instead
if (context instanceof AbstractApplicationContext) {
for (ApplicationListener<?> listener : ((AbstractApplicationContext) context).getApplicationListeners()) {
this.initialMulticaster.addApplicationListener(listener);
}
}
this.initialMulticaster.setErrorHandler(new LoggingErrorHandler());
this.initialMulticaster.multicastEvent(event);
}
}
17
Source : ApplicationContextEventTests.java
with MIT License
from Vip-Augus
with MIT License
from Vip-Augus
@Test
@SuppressWarnings("unchecked")
public void proxiedListenersMixedWithTargetListeners() {
MyOrderedListener1 listener1 = new MyOrderedListener1();
MyOrderedListener2 listener2 = new MyOrderedListener2(listener1);
ApplicationListener<ApplicationEvent> proxy1 = (ApplicationListener<ApplicationEvent>) new ProxyFactory(listener1).getProxy();
ApplicationListener<ApplicationEvent> proxy2 = (ApplicationListener<ApplicationEvent>) new ProxyFactory(listener2).getProxy();
SimpleApplicationEventMulticaster smc = new SimpleApplicationEventMulticaster();
smc.addApplicationListener(listener1);
smc.addApplicationListener(listener2);
smc.addApplicationListener(proxy1);
smc.addApplicationListener(proxy2);
smc.multicastEvent(new MyEvent(this));
smc.multicastEvent(new MyOtherEvent(this));
replacedertEquals(2, listener1.seenEvents.size());
}
17
Source : ApplicationContextEventTests.java
with MIT License
from Vip-Augus
with MIT License
from Vip-Augus
@Test
public void simpleApplicationEventMulticasterWithErrorHandler() {
@SuppressWarnings("unchecked")
ApplicationListener<ApplicationEvent> listener = mock(ApplicationListener.clreplaced);
ApplicationEvent evt = new ContextClosedEvent(new StaticApplicationContext());
SimpleApplicationEventMulticaster smc = new SimpleApplicationEventMulticaster();
smc.setErrorHandler(TaskUtils.LOG_AND_SUPPRESS_ERROR_HANDLER);
smc.addApplicationListener(listener);
willThrow(new RuntimeException()).given(listener).onApplicationEvent(evt);
smc.multicastEvent(evt);
}
17
Source : ApplicationContextEventTests.java
with MIT License
from Vip-Augus
with MIT License
from Vip-Augus
@Test
@SuppressWarnings("unchecked")
public void proxiedListeners() {
MyOrderedListener1 listener1 = new MyOrderedListener1();
MyOrderedListener2 listener2 = new MyOrderedListener2(listener1);
ApplicationListener<ApplicationEvent> proxy1 = (ApplicationListener<ApplicationEvent>) new ProxyFactory(listener1).getProxy();
ApplicationListener<ApplicationEvent> proxy2 = (ApplicationListener<ApplicationEvent>) new ProxyFactory(listener2).getProxy();
SimpleApplicationEventMulticaster smc = new SimpleApplicationEventMulticaster();
smc.addApplicationListener(proxy1);
smc.addApplicationListener(proxy2);
smc.multicastEvent(new MyEvent(this));
smc.multicastEvent(new MyOtherEvent(this));
replacedertEquals(2, listener1.seenEvents.size());
}
17
Source : SimpleApplicationEventMulticaster.java
with MIT License
from Vip-Augus
with MIT License
from Vip-Augus
@Override
public void multicastEvent(final ApplicationEvent event, @Nullable ResolvableType eventType) {
ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event));
Executor executor = getTaskExecutor();
// 遍历注册的消息监听器
for (ApplicationListener<?> listener : getApplicationListeners(event, type)) {
if (executor != null) {
executor.execute(() -> invokeListener(listener, event));
} else {
invokeListener(listener, event);
}
}
}
17
Source : SimpleApplicationEventMulticaster.java
with MIT License
from Vip-Augus
with MIT License
from Vip-Augus
@SuppressWarnings({ "unchecked", "rawtypes" })
private void doInvokeListener(ApplicationListener listener, ApplicationEvent event) {
try {
listener.onApplicationEvent(event);
} catch (ClreplacedCastException ex) {
String msg = ex.getMessage();
if (msg == null || matchesClreplacedCastMessage(msg, event.getClreplaced())) {
// Possibly a lambda-defined listener which we could not resolve the generic event type for
// -> let's suppress the exception and just log a debug message.
Log logger = LogFactory.getLog(getClreplaced());
if (logger.isTraceEnabled()) {
logger.trace("Non-matching event type for listener: " + listener, ex);
}
} else {
throw ex;
}
}
}
17
Source : EventNetstrapSpringRunListener.java
with Apache License 2.0
from tryob
with Apache License 2.0
from tryob
@Override
public void failed(ConfigurableApplicationContext context, Throwable exception) {
FailedApplicationEvent event = new FailedApplicationEvent(this.application, context, exception);
if (context != null && context.isActive()) {
context.publishEvent(event);
} else {
if (context instanceof AbstractApplicationContext) {
for (ApplicationListener<?> listener : ((AbstractApplicationContext) context).getApplicationListeners()) {
this.initialMulticaster.addApplicationListener(listener);
}
}
this.initialMulticaster.setErrorHandler(new LoggingErrorHandler());
this.initialMulticaster.multicastEvent(event);
}
}
17
Source : ApplicationContextEventTests.java
with Apache License 2.0
from SourceHot
with Apache License 2.0
from SourceHot
@Test
@SuppressWarnings("unchecked")
public void proxiedListeners() {
MyOrderedListener1 listener1 = new MyOrderedListener1();
MyOrderedListener2 listener2 = new MyOrderedListener2(listener1);
ApplicationListener<ApplicationEvent> proxy1 = (ApplicationListener<ApplicationEvent>) new ProxyFactory(listener1).getProxy();
ApplicationListener<ApplicationEvent> proxy2 = (ApplicationListener<ApplicationEvent>) new ProxyFactory(listener2).getProxy();
SimpleApplicationEventMulticaster smc = new SimpleApplicationEventMulticaster();
smc.addApplicationListener(proxy1);
smc.addApplicationListener(proxy2);
smc.multicastEvent(new MyEvent(this));
smc.multicastEvent(new MyOtherEvent(this));
replacedertThat(listener1.seenEvents.size()).isEqualTo(2);
}
17
Source : ApplicationContextEventTests.java
with Apache License 2.0
from SourceHot
with Apache License 2.0
from SourceHot
@Test
@SuppressWarnings("unchecked")
public void proxiedListenersMixedWithTargetListeners() {
MyOrderedListener1 listener1 = new MyOrderedListener1();
MyOrderedListener2 listener2 = new MyOrderedListener2(listener1);
ApplicationListener<ApplicationEvent> proxy1 = (ApplicationListener<ApplicationEvent>) new ProxyFactory(listener1).getProxy();
ApplicationListener<ApplicationEvent> proxy2 = (ApplicationListener<ApplicationEvent>) new ProxyFactory(listener2).getProxy();
SimpleApplicationEventMulticaster smc = new SimpleApplicationEventMulticaster();
smc.addApplicationListener(listener1);
smc.addApplicationListener(listener2);
smc.addApplicationListener(proxy1);
smc.addApplicationListener(proxy2);
smc.multicastEvent(new MyEvent(this));
smc.multicastEvent(new MyOtherEvent(this));
replacedertThat(listener1.seenEvents.size()).isEqualTo(2);
}
17
Source : ProxyFactoryBeanTests.java
with Apache License 2.0
from SourceHot
with Apache License 2.0
from SourceHot
/**
* Checks that globals get invoked,
* and that they can add aspect interfaces unavailable
* to other beans. These interfaces don't need
* to be included in proxiedInterface [].
*/
@Test
public void testGlobalsCanAddAspectInterfaces() {
AddedGlobalInterface agi = (AddedGlobalInterface) factory.getBean("autoInvoker");
replacedertThat(agi.globalsAdded() == -1).isTrue();
ProxyFactoryBean pfb = (ProxyFactoryBean) factory.getBean("&validGlobals");
// Trigger lazy initialization.
pfb.getObject();
// 2 globals + 2 explicit
replacedertThat(pfb.getAdvisors().length).as("Have 2 globals and 2 explicit advisors").isEqualTo(3);
ApplicationListener<?> l = (ApplicationListener<?>) factory.getBean("validGlobals");
agi = (AddedGlobalInterface) l;
replacedertThat(agi.globalsAdded() == -1).isTrue();
replacedertThat(factory.getBean("test1")).as("Aspect interface should't be implemeneted without globals").isNotInstanceOf(AddedGlobalInterface.clreplaced);
}
17
Source : SimpleApplicationEventMulticaster.java
with Apache License 2.0
from SourceHot
with Apache License 2.0
from SourceHot
@Override
public void multicastEvent(final ApplicationEvent event, @Nullable ResolvableType eventType) {
ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event));
Executor executor = getTaskExecutor();
for (ApplicationListener<?> listener : getApplicationListeners(event, type)) {
if (executor != null) {
executor.execute(() -> invokeListener(listener, event));
} else {
invokeListener(listener, event);
}
}
}
17
Source : SimpleApplicationEventMulticaster.java
with Apache License 2.0
from SourceHot
with Apache License 2.0
from SourceHot
@SuppressWarnings({ "rawtypes", "unchecked" })
private void doInvokeListener(ApplicationListener listener, ApplicationEvent event) {
try {
listener.onApplicationEvent(event);
} catch (ClreplacedCastException ex) {
String msg = ex.getMessage();
if (msg == null || matchesClreplacedCastMessage(msg, event.getClreplaced())) {
// Possibly a lambda-defined listener which we could not resolve the generic event type for
// -> let's suppress the exception and just log a debug message.
Log logger = LogFactory.getLog(getClreplaced());
if (logger.isTraceEnabled()) {
logger.trace("Non-matching event type for listener: " + listener, ex);
}
} else {
throw ex;
}
}
}
17
Source : AbstractApplicationEventMulticaster.java
with Apache License 2.0
from SourceHot
with Apache License 2.0
from SourceHot
/**
* Actually retrieve the application listeners for the given event and source type.
*
* 搜索应用上下文监听器
* @param eventType the event type
* @param sourceType the event source type
* @param retriever the ListenerRetriever, if supposed to populate one (for caching purposes)
* @return the pre-filtered list of application listeners for the given event and source type
*/
private Collection<ApplicationListener<?>> retrieveApplicationListeners(ResolvableType eventType, @Nullable Clreplaced<?> sourceType, @Nullable ListenerRetriever retriever) {
// 返回值列表
List<ApplicationListener<?>> allListeners = new ArrayList<>();
Set<ApplicationListener<?>> listeners;
Set<String> listenerBeans;
synchronized (this.retrievalMutex) {
// 应用监听器实例列表
listeners = new LinkedHashSet<>(this.defaultRetriever.applicationListeners);
// 应用监听器名称列表
listenerBeans = new LinkedHashSet<>(this.defaultRetriever.applicationListenerBeans);
}
// Add programmatically registered listeners, including ones coming
// from ApplicationListenerDetector (singleton beans and inner beans).
for (ApplicationListener<?> listener : listeners) {
// 判断当前监听器是否支持传入的事件
if (supportsEvent(listener, eventType, sourceType)) {
if (retriever != null) {
// 帮助容器中添加数据
retriever.applicationListeners.add(listener);
}
// 放入返回对象中
allListeners.add(listener);
}
}
// Add listeners by bean name, potentially overlapping with programmatically
// registered listeners above - but here potentially with additional metadata.
if (!listenerBeans.isEmpty()) {
ConfigurableBeanFactory beanFactory = getBeanFactory();
// applicationListener name 处理
for (String listenerBeanName : listenerBeans) {
try {
// 事件监听器 是否支持 当前的事件
// 支持的处理
if (supportsEvent(beanFactory, listenerBeanName, eventType)) {
// 获取 bean
ApplicationListener<?> listener = beanFactory.getBean(listenerBeanName, ApplicationListener.clreplaced);
if (!allListeners.contains(listener) && supportsEvent(listener, eventType, sourceType)) {
// 帮助容器中添加数据
if (retriever != null) {
if (beanFactory.isSingleton(listenerBeanName)) {
retriever.applicationListeners.add(listener);
} else {
retriever.applicationListenerBeans.add(listenerBeanName);
}
}
// 放入返回对象中
allListeners.add(listener);
}
} else // 不支持的处理
{
// Remove non-matching listeners that originally came from
// ApplicationListenerDetector, possibly ruled out by additional
// BeanDefinition metadata (e.g. factory method generics) above.
Object listener = beanFactory.getSingleton(listenerBeanName);
// 将 ApplicationListener 移除容器
if (retriever != null) {
retriever.applicationListeners.remove(listener);
}
allListeners.remove(listener);
}
} catch (NoSuchBeanDefinitionException ex) {
// Singleton listener instance (without backing bean definition) disappeared -
// probably in the middle of the destruction phase
}
}
}
// 排序后放入容器
AnnotationAwareOrderComparator.sort(allListeners);
if (retriever != null && retriever.applicationListenerBeans.isEmpty()) {
retriever.applicationListeners.clear();
retriever.applicationListeners.addAll(allListeners);
}
return allListeners;
}
17
Source : SpringExtensionFactory.java
with Apache License 2.0
from smallFive55
with Apache License 2.0
from smallFive55
/**
* SpringExtensionFactory
*/
public clreplaced SpringExtensionFactory implements ExtensionFactory {
private static final Logger logger = LoggerFactory.getLogger(SpringExtensionFactory.clreplaced);
private static final Set<ApplicationContext> contexts = new ConcurrentHashSet<ApplicationContext>();
private static final ApplicationListener shutdownHookListener = new ShutdownHookListener();
public static void addApplicationContext(ApplicationContext context) {
contexts.add(context);
BeanFactoryUtils.addApplicationListener(context, shutdownHookListener);
}
public static void removeApplicationContext(ApplicationContext context) {
contexts.remove(context);
}
public static Set<ApplicationContext> getContexts() {
return contexts;
}
// currently for test purpose
public static void clearContexts() {
contexts.clear();
}
@Override
@SuppressWarnings("unchecked")
public <T> T getExtension(Clreplaced<T> type, String name) {
for (ApplicationContext context : contexts) {
if (context.containsBean(name)) {
Object bean = context.getBean(name);
if (type.isInstance(bean)) {
return (T) bean;
}
}
}
logger.warn("No spring extension (bean) named:" + name + ", try to find an extension (bean) of type " + type.getName());
if (Object.clreplaced == type) {
return null;
}
for (ApplicationContext context : contexts) {
try {
return context.getBean(type);
} catch (NoUniqueBeanDefinitionException multiBeanExe) {
logger.warn("Find more than 1 spring extensions (beans) of type " + type.getName() + ", will stop auto injection. Please make sure you have specified the concrete parameter type and there's only one extension of that type.");
} catch (NoSuchBeanDefinitionException noBeanExe) {
if (logger.isDebugEnabled()) {
logger.debug("Error when get spring extension(bean) for type:" + type.getName(), noBeanExe);
}
}
}
logger.warn("No spring extension (bean) named:" + name + ", type:" + type.getName() + " found, stop get bean.");
return null;
}
private static clreplaced ShutdownHookListener implements ApplicationListener {
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ContextClosedEvent) {
// we call it anyway since dubbo shutdown hook make sure its destroyAll() is re-entrant.
// pls. note we should not remove dubbo shutdown hook when spring framework is present, this is because
// its shutdown hook may not be installed.
DubboShutdownHook shutdownHook = DubboShutdownHook.getDubboShutdownHook();
shutdownHook.destroyAll();
}
}
}
}
17
Source : PersistentApplicationEventMulticaster.java
with Apache License 2.0
from odrotbohm
with Apache License 2.0
from odrotbohm
/*
* (non-Javadoc)
* @see org.springframework.context.event.ApplicationEventMulticaster#multicastEvent(org.springframework.context.ApplicationEvent, org.springframework.core.ResolvableType)
*/
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public void multicastEvent(ApplicationEvent event, ResolvableType eventType) {
ResolvableType type = eventType == null ? ResolvableType.forInstance(event) : eventType;
Collection<ApplicationListener<?>> listeners = getApplicationListeners(event, type);
if (listeners.isEmpty()) {
return;
}
TransactionalEventListeners txListeners = new TransactionalEventListeners(listeners);
Object eventToPersist = getEventToPersist(event);
registry.get().store(eventToPersist, //
txListeners.stream().map(//
TransactionalApplicationListener::getListenerId).map(PublicationTargetIdentifier::of));
for (ApplicationListener listener : listeners) {
listener.onApplicationEvent(event);
}
}
17
Source : SimpleApplicationEventMulticaster.java
with MIT License
from mindcarver
with MIT License
from mindcarver
@Override
public void multicastEvent(final ApplicationEvent event, @Nullable ResolvableType eventType) {
ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event));
for (final ApplicationListener<?> listener : getApplicationListeners(event, type)) {
Executor executor = getTaskExecutor();
if (executor != null) {
executor.execute(() -> invokeListener(listener, event));
} else {
invokeListener(listener, event);
}
}
}
17
Source : SimpleApplicationEventMulticaster.java
with MIT License
from mindcarver
with MIT License
from mindcarver
@SuppressWarnings({ "unchecked", "rawtypes" })
private void doInvokeListener(ApplicationListener listener, ApplicationEvent event) {
try {
listener.onApplicationEvent(event);
} catch (ClreplacedCastException ex) {
String msg = ex.getMessage();
if (msg == null || matchesClreplacedCastMessage(msg, event.getClreplaced())) {
// Possibly a lambda-defined listener which we could not resolve the generic event type for
// -> let's suppress the exception and just log a debug message.
Log logger = LogFactory.getLog(getClreplaced());
if (logger.isDebugEnabled()) {
logger.debug("Non-matching event type for listener: " + listener, ex);
}
} else {
throw ex;
}
}
}
17
Source : RscApplication.java
with Apache License 2.0
from making
with Apache License 2.0
from making
public static void main(String[] args) {
final SpringApplication application = new SpringApplication(RscApplication.clreplaced);
for (ApplicationListener<?> listener : application.getListeners()) {
if (listener instanceof LoggingApplicationListener) {
((LoggingApplicationListener) listener).setParseArgs(false);
}
}
application.run(args);
}
17
Source : ApplicationContextEventTests.java
with Apache License 2.0
from langtianya
with Apache License 2.0
from langtianya
@Test
@SuppressWarnings("unchecked")
public void proxiedListeners() {
MyOrderedListener1 listener1 = new MyOrderedListener1();
MyOrderedListener2 listener2 = new MyOrderedListener2(listener1);
ApplicationListener<ApplicationEvent> proxy1 = (ApplicationListener<ApplicationEvent>) new ProxyFactory(listener1).getProxy();
ApplicationListener<ApplicationEvent> proxy2 = (ApplicationListener<ApplicationEvent>) new ProxyFactory(listener2).getProxy();
SimpleApplicationEventMulticaster smc = new SimpleApplicationEventMulticaster();
smc.addApplicationListener(proxy1);
smc.addApplicationListener(proxy2);
smc.multicastEvent(new MyEvent(this));
smc.multicastEvent(new MyOtherEvent(this));
}
17
Source : EventPublishingRunListener.java
with Apache License 2.0
from hello-shf
with Apache License 2.0
from hello-shf
@Override
public void failed(ConfigurableApplicationContext context, Throwable exception) {
ApplicationFailedEvent event = new ApplicationFailedEvent(this.application, this.args, context, exception);
if (context != null && context.isActive()) {
// Listeners have been registered to the application context so we should
// use it at this point if we can
context.publishEvent(event);
} else {
// An inactive context may not have a multicaster so we use our multicaster to
// call all of the context's listeners instead
if (context instanceof AbstractApplicationContext) {
for (ApplicationListener<?> listener : ((AbstractApplicationContext) context).getApplicationListeners()) {
this.initialMulticaster.addApplicationListener(listener);
}
}
this.initialMulticaster.setErrorHandler(new LoggingErrorHandler());
this.initialMulticaster.multicastEvent(event);
}
}
17
Source : SpringScenario.java
with Apache License 2.0
from FlowCI
with Apache License 2.0
from FlowCI
@After
public void cleanListeners() {
for (ApplicationListener<?> listener : listenersForTest) {
applicationEventMulticaster.removeApplicationListener(listener);
}
}
17
Source : SpringExtensionFactory.java
with Apache License 2.0
from boomblog
with Apache License 2.0
from boomblog
/**
* SpringExtensionFactory
*/
public clreplaced SpringExtensionFactory implements ExtensionFactory {
private static final Logger logger = LoggerFactory.getLogger(SpringExtensionFactory.clreplaced);
private static final Set<ApplicationContext> contexts = new ConcurrentHashSet<ApplicationContext>();
private static final ApplicationListener shutdownHookListener = new ShutdownHookListener();
public static void addApplicationContext(ApplicationContext context) {
contexts.add(context);
if (context instanceof ConfigurableApplicationContext) {
((ConfigurableApplicationContext) context).registerShutdownHook();
DubboShutdownHook.getDubboShutdownHook().unregister();
}
BeanFactoryUtils.addApplicationListener(context, shutdownHookListener);
}
public static void removeApplicationContext(ApplicationContext context) {
contexts.remove(context);
}
public static Set<ApplicationContext> getContexts() {
return contexts;
}
// currently for test purpose
public static void clearContexts() {
contexts.clear();
}
@Override
@SuppressWarnings("unchecked")
public <T> T getExtension(Clreplaced<T> type, String name) {
// SPI should be get from SpiExtensionFactory
if (type.isInterface() && type.isAnnotationPresent(SPI.clreplaced)) {
return null;
}
for (ApplicationContext context : contexts) {
if (context.containsBean(name)) {
Object bean = context.getBean(name);
if (type.isInstance(bean)) {
return (T) bean;
}
}
}
logger.warn("No spring extension (bean) named:" + name + ", try to find an extension (bean) of type " + type.getName());
if (Object.clreplaced == type) {
return null;
}
for (ApplicationContext context : contexts) {
try {
return context.getBean(type);
} catch (NoUniqueBeanDefinitionException multiBeanExe) {
logger.warn("Find more than 1 spring extensions (beans) of type " + type.getName() + ", will stop auto injection. Please make sure you have specified the concrete parameter type and there's only one extension of that type.");
} catch (NoSuchBeanDefinitionException noBeanExe) {
if (logger.isDebugEnabled()) {
logger.debug("Error when get spring extension(bean) for type:" + type.getName(), noBeanExe);
}
}
}
logger.warn("No spring extension (bean) named:" + name + ", type:" + type.getName() + " found, stop get bean.");
return null;
}
private static clreplaced ShutdownHookListener implements ApplicationListener {
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ContextClosedEvent) {
DubboShutdownHook shutdownHook = DubboShutdownHook.getDubboShutdownHook();
shutdownHook.doDestroy();
}
}
}
}
See More Examples