spring-事件發(fā)布器

實(shí)現(xiàn):異步監(jiān)聽兩個(gè)事件,update事件和delete事件
上代碼:
刪除事件定義:

public class DeleteEvent extends ApplicationEvent {
    private String eventId;

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

    public DeleteEvent(Object source, Clock clock) {
        super(source, clock);
    }

    public String getEventId() {
        return eventId;
    }

    public void setEventId(String eventId) {
        this.eventId = eventId;
    }
}

更新事件定義:

public class UpdateEvent extends ApplicationEvent {
    private String eventId;

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

    public UpdateEvent(Object source, Clock clock) {
        super(source, clock);
    }

    public String getEventId() {
        return eventId;
    }

    public void setEventId(String eventId) {
        this.eventId = eventId;
    }
}

自定義事件多波器,實(shí)現(xiàn)異步處理:

@Component
public class ListenerConfig {
   //異步執(zhí)行的關(guān)鍵,如果不配置Multicaster,則是同步執(zhí)行
   //ApplicationEvent默認(rèn)是同步執(zhí)行
    @Bean
    public SimpleApplicationEventMulticaster applicationEventMulticaster() {
        SimpleApplicationEventMulticaster simpleApplicationEventMulticaster = new SimpleApplicationEventMulticaster();
        BlockingQueue<Runnable> blockingQueue = new LinkedBlockingDeque<>(1000);
        //根據(jù)實(shí)際需要調(diào)整線程數(shù)
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(5, 20, 1, TimeUnit.SECONDS, blockingQueue);
        simpleApplicationEventMulticaster.setTaskExecutor(threadPoolExecutor);
        return simpleApplicationEventMulticaster;
    }

    @Bean
    public ApplicationListener<UpdateEvent> updateListener(){
        return updateEvent -> {
            System.out.println("收到更新事件"+updateEvent);
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("輸出更新事件id:"+updateEvent.getEventId());
        };
    }

    @Bean
    public ApplicationListener<DeleteEvent> deleteListener(){
        return deleteEvent -> {
            System.out.println("收到刪除事件"+deleteEvent);
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("輸出刪除事件id:"+deleteEvent.getEventId());
        };
    }
}

測試:
方式一:

    @Autowired
    private ApplicationEventPublisher eventPublisher;

    @Test
    public void tset(){
        System.out.println("事件測試開始。。。。");
        UpdateEvent update = new UpdateEvent("update");
        update.setEventId("update-1");
        eventPublisher.publishEvent(update);
        DeleteEvent deleteEvent = new DeleteEvent("delete");
        deleteEvent.setEventId("delete-1");
        eventPublisher.publishEvent(deleteEvent);
        System.out.println("事件測試結(jié)束。。。。。");
    }

方式二:

   @Test
    public void tset(){
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(ListenerConfig.class);
        System.out.println("事件測試開始。。。。");
        UpdateEvent update = new UpdateEvent("update");
        update.setEventId("update-1");
        applicationContext.publishEvent(update);
        DeleteEvent deleteEvent = new DeleteEvent("delete");
        deleteEvent.setEventId("delete-1");
        applicationContext.publishEvent(deleteEvent);
        applicationContext.close();
        System.out.println("事件測試結(jié)束。。。。。");
    }

結(jié)果輸出:


image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容