"Bird-java" event bus between distributed services Eventbus

Source: Internet
Author: User
Tags event listener eventbus

Tag: false getc Style time () Targe ref event Send identity

What is Eventbus
Eventbus is an implementation of the publish-subscribe pattern. It realizes the decoupling and communication between components in a very elegant way, and it is widely used in the fields of Android development, DDD and so on.

The event flow is roughly as follows:

    • Producer sends an event to Eventbus.
    • Eventbus pushes events to all consumer that have listened to the event.
    • A consumer consumption event was monitored for this event.


Note: A component can be either a producer or a consumer.

The Eventbus between distributed services
In distributed systems, the delivery of events between services is much more complex than single-machine eventbus. Is there a eventbus for distributed services, and event passing is as simple as a single machine? Search the Java implementation of Eventbus on GitHub, the top ten are almost all for Android or Java single-machine event bus. After a long ... Let's do it yourself. The Eventbus in a clustered environment will require more consideration than the standalone version, such as:

    1. In the case of a service cluster deployment, how to ensure that each cluster can subscribe to the event, and that each cluster can consume the event only once.
    2. How to implement a service within multiple ' xxxservice ' subscriptions to the same event.

Solution:

    1. Using ' Kafka ' to implement a publish subscription between clusters (based on ' topic '), the same cluster is in the same Kafka Consumer-group to ensure that each cluster consumes only one event at a time.
    2. The service can be reflected at startup to get all classes that implement the ' ieventhandler<teventarg> ' and cache, and the service consumes the message when it gets all the handler that registered the message and calls its ' handleevent ' method.

Some key source code

1. Definition of event message

 Public Abstract classEventArgImplementsieventarg{PrivateDate eventtime;  PublicEventArg () {eventtime=NewDate (); }     PublicDate Geteventtime () {returneventtime; }     Public voidseteventtime (Date eventtime) { This. Eventtime =eventtime; }}

Event message Default record creation time, can extend other fields, such as Send time, identity, etc.

2. Send messages using Spring-kafka

/*** Kafka Event Registrar, push message to Kafka queue*/@Component Public classKafkaregisterImplementsIeventregister {@Autowired (required=false)    PrivateKafkatemplate<string,ieventarg>kafkatemplate; /*** Event Registration * *@paramEventArg Event Parameters*/@Override Public voidregist (Ieventarg eventarg) {kafkatemplate.send (Gettopic (EventArg), EventArg); }    /*** Get Kafka's topic * *@paramEventArg *@returnTopic*/    PrivateString gettopic (Ieventarg eventarg) {returnEventarg.getclass (). GetName (); }}

3. Consume Kafka messages and perform all events subscribed to the message in the current service

/*** Kafka Event Listener*/ Public classKafkaeventarglistenerImplementsMessagelistener<string,eventarg>{@AutowiredPrivateieventhandlerfactory eventhandlerfactory; @Override Public voidOnMessage (consumerrecord<string, eventarg>Consumerrecord) {        if(Consumerrecord = =NULL)return; EventArg value=Consumerrecord.value (); Set<IEventHandler> handlers =eventhandlerfactory.gethandlers (value); if(Handlers = =NULL)return;  for(Ieventhandler handler:handlers) {handler.        Handleevent (value); }    }}

Use of Eventbus

1, the definition of the event. All events are inherited from the EventArg abstract class above, as shown in the following example:

 Public class extends eventarg{    private  String value;      Public String GetValue () {        return  value;    }      Public void SetValue (String value) {        this. Value = value;    }} 

2, Event release. Example code:

Eventbus.push (new testeventarg ());

3, event subscription. After a service publishing event, any ' Xxxserviceimpl ' class in any service can subscribe to the event, implementing the ' ieventhandler<teventarg> ' interface to complete the subscription to the event, as shown in the following example:

 Public class extends Implements Formservice,ieventhandler<testeventarg> {    @Override    publicvoid  Handleevent (Testeventarg eventarg) {        System.out.println ("Notify zero======");}    }

Overall, the use is still very simple, eventbus implementation and use examples are included in the Bird-java Project, Project address: Https://github.com/liuxx001/bird-java.

"Bird-java" event bus between distributed services 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.