Example usage for org.springframework.context ApplicationListener onApplicationEvent

List of usage examples for org.springframework.context ApplicationListener onApplicationEvent

Introduction

In this page you can find the example usage for org.springframework.context ApplicationListener onApplicationEvent.

Prototype

void onApplicationEvent(E event);

Source Link

Document

Handle an application event.

Usage

From source file:br.com.valecard.context.event.ChoiceTaskExecutorApplicationEventMulticaster.java

@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public void multicastEvent(final ApplicationEvent event) {
    Executor executor = getTaskExecutor();
    if (event instanceof RepositoryEvent) {
        executor = new SyncTaskExecutor();
    }//from w  w w .  j  ava 2s  .  c  o  m
    for (final ApplicationListener listener : getApplicationListeners(event)) {
        if (executor != null) {
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    listener.onApplicationEvent(event);
                }
            });
        } else {
            listener.onApplicationEvent(event);
        }
    }

}

From source file:org.trpr.platform.core.impl.event.PlatformEventMulticaster.java

/**
 * Interface method implementation. Forwards the published event to all event consumers whose subscriptions match the endpointURI
 * contained in the specified PlatformApplicationEvent.
 * Note that Spring ApplicationEvent instances that are not of type PlatformApplicationEvent are ignored and a warning message is logged.
 * @see org.springframework.context.event.ApplicationEventMulticaster#multicastEvent(org.springframework.context.ApplicationEvent)
 *//*from  w  ww.  ja v a 2 s .  c  o m*/
public void multicastEvent(ApplicationEvent event) {
    if (event instanceof PlatformApplicationEvent) {
        PlatformApplicationEvent platformApplicationEvent = (PlatformApplicationEvent) event;
        String eventEndpointURI = platformApplicationEvent.getEndpointURI();
        if (eventEndpointURI == null) {
            LOGGER.warn(
                    "End-point URI of PlatformApplicationEvent is null. Event will not be forwarded. Event type is : "
                            + platformApplicationEvent.getClass().getName());
            return;
        }
        if (!isSubscriptionMatch(eventEndpointURI, subscriptions)) {
            LOGGER.warn(
                    "Endpoint URI doesnot match any of the subscriptions specified on this multi-caster. Event will not be forwarded. Event URI is : "
                            + eventEndpointURI);
            return;
        }
        for (Iterator iterator = getApplicationListeners().iterator(); iterator.hasNext();) {
            ApplicationListener listener = (ApplicationListener) iterator.next();
            if (listener instanceof EndpointEventConsumer) {
                if (isSubscriptionMatch(eventEndpointURI,
                        ((EndpointEventConsumer) listener).getSubscriptions())) {
                    listener.onApplicationEvent(event);
                }
            }
        }
    } else {
        // log a warning and ignore the event
        LOGGER.warn("Spring ApplicationEvent of un-supported type received : " + event.getClass().getName()
                + ". Only PlatformApplicationEvent instances with be forwarded.");
    }
}

From source file:org.trpr.platform.servicefw.impl.event.PlatformEventMulticaster.java

/**
 * Interface method implementation. Forwards the published event to all event consumers whose subscriptions match the endpointURI
 * contained in the specified PlatformApplicationEvent.
 * Note that Spring ApplicationEvent instances that are not of type PlatformApplicationEvent are ignored and a warning message is logged.
 * @see org.springframework.context.event.ApplicationEventMulticaster#multicastEvent(org.springframework.context.ApplicationEvent)
 *///from  w w  w .j a  v a  2s  .  c  om
@SuppressWarnings({ "rawtypes", "unchecked" })
public void multicastEvent(ApplicationEvent event) {
    if (event instanceof PlatformApplicationEvent) {
        PlatformApplicationEvent platformApplicationEvent = (PlatformApplicationEvent) event;
        String eventEndpointURI = platformApplicationEvent.getEndpointURI();
        if (eventEndpointURI == null) {
            LOGGER.warn(
                    "End-point URI of PlatformApplicationEvent is null. Event will not be forwarded. Event type is : "
                            + platformApplicationEvent.getClass().getName());
            return;
        }
        if (!isSubscriptionMatch(eventEndpointURI, subscriptions)) {
            LOGGER.warn(
                    "Endpoint URI doesnot match any of the subscriptions specified on this multi-caster. Event will not be forwarded. Event URI is : "
                            + eventEndpointURI);
            return;
        }
        for (Iterator<ApplicationListener> iterator = getApplicationListeners().iterator(); iterator
                .hasNext();) {
            ApplicationListener listener = (ApplicationListener) iterator.next();
            if (listener instanceof ServiceEventConsumer) {
                if (isSubscriptionMatch(eventEndpointURI,
                        ((ServiceEventConsumer) listener).getSubscriptions())) {
                    listener.onApplicationEvent(event);
                }
            }
        }
    } else {
        // log a warning and ignore the event
        LOGGER.warn("Spring ApplicationEvent of un-supported type received : " + event.getClass().getName()
                + ". Only PlatformApplicationEvent instances with be forwarded.");
    }
}

