[SpringBoot] Analyze Springboot event mechanism, event publishing and monitoring

1. What are Spring events?

Spring's event listening (also called event-driven) (Application Event) provides support for message communication between Beans. It is an implementation of the observer pattern. As long as it is the observer pattern, it contains a theme (for this theme events), publishers (publish topics or events), subscribers (people who listen to topics). It consists of three parts, event (ApplicationEvent), listener (ApplicationListener) and event publishing operation.

Its role: Using the event mechanism, we can decouple mutually coupled code, thereby facilitating function expansion and adjustment.

There are three roles in the event mechanism: event publisher, event, and event listener

  • Publisher: the object that publishes the event
  • Event: the specific content of the event
  • Event listener: object waiting for processing time

2. Usage steps

2.1 Dependency handling

SpringBoot's corresponding dependencies are already included in the context package, so we don't need to add them separately in the pom.xml file when we use them. as the picture shows:

Insert image description here

2.2 Define event entity class

The event class needs to inherit the ApplicationEvent class

Example: Define an alarm event. The attribute is the alarm entity class. It can also be other fields or entities customized according to the business.

package com.example.demozmq.event;

import org.springframework.context.ApplicationEvent;

/**
 * 定义一个报警事件,属性是报警实体类
 */
public class AlarmEvent extends ApplicationEvent {
    
    

    private PfsAlarm pfsAlarm;

    public AlarmEvent(Object source, PfsAlarm pfsAlarm) {
    
    
        super(source);
        this.pfsAlarm = pfsAlarm;
    }

    public PfsAlarm getPfsAlarm() {
    
    
        return pfsAlarm;
    }
}

2.3 Define event listening class

Event listening class, implements the ApplicationListener interface, and the generic type is <custom event entity class>

package com.example.demozmq.listener;

import com.example.demozmq.event.AlarmEvent;
import java.util.concurrent.TimeUnit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class AlarmListener implements ApplicationListener<AlarmEvent> {
    
    

    @Override
    public void onApplicationEvent(AlarmEvent alarmEvent) {
    
    
        log.info(">>>>>>>>>>>>>>>>>>>  执行事件监听开始,调用参数是:{}", alarmEvent.getPfsAlarm());
        try {
    
    
            // 此处是执行具体的业务处理逻辑
            TimeUnit.SECONDS.sleep(100);
            log.info(">>>>>>>>>>>>>>>>>>> 执行具体的任务完成!");
        } catch (Exception e) {
    
    
            e.printStackTrace();
        }
        log.info(">>>>>>>>>>>>>>>>>>> 执行事件监听结束");
    }
}

The actual application example is shown in the figure below:

Insert image description here

2.4 Event release

The following two places are both event releases, but they are different businesses. You can refer to them.

Insert image description hereInsert image description here

3. Asynchronous calls

3.1 Enable asynchronous calls

Insert image description here

3.2 Add @Async annotation to the listener method

Insert image description here

This article is finished!

Guess you like

Origin blog.csdn.net/weixin_44299027/article/details/134839770