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 測試結果   注意控制台打印順序

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM