“This is the 13th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021”

Event-driven is a term we developers hear a lot. So what exactly does event-driven stand for? In simple terms, when one thing happens, another thing will happen. For example, when we place an order for goods, payment will be deducted after payment, order status will be changed, SMS notification will be triggered, membership points will be increased, warehouse will be notified of delivery, etc.

1 the concept

Event-driven Architecture (EDA) is a mainstream asynchronous Event distribution architecture pattern that is often used to design highly scalable applications. An event-driven architecture in which events are transmitted between loosely coupled components and services. An event-driven system generally consists of event consumers and event producers. Event consumers subscribe to events to event managers, and event producers publish events to event managers. When an event manager receives an event from an event producer, the event manager forwards the event to the appropriate event consumer. If the event consumer is unavailable, the event manager retains the event and forwards the event consumer again after an interval.

To put it simply, event-driven architecture puts our programs in a passive position, where events trigger the execution of programs.

Event-driven design cases

Event-driven GUI

Students who have done desktop programs must feel particularly strong, such as our C# WinForm program, is a component, plus an event to design, such as form loading events, mouse click, double click events, when each event will trigger a program, to handle the event, For example, a form load event triggers a query, a save event triggers the save data logic, and so on. The same is true for QQ music below. The above list will trigger different types of queries and display different pages. Clicking play will trigger the operation of playing music, which are all event-driven. The Web front end is very similar in that it listens for events to respond to and processes events as they occur.

Event-driven implementation

3.1 Observer Model

  • The observer registers with the event manager
  • The event manager listens to the message and notifies the corresponding observer to execute the corresponding method

Concrete implementation:

  • Spring Events

    The Spring framework’s event mechanism is a classic observer pattern

3.2 Publish/subscribe

  • Event sources publish messages to an abstract event manager
  • The abstract event manager puts the message into the corresponding topic
  • Recipients subscribed to the corresponding topic receive processing messages

Concrete implementation:

  • Guava EventBus EventBus
  • The message queue

    Message queues are also built with publish-subscribe in mind

4. Event-driven features

Advantages:

  • Loose coupling.
  • Support asynchronous;
  • Support reuse, easy concurrent processing;
  • Good expansibility;

Disadvantages:

  • Weakened ability to control the system;
  • Data exchange problem;
  • The system complexity increases and troubleshooting becomes more complex.