Guava Library Learning: Learning Guava Eventbus (a) Eventbus

Source: Internet
Author: User
Tags event listener eventbus

Original address: http://www.xx566.com/detail/184.html

In the software development process, the sharing of object information and direct collaboration with each other is necessary, the difficulty is to ensure that the communication between the objects is effectively completed, rather than the cost of highly coupled components. When an object has too much detail about the responsibilities of other components, it is considered highly coupled. When an application has a high degree of coupling, maintenance will become very challenging and any change will bring ripple effects. In order to solve this kind of software design problem, we need event-based programming. In this article, we will learn guava event-based programming, Guava Eventbus (a) Eventbus.


In event-based programming, objects can subscribe/listen to specific events, or publish events. In Java, we have a preliminary understanding of event interception, an event listener is an object whose purpose is to be notified when a particular event occurs. In the Guava cache series, we've already mentioned Removallistener, in this article we're going to learn the guava Eventbus class, and how it can be used for publishing and subscribing to events. The Eventbus class will be able to improve the level of mutual collaboration, and there is little coupling between objects. It is important to note that Eventbus is a lightweight, in-process publish/Subscribe communication style and is not used for interprocess communication.


In this series of studies, we will study and discuss the following:

    • Eventbus and Asynceventbus class

    • Registering subscription events and event notifications with Eventbus

    • Publish a subscription using Eventbus

    • According to our needs, write event handlers, select coarse-grained or fine-grained event-handling mechanisms

    • Using the Dependency Injection framework in conjunction with Eventbus

Eventbus

Eventbus class bit with Com.google.common.eventbus package, it is the focus and foundation of guava event-based programming, publish/subscribe programming paradigm, and at a very high level, users will register Eventbus notification of specific events, The publisher sends the event to the interested user via Eventbus. Continuous notification of all users, and more importantly, the event handling method can be executed very quickly in any code.


Creating an Eventbus instance

By calling the Eventbus constructor, we can create a Eventbus instance:

Eventbus Eventbus = new Eventbus ();

We can also provide an optional string parameter as the identifier to create a eventbus (for logging):

Eventbus EventBus1 = new Eventbus (TradeAccountEvent.class.getName ());


Subscribe to Events

You can receive a notification object from Eventbus by following three required steps:

    1. An object needs to define a public method that accepts only one parameter, which identifies an object of the event type that is interested in receiving notifications.

    2. The method of event notification exposure must use @subscribe annotations.

    3. Finally, an Eventbus instance of an object registration, the Register method of registration, itself is passed as a parameter to Eventbus.


Publish Events

To publish an event, we need to pass an event object to the Eventbus.post method, Eventbus will invoke the Subscriber registration handler method and assign the parameter with the event object type. This is a very powerful design concept that includes interfaces, hyper-classes, implementations of super-class interfaces, and so on, which means that we can easily make our event handlers become the finer granularity we want , and only the event-handling methods that are accepted by changing the type.


Defining processing methods

The method used for the event handler must accept only one event object parameter, as described previously, Eventbus will invoke the event-handling method consecutively, so it is important to ensure that these methods are completed quickly. If you need to do any extended processing of the receive event, it is best to run the code on a separate thread.


concurrency of

Eventbus does not invoke handler methods from multiple threads unless the @allowconcurrentevent annotation handler method is used. With the @allowconcurrentevent annotation annotation processing method, we assert that the handler method is thread-safe. A handler method that uses the @allowconcurrentevent annotation itself is not registered in the Eventbus.


At this point, we have simply learned how to use the Eventbus class, the next one, we let some examples to learn, welcome the continued attention.


Guava Library Learning: Learning Guava Eventbus (a) Eventbus

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.