Java for Web Learning notes (eight or nine): Messages and Clusters (4) custom publishing and ordering __java

Source: Internet
Author: User
Simpleapplicationeventmulticaster

This study is to learn more about spring's support for Publish/subcribe and, on the other hand, to prepare for the use of WebSocket's small example of passing events in the cluster. Spring will publish the message as an event, publish the message through the message broadcaster, and send it to the response's subscription. Message broadcasters can be understood as broker within the app. Spring provides Simpleapplicationeventmulticaster, implements the Applicationeventmulticaster interface, and broadcasts events to the registered listener. First look at Javadoc's description of Simpleapplicationeventmulticaster:

Simple implementation of the Applicationeventmulticaster interface.

Multicasts all events to all registered listeners, leaving it up to the listeners to ignore events that they are not inter ested in. Listeners'll usually perform corresponding instanceof checks on the Passed-in event object.

By default, all listeners are invoked in the calling thread. This allows the danger of a rogue listener blocking the entire application, but adds the minimal overhead. Specify an alternative task executor to have listeners executed in different threads, for example from a thread pool.

Reference reading: Http://www.baeldung.com/spring-events implement asynchronous listener events

In the last study, it is easy to add @async to the listener trigger method, so that subscribe asynchronous processing, if more events, need to be in many ways to add asynchronous words, we can, as Javadoc said, Specifies a task executor to allow listener to execute in another thread. In other words, we need to give the applicationeventmulticaster of the definition. In the root context:

The name of the "Learning 1" bean must be applicationeventmulticaster, and if our method takes another name, that is, a applicationeventmulticaster (), it can be specified by name
@Bean (name = "Applicationeventmulticaster") public
Applicationeventmulticaster Custommulticaster () {    
    Simpleapplicationeventmulticaster eventmulticaster = new Simpleapplicationeventmulticaster ();
    "Learning 2" specifies a task executor for background processing
    eventmulticaster.settaskexecutor (new Simpleasynctaskexecutor ());
    return eventmulticaster;
}

We released an event that was monitored by two listener, and the relevant log is as follows, which implements a different

10:47:51.199 [SimpleAsyncTaskExecutor-12] [INFO] authenticationinterestedparty:20 onapplicationevent ()- Authentication event from CONTEXT/ADFSJJFLSDJFLJSEDLJFLJF received in Context/chapter18.
10:47:51.199 [SIMPLEASYNCTASKEXECUTOR-13] [INFO] logininterestedparty:21 onapplicationevent ()-Login event for context /ADFSJJFLSDJFLJSEDLJFLJF received in Context/chapter18.
further understanding

We customize a class Mymulticaster, inherit Simpleapplicationeventmulticaster, to further understand. Accordingly, in the configuration, you want to set Applicationeventmulticaster to the custom class:

@Bean public
Applicationeventmulticaster Applicationeventmulticaster () {    
    Simpleapplicationeventmulticaster eventmulticaster = new Mymulticaster ();
    Eventmulticaster.settaskexecutor (New Simpleasynctaskexecutor ());
    return eventmulticaster;
}

Let's focus on mymulticaster and rewrite the three methods:

public class Mymulticaster extends simpleapplicationeventmulticaster{
    private static final Logger log = Logmanager.getlogger ();

    @Override public
    void Multicastevent (Applicationevent event) {
        Log.traceentry ("This is a pit") This method is not actually triggered (Spring version 4.3.11.RELEASE) and requires special attention. ");
        Super.multicastevent (event);
    }

    @Override public
    void Multicastevent (Applicationevent event, Resolvabletype eventtype) {
        log.traceentry (" Called at the time of publication, EventType can be null, which runs on the thread of the publication, where it can be used for cluster message publishing and added over the network to other modules. ");
        Super.multicastevent (event, EventType);
    }

    @Override
    protected void Invokelistener (applicationlistener<?> Listener, applicationevent event) {
        Log.traceentry ("Call when triggering receive event, if set Taskexecutor, run asynchronously");
        Super.invokelistener (Listener, event);
    }
}

RELATED links: My professional Java for WEB applications related articles

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.