From source file:org.alfresco.repo.management.SafeApplicationEventMulticaster.java

@SuppressWarnings("unchecked")
protected void multicastEventInternal(final ApplicationEvent event) {
    for (final ApplicationListener listener : getApplicationListeners(event)) {
        Executor executor = getTaskExecutor();
        if (executor != null) {
            executor.execute(new Runnable() {
                public void run() {
                    listener.onApplicationEvent(event);
                }//from  w ww. j  a va  2s.  com
            });
        } else {
            listener.onApplicationEvent(event);
        }
    }
}

From source file:org.alfresco.repo.search.impl.lucene.index.IndexInfo.java

private void notifyListeners(String description, int count) {
    if (!this.applicationListeners.isEmpty()) {
        IndexEvent event = new IndexEvent(this, description, count);
        for (ApplicationListener listener : this.applicationListeners) {
            listener.onApplicationEvent(event);
        }/*from   ww w .j a  va  2s.c om*/
    }
}

From source file:org.mule.module.spring.events.MuleEventMulticaster.java

/**
 * Method is used to dispatch events to listeners registered with the
 * EventManager or dispatches events to Mule depending on the type and state of
 * the event received. If the event is not a Mule event it will be dispatched to
 * any listeners registered that are NOT MuleEventListeners. If the event is a
 * Mule event and there is no source event attached to it, it is assumed that the
 * event was dispatched by an object in the context using context.publishEvent()
 * and will be dispatched by Mule. If the event does have a source event attached
 * to it, it is assumed that the event was dispatched by Mule and will be
 * delivered to any listeners subscribed to the event.
 *
 * @param e the application event received by the context
 *//*from  w w w.  j ava 2s  . c o m*/
public void multicastEvent(ApplicationEvent e) {
    MuleApplicationEvent muleEvent = null;
    // if the context gets refreshed we need to reinitialise
    if (e instanceof ContextRefreshedEvent) {
        try {
            registerMulticasterComponent();
        } catch (MuleException ex) {
            throw new MuleRuntimeException(SpringMessages.failedToReinitMule(), ex);
        }
    } else if (e instanceof ContextClosedEvent) {
        if (!muleContext.isDisposing() && !muleContext.isDisposed()) {
            muleContext.dispose();
        }
        return;
    } else if (e instanceof MuleApplicationEvent) {
        muleEvent = (MuleApplicationEvent) e;
        // If there is no Mule event the event didn't originate from Mule
        // so its an outbound event
        if (muleEvent.getMuleEventContext() == null) {
            try {
                dispatchEvent(muleEvent);
            } catch (ApplicationEventException e1) {
                exceptionListener.exceptionThrown(e1);
            }
            return;
        }
    }

    for (Iterator iterator = listeners.iterator(); iterator.hasNext();) {
        ApplicationListener listener = (ApplicationListener) iterator.next();
        if (muleEvent != null) {
            // As the asynchronous listener wraps the real listener we need
            // to check the type of the wrapped listener, but invoke the Async
            // listener
            if (listener instanceof AsynchronousEventListener) {
                AsynchronousEventListener asyncListener = (AsynchronousEventListener) listener;
                if (asyncListener.getListener() instanceof MuleSubscriptionEventListener) {
                    if (isSubscriptionMatch(muleEvent.getEndpoint(),
                            ((MuleSubscriptionEventListener) asyncListener.getListener()).getSubscriptions())) {
                        asyncListener.onApplicationEvent(muleEvent);
                    }
                } else if (asyncListener.getListener() instanceof MuleEventListener) {
                    asyncListener.onApplicationEvent(muleEvent);
                } else if (!(asyncListener.getListener() instanceof MuleEventListener)) {
                    asyncListener.onApplicationEvent(e);
                }
                // Synchronous MuleEvent listener Checks
            } else if (listener instanceof MuleSubscriptionEventListener) {
                if (isSubscriptionMatch(muleEvent.getEndpoint(),
                        ((MuleSubscriptionEventListener) listener).getSubscriptions())) {
                    listener.onApplicationEvent(muleEvent);
                }
            } else if (listener instanceof MuleEventListener) {
                listener.onApplicationEvent(muleEvent);
            }
        } else if (listener instanceof AsynchronousEventListener
                && !(((AsynchronousEventListener) listener).getListener() instanceof MuleEventListener)) {
            listener.onApplicationEvent(e);
        } else if (!(listener instanceof MuleEventListener)) {
            listener.onApplicationEvent(e);
        } else {
            // Finally only propagate the Application event if the
            // ApplicationEvent interface is explicitly implemented
            for (int i = 0; i < listener.getClass().getInterfaces().length; i++) {
                if (listener.getClass().getInterfaces()[i].equals(ApplicationListener.class)) {
                    listener.onApplicationEvent(e);
                    break;
                }
            }

        }
    }
}

