Search in sources :

Example 6 with PayloadApplicationEvent

use of org.springframework.context.PayloadApplicationEvent in project spring-framework by spring-projects.

the class AbstractApplicationContext method publishEvent.

/**
	 * Publish the given event to all listeners.
	 * @param event the event to publish (may be an {@link ApplicationEvent}
	 * or a payload object to be turned into a {@link PayloadApplicationEvent})
	 * @param eventType the resolved event type, if known
	 * @since 4.2
	 */
protected void publishEvent(Object event, ResolvableType eventType) {
    Assert.notNull(event, "Event must not be null");
    if (logger.isTraceEnabled()) {
        logger.trace("Publishing event in " + getDisplayName() + ": " + event);
    }
    // Decorate event as an ApplicationEvent if necessary
    ApplicationEvent applicationEvent;
    if (event instanceof ApplicationEvent) {
        applicationEvent = (ApplicationEvent) event;
    } else {
        applicationEvent = new PayloadApplicationEvent<>(this, event);
        if (eventType == null) {
            eventType = ((PayloadApplicationEvent) applicationEvent).getResolvableType();
        }
    }
    // Multicast right now if possible - or lazily once the multicaster is initialized
    if (this.earlyApplicationEvents != null) {
        this.earlyApplicationEvents.add(applicationEvent);
    } else {
        getApplicationEventMulticaster().multicastEvent(applicationEvent, eventType);
    }
    // Publish event via parent context as well...
    if (this.parent != null) {
        if (this.parent instanceof AbstractApplicationContext) {
            ((AbstractApplicationContext) this.parent).publishEvent(event, eventType);
        } else {
            this.parent.publishEvent(event);
        }
    }
}
Also used : PayloadApplicationEvent(org.springframework.context.PayloadApplicationEvent) ApplicationEvent(org.springframework.context.ApplicationEvent)

Example 7 with PayloadApplicationEvent

use of org.springframework.context.PayloadApplicationEvent in project spring-framework by spring-projects.

the class ApplicationListenerMethodAdapter method getResolvableType.

private ResolvableType getResolvableType(ApplicationEvent event) {
    ResolvableType payloadType = null;
    if (event instanceof PayloadApplicationEvent) {
        PayloadApplicationEvent<?> payloadEvent = (PayloadApplicationEvent<?>) event;
        payloadType = payloadEvent.getResolvableType().as(PayloadApplicationEvent.class).getGeneric();
    }
    for (ResolvableType declaredEventType : this.declaredEventTypes) {
        if (!ApplicationEvent.class.isAssignableFrom(declaredEventType.getRawClass()) && payloadType != null) {
            if (declaredEventType.isAssignableFrom(payloadType)) {
                return declaredEventType;
            }
        }
        if (declaredEventType.getRawClass().isInstance(event)) {
            return declaredEventType;
        }
    }
    return null;
}
Also used : PayloadApplicationEvent(org.springframework.context.PayloadApplicationEvent) ResolvableType(org.springframework.core.ResolvableType)

Aggregations

PayloadApplicationEvent (org.springframework.context.PayloadApplicationEvent)7 Method (java.lang.reflect.Method)5 Test (org.junit.Test)5 ApplicationEvent (org.springframework.context.ApplicationEvent)1 ResolvableType (org.springframework.core.ResolvableType)1