Guava - EventBus (Event Bus)

Guava atguava-librariesWe provide the event bus EventBus library, which is an implementation of the event publishing subscription mode, which allows us to decouple our module and domain boundaries with the weak reference nature of events in domain-driven design (DDD).

No more nonsense, go straight to the Guava EventBus theme. First of all, Guava provides us with two events bus, the synchronous event EventBus and the asynchronous implementation of AsyncEventBus. They are not singletons. The official reason is not to think about how we use them. Of course, if we want to be a singleton, we can easily package it, and a singleton pattern guarantees that only one instance is created.

Let's take EventBus as an example. AsyncEventBus is used in the same way.

subscription

First of all, EventBus provides us with a register method to subscribe to events. Guava's implementation here is very friendly. We don't need to implement any extra interfaces or base classes. We only need to mark the subscription method.@SubscribeAnd guaranteeOnly one input parameterThe method can be done. So for some simple events, we can even go straight

new Object() {

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

Events published by Guava do not handle thread-safe by default, but we can mark @AllowConcurrentEvents to keep them thread safe.

release

For event sources, events can be published via the post method. The release of events for Guava is determined by the method parameter type of the subscription method in the above example. In other words, the type of post incoming 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 Integer, Long, and their base class Number. When we send an integer data, either the Integer and Number methods are received, and the Long type is accepted by the Long type and the Number type.

So bloggers recommend that it is necessary to encapsulate a specific type of event for each type of event.

DeadEvent

DeadEvent is temporarily unclear how to translate more desirable. It describes the death event, that is, there is no subscriber concern, no processing, and is represented by the method of the DeadEvent type parameter. For example, in the above example, 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)
This article is transferred from the Broken Wolf Blog Park blog, the original link: http://www.cnblogs.com/whitewolf/p/4132840.html, if you need to reprint, please contact the original author by yourself.

Intelligent Recommendation

ionic4-EventBus (event bus)

ionic4-EventBus (event bus) surroundings Welcome to visit me:Ionic4 column Foreword: AngularJs, we can use b r o a d c a s t 、 broadcast、 broadcast、On to send out, listen to the broadcast, to achieve ...

VUE event bus eventbus

table of Contents First, introduction to eventbus Second, the problem needs to be solved Third, how to use (1) Create a js file of EventBus (two) global call (3) Sending an event (4) Receiving events ...

Vue eventBus event bus

After the project is over, the state management or component value is often used. It is more troublesome to use the traditional method, so there is a big invented bus stuff, here is a summary. Reprint...

vue ---- Event Bus (eventBus)

Vue event bus (eventBus) EventBus event bus for communication between the two components, is used to establish eventBus.js file as follows: Import respectively in two components need to communicate in...

Android event bus: EventBus

Recently another project to maintain, are relatively old variety of libraries used, eventbus using the 2.x version, then upgrade to one, by the way read the next eventbus source, make a note of this: ...

More Recommendation

EventBus of Android event bus

First language EventBus is an Android-optimized publish/subscribe message bus, which simplifies communication between components in the application, and between components and background threads. For ...

Event bus: EventBus

When non-parent-child components need to communicate, in addition to vuex, there isEvent busUp Introduction EventBus is also called event bus. In Vue, you can use EventBus as a communication bridge co...

EventBus event processing bus

Write first EventBus is an event processing bus, which can replace Android's traditional Intent, Handler, Broadcast or interface functions to transfer data and execute methods between Fragment, Activi...

Android EventBus event bus

Android EventBus Preface: Mainly analyze and understand from three aspects: what is EventBus, why use EventBus and how to use EventBus. What is EventBus (what) Why use EventBus (why) How to use EventB...

EventBus 3.0 Event Bus

How to use EventBus 3.0 and some problems encountered during use Hmm~ I am a new Android programmer This is the first article I wrote on the blog, and I finally finished writing with a nervous mood De...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top