From source file:org.springframework.context.event.SimpleApplicationEventMulticaster.java

@SuppressWarnings({ "unchecked", "rawtypes" })
private void doInvokeListener(ApplicationListener listener, ApplicationEvent event) {
    try {// w w  w. j  a  v  a2  s. com
        listener.onApplicationEvent(event);
    } catch (ClassCastException ex) {
        String msg = ex.getMessage();
        if (msg == null || matchesClassCastMessage(msg, event.getClass().getName())) {
            // 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(getClass());
            if (logger.isDebugEnabled()) {
                logger.debug("Non-matching event type for listener: " + listener, ex);
            }
        } else {
            throw ex;
        }
    }
}

From source file:org.springframework.data.gemfire.support.SpringContextBootstrappingInitializer.java

/**
 * Notifies any Spring ApplicationListeners of a current and existing ContextRefreshedEvent if the
 * ApplicationContext had been previously created, initialized and refreshed before any ApplicationListeners
 * interested in ContextRefreshedEvents were registered so that application components (such as the
 * GemFire CacheLoaders extending LazyWiringDeclarableSupport objects) registered late, requiring configuration
 * (auto-wiring), also get notified and wired accordingly.
 *
 * @param listener a Spring ApplicationListener requiring notification of any ContextRefreshedEvents after the
 * ApplicationContext has already been created, initialized and/or refreshed.
 * @see org.springframework.context.ApplicationListener#onApplicationEvent(org.springframework.context.ApplicationEvent)
 * @see org.springframework.context.event.ContextRefreshedEvent
 *//*from   w  w  w.ja  va2 s.  c o  m*/
protected static void notifyOnExistingContextRefreshedEvent(
        ApplicationListener<ContextRefreshedEvent> listener) {
    synchronized (applicationEventNotifier) {
        if (contextRefreshedEvent != null) {
            listener.onApplicationEvent(contextRefreshedEvent);
        }
    }
}

From source file:org.springframework.integration.config.annotation.MessagingAnnotationPostProcessor.java

public void onApplicationEvent(ApplicationEvent event) {
    for (ApplicationListener<ApplicationEvent> listener : listeners) {
        try {//from   w  w  w.j  a  v a2s . c  o m
            listener.onApplicationEvent(event);
        } catch (ClassCastException e) {
            if (logger.isWarnEnabled() && event != null) {
                logger.warn("ApplicationEvent of type [" + event.getClass()
                        + "] not accepted by ApplicationListener [" + listener + "]");
            }
        }
    }
}

From source file:org.springframework.statemachine.processor.StateMachineAnnotationPostProcessor.java

@Override
public void onApplicationEvent(ApplicationEvent event) {
    for (ApplicationListener<ApplicationEvent> listener : listeners) {
        try {/*from w w w .j  a v  a2s  . c  o m*/
            listener.onApplicationEvent(event);
        } catch (ClassCastException e) {
            if (log.isWarnEnabled() && event != null) {
                log.warn("ApplicationEvent of type [" + event.getClass()
                        + "] not accepted by ApplicationListener [" + listener + "]");
            }
        }
    }
}