詳解SpringBoot 發(fā)布ApplicationEventPublisher和監(jiān)聽ApplicationEvent事件
資料地址
實(shí)現(xiàn)方法
- 自定義需要發(fā)布的事件類,需要繼承ApplicationEvent類或PayloadApplicationEvent<T>(該類也僅僅是對(duì)ApplicationEvent的一層封裝)
- 使用@EventListener來(lái)監(jiān)聽事件
- 使用ApplicationEventPublisher來(lái)發(fā)布自定義事件(@Autowired注入即可)
/** * 自定義保存事件 * @author peter * 2019/1/27 14:59 */ public class PersonSaveEvent<DATA> extends ApplicationEvent { private DATA data; public PersonSaveEvent(DATA source) { super(source); this.data = source; } public DATA getData() { return data; } } //發(fā)布事件 public void savePerson(Person person){ personDao.save(person); publisher.publishEvent(new PersonSaveEvent<>(1)); } //監(jiān)聽事件 @EventListener public void listenEvent(PersonSaveEvent<Integer> event) { System.out.println("監(jiān)聽到PersonSaveEvent事件; 接收到的值:" + event.getData() + ";發(fā)布的時(shí)間為" + Instant.ofEpochMilli(event.getTimestamp())); }
好處
可以使核心業(yè)務(wù)與子業(yè)務(wù)進(jìn)行解耦,也方便后期的業(yè)務(wù)的擴(kuò)展。如新用戶注冊(cè)之后,需要發(fā)放優(yōu)惠券,此時(shí)可以在保存用戶之后,發(fā)布一個(gè)新用戶的注冊(cè)成功事件,通過(guò)監(jiān)聽該事件來(lái)實(shí)現(xiàn)發(fā)放優(yōu)惠券的功能。后期新增一個(gè)對(duì)新用戶進(jìn)行xxx功能,此時(shí)可以新寫一個(gè)監(jiān)聽注冊(cè)成功事件的監(jiān)聽器,來(lái)處理新的業(yè)務(wù)邏輯,而不需要修改之前的注冊(cè)邏輯。
注意事項(xiàng)
1、監(jiān)聽器方法中一定要try-catch異常,否則會(huì)造成發(fā)布事件(有事物的)的方法進(jìn)行回滾
2、可以使用@Order注解來(lái)控制多個(gè)監(jiān)聽器的執(zhí)行順序,@Order傳入的值越小,執(zhí)行順序越高
3、對(duì)于需要進(jìn)行事物監(jiān)聽或不想try-catch runtime異常,可以使用@TransactionalEventListener注解
@TransactionalEventListener 監(jiān)聽器
在該注解的源碼中:
* <p>If the event is not published within the boundaries of a managed transaction, the * event is discarded unless the {@link #fallbackExecution} flag is explicitly set. If a * transaction is running, the event is processed according to its {@code TransactionPhase}.
大意是:如果事件的發(fā)布不是在事物(@Transactional)范圍內(nèi),則監(jiān)聽不到該事件,除非將fallbackExecution標(biāo)志設(shè)置為true(@TransactionalEventListener(fallbackExecution = true));如果在事物中,可以選擇在事物的哪個(gè)階段來(lái)監(jiān)聽事件,默認(rèn)在事物提交后監(jiān)聽。
修改監(jiān)聽事物的范圍:@TransactionalEventListener(phase = TransactionPhase.AFTER_COMPLETION)
在監(jiān)聽器中重新開一個(gè)事物
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMPLETION) public void listenEvent1(PersonSaveEvent<Integer> event) { divide(event); } @Transactional(propagation = Propagation.REQUIRES_NEW) public void divide(PersonSaveEvent<Integer> event) { System.out.println("監(jiān)聽到PersonSaveEvent事件; 接收到的值:" + event.getData() + ";接受的時(shí)間為" + Instant.ofEpochMilli(event.getTimestamp())); }
以上事件都是同步,如果需要異步則需要開啟異步支持,在監(jiān)聽器方法加上@Async 注解即可。
/** * @author peter * @version 1.0 * @date 2019/04/18 08:47 */ @Configuration @EnableAsync public class AsyncEventConfiguration implements AsyncConfigurer { @Override public Executor getAsyncExecutor() { return Executors.newCachedThreadPool(); } }
一旦開始異步執(zhí)行,方法的異常將不會(huì)拋出,只能在方法內(nèi)部處理。如需在方法外處理異常:Async 異常處理在文章最后
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java 8中Collectors.toMap空指針異常源碼解析
這篇文章主要為大家介紹了Java 8中Collectors.toMap空指針異常源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08SpringBoot實(shí)現(xiàn)點(diǎn)餐系統(tǒng)的登錄與退出功能流程詳解
結(jié)束了Springboot+MyBatisPlus也是開始了項(xiàng)目之旅,將從后端的角度出發(fā)來(lái)整理這個(gè)項(xiàng)目中重點(diǎn)業(yè)務(wù)功能的梳理與實(shí)現(xiàn)2022-10-10SpringBoot的@RestControllerAdvice作用詳解
這篇文章主要介紹了SpringBoot的@RestControllerAdvice作用詳解,@RestContrllerAdvice是一種組合注解,由@ControllerAdvice,@ResponseBody組成,本質(zhì)上就是@Component,需要的朋友可以參考下2024-01-01SpringBoot+Eureka實(shí)現(xiàn)微服務(wù)負(fù)載均衡的示例代碼
這篇文章主要介紹了SpringBoot+Eureka實(shí)現(xiàn)微服務(wù)負(fù)載均衡的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11Spring Boot靜態(tài)資源路徑的配置與修改詳解
最近在做SpringBoot項(xiàng)目的時(shí)候遇到了“白頁(yè)”問題,通過(guò)查資料對(duì)SpringBoot訪問靜態(tài)資源做了總結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-09-09java 實(shí)現(xiàn)計(jì)數(shù)排序和桶排序?qū)嵗a
這篇文章主要介紹了java 實(shí)現(xiàn)計(jì)數(shù)排序和桶排序?qū)嵗a的相關(guān)資料,需要的朋友可以參考下2017-02-02