Event-driven application-use Queue to simulate operation

Czxdas
4 min readNov 9, 2021

--

Recently, I have been guided by senior architects and learned about the benefits of Event-driven. Event-driven can use the Observer pattern to make a similar description and comparison. Generally speaking, what action is executed and the event is triggered at the same time to make instant notifications, which can achieve the effect of Real Time.

Most of the traditional methods are polling, which constantly allows the system to spend resources to get the purpose to be executed. Event driven, on the contrary, can leave the system in an idle state, and when there is a real demand, it will really spend system resources processing, so it can save resources and effectively use resources. Future systems should be able to be widely used with this concept to achieve future requirements.

We can use Queue to achieve the effect of instant notification and achieve the purpose of Event-driven Real-Time。

A simple example is used to simulate Event-driven. First, we will use Rabbit MQ to display, and then use the self-made Queue category to achieve. No matter what Component is used for implementation, it is still important to pay attention to whether the objects sent to the Queue are important enough to not disappear out of thin air, or if the data is missing after the power is turned off and the power is restored, it seems that nothing happened. You can start in two ways:

  1. Queue or other implemented components have a management model that meets the requirements for supporting.
  2. If the process of sending the object fails, there can be a re-sending mechanism.

In fact, the above supporting facilities have their complexity, so they are put forward for consideration first. Here we mainly show the basic implementation.

The simple design concept diagram is as follows:

Rabbit MQ can be replaced with other QUEUE, or other components that can be notified immediately.

To implement a simple example of Rabbit MQ, as follows:

First create an Action that puts text through the API.

Immediately receive the event-driven Rabbit MQueue in the establishment background.

The next step is to implement, use HTTP Request to send the API, this example prints three data in sequence, the background will immediately find that the data is sent to Rabbit MQ, trigger Corresponding event.

The event here is to show the content after taking it out.

Event-Driven intermediary driver through Queue is very simple and convenient. However, as mentioned in the premise, it is still necessary to consider the matching.

If in a very simple situation, you just want to show an intermediary driver, you can also customize a component yourself, and then continue to fill in the supporting components.

The following is a simple example to demonstrate an intermediary driver using the .NET Queue object. First is the category:

Then comes the API:

The last is the status of implementation. After being sent to the Queue, an event is triggered to print out the message.

Through a simple example to experience what Event-driven is going on, and after understanding it still needs matching and other matching to complete.

--

--

Czxdas
Czxdas

Written by Czxdas

Keep looking for Prajna wisdom

No responses yet