Posts

Showing posts from 2022

Event Handling in Spring

Spring's event handling is single-threaded so if an event is published,  until and unless all the receivers get the message, the processes are blocked and the flow will not continue. [Synchronous] Four things necessary for an event: Source : to publish the event in Spring - ApplicationEventPublisher object Event : event must extends from ApplicationEvent // springframework 4.2 + - - any object  Listener : IOC container Handler : Handler method must annotated with @EventListener Creating, publishing and handling custom events in Spring Create an event class, `CustomEvent` by extending `ApplicationEvent`. This class must define a default constructor which should inherit constructor from ApplicationEvent class. import org.springframework.context.ApplicationEvent; public class CustomEvent extends ApplicationEvent {     public CustomEvent(Object source) {         super(source);    }    public String toStri...