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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值