Guava-eventbus (Event Bus)

Source: Internet
Author: User
Tags in domain eventbus

Guava provides us with the event Bus Eventbus library in Guava-libraries, which is an implementation of the event release subscription model, allowing us to decouple the design of our modules and domain boundaries in domain-driven design (DDD) with the weak reference nature of events.

No more nonsense, straight to the guava Eventbus theme. First guava provides us with synchronous event Eventbus and asynchronous implementation Asynceventbus two event bus, they are not singleton, the official reason is not to think we use the way we do. Of course, if we think of it as a singleton, we can easily encapsulate it, and a singleton pattern guarantees that only one instance is created.

The following will take Eventbus as an example, Asynceventbus use the same way.

Subscription

First Eventbus provides us with the Register method to subscribe to events, guava is very friendly here, we do not need to implement any additional interface or base class, only need to label on the subscription method @Subscribe and Guarantee Only one method of input parameters can be done. So for some simple events, we can even directly

new Object() {    @Subscribe    public void lister(Integer integer) {        System.out.printf("%d from int%n", integer);    }}

Guava published events do not handle thread safety by default, but we can label @allowconcurrentevents to ensure their thread safety

Release

For event sources, events can be published through the Post method. This is where the release of the guava for the event is determined by the method parameter type of the subscription method in the previous example, in other words, the type of post passed in and its base class type can receive this event. For example, the following example:

final EventBus eventBus = new EventBus();eventBus.register(new Object() {    @Subscribe    public void lister(Integer integer) {        System.out.printf("%s from int%n", integer);    }    @Subscribe    public void lister(Number integer) {        System.out.printf("%s from Number%n", integer);    }    @Subscribe    public void lister(Long integer) {        System.out.printf("%s from long%n", integer);    }});eventBus.post(1);eventBus.post(1L);

Here are the Integer,long, with their base class number. When we send an integer data, or the method of integer and number is received, and the long type is accepted by the long type and number type.

So bloggers suggest that it is necessary to encapsulate a particular event type for each class of time.

Deadevent

Deadevent is not sure how to translate more agreeable, it describes the death event, that is, no one is concerned about the subscriber, is not processed, and is represented by the method of the Deadevent type parameter. For example, in the example above we post an object type, as follows:

final EventBus eventBus = new EventBus();eventBus.register(new Object() {    @Subscribe    public void lister(DeadEvent event) {        System.out.printf("%s=%s from dead events%n", event.getSource().getClass(), event.getEvent());    }});eventBus.post(new Object());

More Guava Blog Posts:

    1. guava– Parallel Programming Futures
    2. Guava–eventbus (Event Bus)

Guava-eventbus (Event Bus)

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.