亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

springboot?publish?event?事件機制demo分享

 更新時間:2022年10月27日 09:40:08   作者:阿拉的夢想  
這篇文章主要介紹了springboot?publish?event?事件機制demo,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

1. 使用ApplicationEventPublisher 發(fā)布事件

復制下面全部代碼,右鍵包名,粘貼即可生成java類,執(zhí)行即可看到效果。

事件機制

  • 需要自定義一個事件類繼承ApplicationEvent;
  • 需要自定義一個監(jiān)聽器類實現ApplicationListener接口,或普通類的方法中使用@EventListener注解;
  • 使用默認發(fā)布器ApplicationEventPublisher發(fā)布即可;
  • 事件類不需要注入到IOC;監(jiān)聽器需要注入到IOC;ApplicationEventPublisher用Autowired注入進來即可;
  • 默認情況下,事件發(fā)布與執(zhí)行是同步的,事件執(zhí)行完畢,發(fā)布者才會執(zhí)行下面的邏輯;
package com.example.controller;

import com.alibaba.fastjson.JSON;
import com.example.SpringbootRedisApplication;
import lombok.Data;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Component;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.Objects;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = SpringbootRedisApplication.class)
@Slf4j
@EnableAsync
public class PublishEventDemo {
    /**
     * 事件機制:
     * 需要自定義一個事件類繼承ApplicationEvent;
     * 需要自定義一個監(jiān)聽器類實現ApplicationListener接口,或普通類的方法中使用@EventListener注解;
     * 使用默認發(fā)布器ApplicationEventPublisher發(fā)布即可;
     * 事件類不需要注入到IOC;監(jiān)聽器需要注入到IOC;ApplicationEventPublisher用Autowired注入進來即可;
     */


    /**
     * 事件發(fā)布器
     */
    @Autowired
    private ApplicationEventPublisher eventPublisher;

    /**
     * 測試類
     */
    @Test
    public void publishTest() throws InterruptedException {
        Task task = new Task();
        task.setId(1L);
        task.setTaskName("測試任務");
        task.setTaskContext("任務內容");
        task.setFinish(false);
        MyEvent event = new MyEvent(task);
        log.info("開始發(fā)布任務");
        eventPublisher.publishEvent(event);
        //applicationContext.publishEvent(event);
        log.info("結束發(fā)布任務");
        Thread.sleep(10000);
    }
}

/**
 * 任務類
 * 任務就是一個普通的類,用來保存你要發(fā)布事件的內容。這個沒有特殊限制,可以根據自己業(yè)務隨意設置。
 */
@Data
class Task {

    private Long id;

    private String taskName;

    private String taskContext;

    private boolean finish;
}

/**
 * 事件類
 * 事件類需要繼承org.springframework.context.ApplicationEvent,這樣發(fā)布的事件才能被Spring所識別
 */
@Slf4j
class MyEvent<T> extends ApplicationEvent {

    private Task task;

    public MyEvent(Task task) {
        super(task);
        this.task = task;
    }

    public Task getTask() {
        return task;
    }
}

/**
 * 事件監(jiān)聽類
 * 事件的監(jiān)聽器需要實現org.springframework.context.ApplicationListener,并且需要注入到容器之中。
 */
@Component
@Slf4j
class MyEventListenerA implements ApplicationListener<MyEvent> {
    /**
     * 監(jiān)聽方式1:實現ApplicationListener接口,重寫onApplicationEvent方法
     * 需要使用Component注入IOC
     */
    @SneakyThrows
    @Async
    @Override
    public void onApplicationEvent(MyEvent MyEvent) {
        Thread.sleep(5000);
        if (Objects.isNull(MyEvent)) {
            return;
        }
        Task task = MyEvent.getTask();
        log.info("監(jiān)聽器A接收任務:{}", JSON.toJSONString(task));
        task.setFinish(true);
        log.info("監(jiān)聽器A此時完成任務");

    }
}

@Component
@Slf4j
class MyEventListenerB implements ApplicationListener<MyEvent> {
    /**
     * 監(jiān)聽方式2:接口和注解混合使用
     * 但此時 @EventListener不能與注解@Async在同一個類中使用,會報錯,至于為什么,不知道;
     * 需要使用Component注入IOC
     */
    //@Async//加上這個,@EventListener的方法就會報java.lang.IllegalStateException: Failed to load ApplicationContext
    @SneakyThrows
    @Override
    public void onApplicationEvent(MyEvent MyEvent) {
        Thread.sleep(1000);
        if (Objects.isNull(MyEvent)) {
            return;
        }
        Task task = MyEvent.getTask();
        log.info("監(jiān)聽器B接收任務:{}", JSON.toJSONString(task));
        task.setFinish(true);
        log.info("監(jiān)聽器B此時完成任務");
    }

