current position:Home>Three minute quick start of spring event mechanism # yyds dry goods inventory #

Three minute quick start of spring event mechanism # yyds dry goods inventory #

2022-01-27 01:06:59 Java4ye

How are you guys ~ Let's share this with you today Spring Event mechanism

Content overview

image-20210829132019387

principle

image-20210828184103069

This familiar Observer mode My little friend should see it at a glance ~

It's actually a simple version Release - A subscription model

There are three core classes

  1. event ApplicationEvent
  2. Event publisher ApplicationEventPublisher
  3. Event listener ApplicationListener

purpose

The event publisher publishes events , The listener listens to the corresponding event and responds accordingly ~

Why use

Because the demand is uncertain

scene

Leader : Want to send an email notification after the user's operation A

I : well

A few days later ……

Leader : Forget it , Just remind me in the station first , We'll consider sending text messages later , See which works better

I :…… well

I didn't use this event mechanism at first , I wrote it together without much thought

There's something wrong in the back , Just remember to use this

advantage

  1. decoupling You can send business code and e-mail , Station reminder , SMS, etc
  2. Asynchronous notification Don't wait for these events to complete
  3. Improve scalability It's much more convenient to expand other operations later

remind

Small systems can also use , It is reliable to directly use message oriented middleware to solve the larger problems

Project practice

image-20210829102043376

function

Send SMS and e-mail according to the feedback events of small partners

Main source code

One . Small partner feedback events ReaderFeedbackEvent

image-20210829094311899

Characteristics and functions

Inherit ApplicationEvent , Save feedback DTO

Two . Event publisher ReaderFeedbackEventPublisher

image-20210829102353506

Characteristics and functions

Realization ApplicationEventPublisherAware This interface , obtain ApplicationEventPublisher The event publisher publishes Events

3、 ... and . monitor ReaderFeedbackEvent Event and send mail

image-20210829102540185

Characteristics and functions

Realization ApplicationListener This interface , Get feedback from the event , And send mail

Four . Send SMS notification only when the feedback is a little like

image-20210829102736577

Characteristics and functions

The second way to write a listener —— annotation , It's more convenient , Listen here and judge whether you like it or not , Then send text messages

test

This is directly over here IDEA Intermediate testing ,

image-20210829111125716

The effect is as follows

like = false

image-20210829111409325

like = true

image-20210829124910499

Received text messages

<img src=" http://img.ryzeyang.top/image-20210829124817280.png" alt="image-20210829124817280" style="zoom:67%;" />

Summary

You can see that the function module is very simple , Listen for feedback events , Then send text messages and emails ~

image-20210829130532523

This part of the code is uploaded to my GitHub The warehouse ( The address is at the end of the article ), If you are interested, you can have a look , If you like, give it to star ah , thank !

Spring Source code interpretation

asynchronous / Sync

Here we make a breakpoint , You can easily come here

image-20210829104327436

This method is Event multicast SimpleApplicationEventMulticaster in , You can see that when this is configured Thread pool when , It's going asynchronous Inform the route

Then how to call the listener ?

You can see that it's simple 3 Step , The last is to go back to our onApplicationEvent Method

invokeListener ——> doInvokeListener ——> onApplicationEvent

image-20210829104759111

summary

Active learning and flexible application , Go and try it !

Project address   https://github.com/Java4ye/springboot-demo-4ye

Like it , Please order a little star

image-20210829144304728

The mind map of this issue is as follows

image-20210829132531501

ps: I've been busy recently , The speed will be slower ~

I am a 4ye We should …… See you soon !!

If you love, you can pay attention to the official account Java4ye Under the support 4ye ah

copyright notice
author[Java4ye],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201270106572068.html

Random recommended