SpringBoot uses @EventListener annotation

background

In the development work, you will encounter a scenario where after a certain thing is done, you need to broadcast some messages or notifications to tell other modules to perform some event processing. Generally speaking, you can send requests one by one to notify, but There is a better way, that is, event monitoring, which is also an implementation of publish-subscribe mode and observer mode in design mode. java

Observer mode: To put it simply, when you are doing something, someone around you is staring at you. When you do something that is of interest to the observer, he will do something else based on this. But the person staring at you must register with you, otherwise you will not be able to notify him (or he is not qualified to stare at you to do things). web

For some events of the Spring container, it can listen and trigger the corresponding method. There are two general methods, ApplicationListener interface and **@EventListener** annotation. the spring

Introduction

In order to successfully create and function the listener, several roles are required in this process:
1. The event (event) can encapsulate and pass the parameters to be processed in the listener, such as objects or strings, and serve as a listener The target monitored in the device.
2. The listener (listener) is specifically based on the business processing module where the event occurs, here it can receive the object or string encapsulated in the processing event.
3. Event publisher (publisher) The triggerer of the event.

how to use

custom event

@Builder
public class ImPushEvent extends ApplicationEvent {
    private ReceiveMessage receiveMessage;

    public ImPushEvent(Object source) {
        super(source);
    }



    public ImPushEvent(Object source, ReceiveMessage receiveMessage) {
        super(source);
        this.receiveMessage = receiveMessage;
    }


    public ReceiveMessage getReceiveMessage() {
        return receiveMessage;
    }

    public void setReceiveMessage(ReceiveMessage receiveMessage) {
        this.receiveMessage = receiveMessage;
    }

    @Override
    public String toString() {
        return "ImPushEvent{" +
                "receiveMessage=" + receiveMessage +
                '}';
    }
}

Define event publishing

@Slf4j
@Component
public class ImMsgEventPublisher {
    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;//事件发送

    /**
     * 监听事件发送
     *
     * @param receiveMessage
     */
    public void pushEventMsg(ReceiveMessage receiveMessage) {
        log.info("im事件监听发送消息啦~,接收到数据:{}", receiveMessage.toString());
        this.applicationEventPublisher.publishEvent(new ImPushEvent(this,receiveMessage));
    }
}

event listener

@Slf4j
@Component
public class ImMsgEventListener {
    //消息推送开关(默认关闭)
    private volatile boolean sendSwitch = false;
    //有界队列 默认1000个
    private BlockingQueue<ReceiveMessage> imMsgQueue = new ArrayBlockingQueue(1000);
 

    /**
     * 消息事件监听,推送im消息推送
     *
     * @param imPushEvent
     */
    @Async("asyncThreadPool")
    @EventListener
    public void handleImMsgEvent(ImPushEvent imPushEvent) {
        if (imPushEvent == null) return;
        if (imPushEvent.getReceiveMessage() == null) {
            log.info("im 监听到得消息为空");
            return;
        }
        log.info("im 监听到得消息量:{}", imPushEvent.getReceiveMessage().toString());
        this.imMsgQueue.add(imPushEvent.getReceiveMessage());

//        /TODO 异步处理消息数据(可放入有序队列中依次处理)
        log.info("im 监听到消息啦~,消息数量:{}", imMsgQueue.size());
      //TODO 业务处理
    }

It can also be achieved by implementing the ApplicationListener interface

@Slf4j
@Component
public class ImMsgEventPublisher implements ApplicationListener<StudentEvent> {


    @Override
    public void onApplicationEvent(ImPushEvent imPushEvent) {
        
         if (imPushEvent == null) return;
        if (imPushEvent.getReceiveMessage() == null) {
            log.info("im 监听到得消息为空");
            return;
        }
        log.info("im 监听到得消息量:{}", imPushEvent.getReceiveMessage().toString());
        this.imMsgQueue.add(imPushEvent.getReceiveMessage());

//        /TODO 异步处理消息数据(可放入有序队列中依次处理)
        log.info("im 监听到消息啦~,消息数量:{}", imMsgQueue.size());
      //TODO 业务处理
    }

}

Guess you like

Origin blog.csdn.net/CharlesYooSky/article/details/127432845