    @EventListener
    public void someMethod(MyEvent event) throws InterruptedException {
        Thread.sleep(1000);
        log.info("監(jiān)聽器@EventListenerB收到={}", event.getTask());
    }
}


@Component
@Slf4j
class MyEventListennerC {
    /**
     * 監(jiān)聽方式3:注解@EventListener的監(jiān)聽器不需要實現任何接口
     * 需要使用Component注入IOC
     */
    @EventListener
    public void someMethod(MyEvent event) throws InterruptedException {
        Thread.sleep(1000);
        log.info("監(jiān)聽器@EventListenerC收到={}", event.getTask());
    }
}

運行日志:

2020-11-30 18:13:56.238  INFO 19776 --- [           main] com.example.controller.PublishEventDemo  : Started PublishEventDemo in 11.098 seconds (JVM running for 13.737)
2020-11-30 18:13:56.902  INFO 19776 --- [           main] com.example.controller.PublishEventDemo  : 開始發(fā)布任務
2020-11-30 18:13:57.904  INFO 19776 --- [           main] com.example.controller.MyEventListenerB  : 監(jiān)聽器@EventListenerB收到=Task(id=1, taskName=測試任務, taskContext=任務內容, finish=false)
2020-11-30 18:13:58.905  INFO 19776 --- [           main] c.example.controller.MyEventListennerC   : 監(jiān)聽器@EventListenerC收到=Task(id=1, taskName=測試任務, taskContext=任務內容, finish=false)
2020-11-30 18:13:59.920  INFO 19776 --- [           main] com.example.controller.MyEventListenerB  : 監(jiān)聽器B接收任務:{"finish":false,"id":1,"taskContext":"任務內容","taskName":"測試任務"}
2020-11-30 18:13:59.921  INFO 19776 --- [           main] com.example.controller.MyEventListenerB  : 監(jiān)聽器B此時完成任務
2020-11-30 18:13:59.921  INFO 19776 --- [           main] com.example.controller.PublishEventDemo  : 結束發(fā)布任務
2020-11-30 18:14:03.913  INFO 19776 --- [         task-1] com.example.controller.MyEventListenerA  : 監(jiān)聽器A接收任務:{"finish":true,"id":1,"taskContext":"任務內容","taskName":"測試任務"}
2020-11-30 18:14:03.913  INFO 19776 --- [         task-1] com.example.controller.MyEventListenerA  : 監(jiān)聽器A此時完成任務
2020-11-30 18:14:09.958  INFO 19776 --- [       Thread-3] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'

事件發(fā)送后,會等待事件執(zhí)行完畢,因此他們是同步的。若想異步執(zhí)行事件,可以把@Async加到監(jiān)聽方法上;

2. 使用ApplicationContext發(fā)布事件

與上例不同之處

  • 使用ApplicationContext發(fā)布事件,ApplicationContext實現了ApplicationEventPublisher接口;
  • 使用ApplicationContextEvent 定義事件,ApplicationContextEvent 繼承了ApplicationEvent類;

demo代碼:

package com.example.controller;

import ch.qos.logback.classic.Logger;
import com.example.SpringbootRedisApplication;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ApplicationContextEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.bind.annotation.RestController;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = SpringbootRedisApplication.class)
@RestController
@Slf4j
public class PublishEventDemo2 {
    /**
     * 使用ApplicationContext發(fā)布事件
     */
    @Autowired
    ApplicationContext applicationContext;

    @Test
    public void send() {
        MyEvent2 myEvent2 = new MyEvent2(applicationContext);
        myEvent2.setData("數據");
        log.info("開始發(fā)送事件");
        applicationContext.publishEvent(myEvent2);
        log.info("結束發(fā)送事件");
    }
}

/**
 * 監(jiān)聽器類
 */
@Slf4j
@Component
class MyEventListener2 implements ApplicationListener<MyEvent2> {
    @Override
    public void onApplicationEvent(MyEvent2 event) {
        log.info("監(jiān)聽器MyEventListener2收到={}", event);
    }

    @EventListener
    public void someMethod(MyEvent2 event) {
        log.info("監(jiān)聽器MyEventListener2@EventListener收到={}", event);
    }
}

/**
 * 監(jiān)聽器類
 */
@Slf4j
@Component
class MyEventListenner1 {
    @EventListener
    public void someMethod(MyEvent2 event) {
        log.info("監(jiān)聽器MyEventListenner1收到={}", event);
    }
}

/**
 * 事件類
 *
 * @param <T>
 */
@ToString
class MyEvent2<T> extends ApplicationContextEvent {
    private T data;

    public MyEvent2(ApplicationContext source) {
        super(source);
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}

運行日志:

2020-11-30 18:03:38.638  INFO 15792 --- [           main] c.example.controller.PublishEventDemo2   : Started PublishEventDemo2 in 9.571 seconds (JVM running for 12.677)
2020-11-30 18:03:39.355  INFO 15792 --- [           main] c.example.controller.PublishEventDemo2   : 開始發(fā)送事件
2020-11-30 18:03:39.358  INFO 15792 --- [           main] com.example.controller.MyEventListener2  : 監(jiān)聽器MyEventListener2@EventListener收到=MyEvent2(data=數據)
2020-11-30 18:03:39.359  INFO 15792 --- [           main] c.example.controller.MyEventListenner1   : 監(jiān)聽器MyEventListenner1收到=MyEvent2(data=數據)
2020-11-30 18:03:39.359  INFO 15792 --- [           main] com.example.controller.MyEventListener2  : 監(jiān)聽器MyEventListener2收到=MyEvent2(data=數據)
2020-11-30 18:03:39.359  INFO 15792 --- [           main] c.example.controller.PublishEventDemo2   : 結束發(fā)送事件
2020-11-30 18:03:39.435  INFO 15792 --- [       Thread-3] o.s.s.concurrent.ThreadPoolTaskExecutor  : Shutting down ExecutorService 'applicationTaskExecutor'

事件發(fā)送后,會等待事件執(zhí)行完畢,因此他們是同步的。若想異步執(zhí)行事件,可以把@Async加到監(jiān)聽方法上;

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • 快速搭建Spring Boot+MyBatis的項目IDEA(附源碼下載)

    快速搭建Spring Boot+MyBatis的項目IDEA(附源碼下載)

    這篇文章主要介紹了快速搭建Spring Boot+MyBatis的項目IDEA(附源碼下載),本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-12-12
  • Java算法之計數排序、桶排序、基數排序實現代碼

    Java算法之計數排序、桶排序、基數排序實現代碼

    這篇文章主要介紹了Java算法之計數排序、桶排序、基數排序實現的相關資料,分別通過計數、分桶和位數處理實現高效穩(wěn)定排序,文中通過代碼及圖文介紹的非常詳細,需要的朋友可以參考下
    2025-05-05
  • Spring Cloud之注冊中心Nacos的使用詳解

    Spring Cloud之注冊中心Nacos的使用詳解

    本文介紹SpringCloud Alibaba中的Nacos組件,對比了Nacos與Eureka的區(qū)別,展示了如何在項目中引入SpringCloud Alibaba和Nacos,并配置負載均衡,通過實際操作,驗證了服務注冊和負載均衡的功能,感興趣的朋友一起看看吧
    2025-03-03
  • 阿里通用OCR文字識別/圖像識別/圖片識別對接代碼示例(Java篇)

    阿里通用OCR文字識別/圖像識別/圖片識別對接代碼示例(Java篇)

    這篇文章主要介紹了阿里通用OCR文字識別/圖像識別/圖片識別對接(Java篇)的相關資料,文中詳細介紹了包括開通服務、測試圖片、編寫識別代碼、處理識別結果等步驟,需要的朋友可以參考下
    2024-12-12
  • mybatisplus駝峰命名映射的問題解決

    mybatisplus駝峰命名映射的問題解決

    本文主要介紹了mybatisplus駝峰命名映射的問題解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-07-07
  • TKMybatis的介紹和使用詳解

    TKMybatis的介紹和使用詳解

    TKMybatis 是基于 Mybatis 框架開發(fā)的一個工具,本文主要介紹了TKMybatis的介紹和使用詳解,內部實現了對單表的基本數據操作,只需要簡單繼承 TKMybatis 提供的接口,就能夠實現無需編寫任何 sql 即能完成單表操作,感興趣的可以了解一下
    2021-12-12
  • Spring事務處理Transactional,鎖同步和并發(fā)線程

    Spring事務處理Transactional,鎖同步和并發(fā)線程

    本文詳細講解了Spring事務處理Transactional,鎖同步和并發(fā)線程。對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-12-12
  • Lombok不生效,提示java:?找不到符號的解決方案

    Lombok不生效,提示java:?找不到符號的解決方案

    這篇文章主要介紹了Lombok不生效,提示java:?找不到符號的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • jpa使用uuid策略后無法手動設置id的問題及解決

    jpa使用uuid策略后無法手動設置id的問題及解決

    這篇文章主要介紹了jpa使用uuid策略后無法手動設置id的問題及解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • java 實現定時的方法及實例代碼

    java 實現定時的方法及實例代碼

    這篇文章主要介紹了java 定時任務詳細介紹及實例代碼的相關資料,需要的朋友可以參考下
    2017-03-03

最新評論