package com.elab.spring.utils;

import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;

public class SpringEventUtils implements ApplicationEventPublisherAware {

    private static ApplicationEventPublisher applicationEventPublisher;

    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        this.applicationEventPublisher = applicationEventPublisher;
    }

    //    /**
    //     * 同步推送event事件
    //     *
    //     * @param applicationEvent 事件来源
    //     */
    //    public static void syncEventPublisher(ApplicationEvent applicationEvent) {
    //        applicationEventPublisher.publishEvent(applicationEvent);
    //    }

    public static void syncEventPublisher(ApplicationEvent applicationEvent) {
        applicationEventPublisher.publishEvent(applicationEvent);
    }

    /**
     * 异步推送event事件
     *
     * @param applicationEvent 事件来源
     */
    public static void asyncEventPublisher(ApplicationEvent applicationEvent) {
        ThreadProcessUtils.addRealTimeQueue(() -> {
            applicationEventPublisher.publishEvent(applicationEvent);
        }, null);
    }

}
