Spring event Monitoring Mechanism

Source: Internet
Author: User
Tags event listener getnotify

Key members of the event mechanism:
    1. Event
    2. Event listeners (listen for event triggers, handle things)
    3. Event Source (Publish event)

Javase provides the standard for a range of custom events.

Evenobject, the base class for event types provided for Javase, any custom event must inherit from it.

EventListener, the base class for event listeners provided for Javase, any custom event listeners are implemented.

Javase does not provide an event publisher, which is the role of each application to implement the event publisher itself.

Spring provides the Applicationeventpublisher interface as the event Publisher, and the ApplicationContext interface inherits the interface and acts as the publisher of the event. However, the specific implementation of the ApplicationContext interface is different.

Spring provides the Applicationeventmulticaster interface for managing Applicationlistener and publishing Applicationevent. The ApplicationContext interface delegates the related work of the event to the Applicationeventmulticaster implementation class,

Spring Event mechanism Flow:
    • ApplicationContext Publishing Events
Publish event context.publishevent;

The behavior of publishing events is actually performed by ApplicationContext implementation Abstractapplicationcontext

    • Find event broadcaster, broadcast event
Getapplicationeventmulticaster (). Multicastevent (Applicationevent, EventType);
    • Executes the Multicastevent method in Simpleapplicationeventmulticaster, invoking the Onapplicationevent () method of the event listener
@Overridepublic void Multicastevent (Final applicationevent event, Resolvabletype eventtype) {Resolvabletype type = ( EventType! = null? Eventtype:resolvedefaulteventtype (event)); for (Final applicationlistener<?> listener: Getapplicationlisteners (event, type)) {Executor Executor = Gettaskexecutor (); if (Executor! = null) {Executor.execute ( New Runnable () {@Overridepublic void run () {Invokelistener (Listener, event);}}); else {Invokelistener (listener, event);}}}


protected void Invokelistener (applicationlistener<?> Listener, applicationevent event) {ErrorHandler ErrorHandler = Geterrorhandler (); if (ErrorHandler! = null) {try {Doinvokelistener (listener, event);} catch (Throwable err) {errorhandler.handleerror (err);}} else {Doinvokelistener (listener, event);}} @SuppressWarnings ({"Unchecked", "rawtypes"}) private void Doinvokelistener (Applicationlistener listener, Applicationevent event) {try {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;}}}


Custom Events Demo
    • Custom events


Package Com.spring.event.event;import Org.springframework.context.applicationevent;import com.spring.event.bean.notify;/** * Custom Event * @author SXQ * @time August 31, 2018 Morning 10:26:24 * */public class Notifyevent extends applicationevent {private int version;private Notify notify;public int getversion () {return version;} public void setversion (int version) {this.version = version;} /** * Serialversionuid */private static final long serialversionuid = -6198589267233914254l;public notifyevent (Object sou RCE) {super (source);} Public Notify getnotify () {return Notify;} public void Setnotify (Notify Notify) {this.notify = Notify;} Public notifyevent (Object source,notify Notify) {this (source); this.notify = Notify;}}


    • Custom Listener
Package Com.spring.event.listner;import Org.springframework.context.applicationlistener;import Org.springframework.stereotype.component;import com.spring.event.event.notifyevent;/** * Custom Listener * @author SXQ * @time August 31, 2018 Morning 10:27:04 * */@Componentpublic class Notifylistenr implements applicationlistener<notifyevent>{@ overridepublic void Onapplicationevent (Notifyevent event) {System.out.println (Event.getnotify (). toString ());// After listening for events, handling subsequent things}}


    • Release
ApplicationContext context = new Classpathxmlapplicationcontext ("Spring.xml"); Notify Notify = new Notify ("John Doe", 21); Notifyevent event = new Notifyevent ("Notifyevent", notify); Event.setversion (100);//Publish Events Context.publishevent (event);


Spring event Monitoring Mechanism

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.