Spring Framework Event Handling: Difference between revisions
Jump to navigation
Jump to search
(8 intermediate revisions by the same user not shown) | |||
Line 6: | Line 6: | ||
* [[Spring_Framework#Spring_Framework_Core_Technologies_Concepts|Spring Framework]] | * [[Spring_Framework#Spring_Framework_Core_Technologies_Concepts|Spring Framework]] | ||
* [[Spring_Dependency_Injection_and_Inversion_of_Control_Container_Concepts#Application_Listeners|Spring Dependency Injection and Inversion of Control Container Concepts]] | |||
=TO PROCESS= | |||
* https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-spring-application.html#boot-features-application-events-and-listeners | |||
=Overview= | =Overview= | ||
Line 18: | Line 23: | ||
Events are instances extending [https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/ApplicationEvent.html ApplicationEvent]. | Events are instances extending [https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/ApplicationEvent.html ApplicationEvent]. | ||
==Standard Events== | |||
* ContextStartedEvent | |||
* ContextStoppedEvent | |||
==Custom Event== | ==Custom Event== | ||
Line 24: | Line 34: | ||
Events can be published using publishers. Any custom publisher should inject an [https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/ApplicationEventPublisher.html ApplicationEventPublisher] object. | Events can be published using publishers. Any custom publisher should inject an [https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/ApplicationEventPublisher.html ApplicationEventPublisher] object. | ||
<syntaxhighlight lang='java'> | |||
package org.springframework.context; | |||
@FunctionalInterface | |||
public interface ApplicationEventPublisher { | |||
default void publishEvent(ApplicationEvent event) { | |||
publishEvent((Object) event); | |||
} | |||
void publishEvent(Object event); | |||
} | |||
</syntaxhighlight> | |||
Events can be published directly in the application context: | |||
=Application Event Listener= | =Application Event Listener= | ||
Listeners should implement the [https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/ApplicationListener.html ApplicationListener] interface. | Listeners should implement the [https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/ApplicationListener.html ApplicationListener] interface. | ||
==Adding Application Listeners== | |||
<syntaxhighlight lang='java'> | |||
applicationContext.addApplicationListener(...); | |||
</syntaxhighlight> | |||
=Implementation Details= |
Latest revision as of 21:09, 5 December 2018
External
Internal
TO PROCESS
Overview
One of application context's capabilities is to handle events. The events are by default synchronous, which has a few advantages, one of them being the fact that the listener is able to participated in the publisher's transaction context.
Threading Consideration
Spring event handling is single threaded and synchronous in nature. If an event is published, until and unless all receivers get the message, the processes are blocked and the flow will not continue.
Event
Events are instances extending ApplicationEvent.
Standard Events
- ContextStartedEvent
- ContextStoppedEvent
Custom Event
Publisher
Events can be published using publishers. Any custom publisher should inject an ApplicationEventPublisher object.
package org.springframework.context;
@FunctionalInterface
public interface ApplicationEventPublisher {
default void publishEvent(ApplicationEvent event) {
publishEvent((Object) event);
}
void publishEvent(Object event);
}
Events can be published directly in the application context:
Application Event Listener
Listeners should implement the ApplicationListener interface.
Adding Application Listeners
applicationContext.addApplicationListener(...);