springboot自定义事件的发布和监听

时间:2019-07-04
本文章向大家介绍springboot自定义事件的发布和监听,主要包括springboot自定义事件的发布和监听使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1 创建自定义事件 继承 ApplicationEvent 

package wyp.eventspringbootdemo.wyp;

import org.springframework.context.ApplicationEvent;

/**
 * @author : miles wang
 * @date : 2019/7/4  11:43 AM
 */
public abstract class AbstractEvent extends ApplicationEvent {

    public AbstractEvent(Object source) {
        super(source);
    }
}
package wyp.eventspringbootdemo.wyp;
/**
 * @author : miles wang
 * @date : 2019/6/25  5:33 PM
 */
public class MyApplicationEvent1 extends AbstractEvent {
    public MyApplicationEvent1(Object source) {
        super(source);
    }
    //TODO do some thing 我们可以添加字段添加方法,比如写入redis的key等等
}
package wyp.eventspringbootdemo.wyp;

/**
 * @author : miles wang
 * @date : 2019/6/25  5:33 PM
 */
public class MyApplicationEvent2 extends AbstractEvent {
    public MyApplicationEvent2(Object source) {
        super(source);
    }
}

2 编写监听者

package wyp.eventspringbootdemo.wyp;

import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

/**
 * 这里提供一种监听事件的思路
 * 我们会在系统中发布不同的事件,但是我们可以写一个监听器
 * 1 写一个抽象类 AbstractEvent
 * 2 所有事件都继承AbstractEvent
 * 3 获取子类名称
 * 4 处理对应的事件
 */
@Component
public class MyApplicationListener2 {

    @EventListener
    public void  handleAbstractEvent(AbstractEvent object){
        String simpleName = object.getClass().getSimpleName();
        switch (simpleName){
            case  EventTypeConstnts.SOME_EVENt_NAME1:
                System.out.println("this is :"+simpleName);
                //TODO do someting :consume event
                break;
            case  EventTypeConstnts.SOME_EVENt_NAME2:
                System.out.println("this is :"+simpleName);
                break;
            default:
                return;
        }
        System.out.println(simpleName);
    }
}

3 启动类

package wyp.eventspringbootdemo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ConfigurableApplicationContext;
import wyp.eventspringbootdemo.wyp.MyApplicationEvent1;
import wyp.eventspringbootdemo.wyp.MyApplicationEvent2;
import wyp.eventspringbootdemo.wyp.MyApplicationListener1;

@SpringBootApplication
public class EventSpringbootDemoApplication {


    public static void main(String[] args) {
        ConfigurableApplicationContext run = SpringApplication.run(EventSpringbootDemoApplication.class, args);
//        SpringApplication springApplication = new SpringApplication(EventSpringbootDemoApplication.class);
//        springApplication.addListeners(new MyApplicationListener1());
//        ConfigurableApplicationContext run = springApplication.run(args);
//        run.publishEvent(new MyApplicationEvent1("这是一个event"));
//        run.close();
        run.publishEvent(new MyApplicationEvent1("这是一个event1"));
        run.publishEvent(new MyApplicationEvent2("这是一个event2"));
        run.close();

    }

}

4 跨服务监听事件思路

 我们可以编写一个类 负责把spring的event发送到我们的事件中心系统中, 也就是kafka当中 这样不同的服务可以消费kafka

5 测试结果   注意控制台打印顺序

原文地址:https://www.cnblogs.com/wangpipi/p/11134175.html