亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

SpringBoot實現異步事件驅動的方法

瀏覽:63日期:2023-03-01 08:04:44
目錄Spring事件驅動源碼實戰

在項目實際開發過程中,我們有很多這樣的業務場景:一個事務中處理完一個業務邏輯后需要跟著處理另外一個業務邏輯,偽碼大致如下:

@Servicepublic class ProductServiceImpl { ... public void saveProduct(Product product) {productMapper.saveOrder(product);notifyService.notify(product); } ...}

很簡單并且很常見的一段業務邏輯:首先將產品先保存數據庫,然后發送通知。

某一天你們可能需要把新增的產品存到Es中,這時候也需要代碼可能變成這樣:

@Servicepublic class ProductServiceImpl { ... public void saveProduct(Product product) {productMapper.saveProduct(product);esService.saveProduct(product)notifyService.notify(product); } ...}

隨著業務需求的變化,代碼也需要跟著一遍遍的修改。而且還會存在另外一個問題,如果通知系統掛了,那就不能再新增產品了。

對于上面這種情況非常適合引入消息中間件(消息隊列)來對業務進行解耦,但并非所有的業務系統都會引入消息中間件(引入會第三方架構組件會帶來很大的運維成本)。

Spring提供了事件驅動機制可以幫助我們實現這一需求。

Spring事件驅動

spring事件驅動由3個部分組成

ApplicationEvent:表示事件本身,自定義事件需要繼承該類,用來定義事件 ApplicationEventPublisher:事件發送器,主要用來發布事件 ApplicationListener:事件監聽器接口,監聽類實現ApplicationListener 里onApplicationEvent方法即可,也可以在方法上增加@EventListener以實現事件監聽。

實現Spring事件驅動一般只需要三步:

自定義需要發布的事件類,需要繼承ApplicationEvent類 使用ApplicationEventPublisher來發布自定義事件 使用@EventListener來監聽事件

這里需要特別注意一點,默認情況下事件是同步的。即事件被publish后會等待Listener的處理。如果發布事件處的業務存在事務,監聽器處理也會在相同的事務中。如果需要異步處理事件,可以onApplicationEvent方法上加@Aync支持異步或在有@EventListener的注解方法上加上@Aync。

源碼實戰

創建事件

public class ProductEvent extends ApplicationEvent { public ProductEvent(Product product) {super(product); }}

發布事件

@Servicepublic class ProductServiceImpl implements IproductService { ... @Autowired private ApplicationEventPublisher publisher; @Override @Transactional(rollbackFor = Exception.class) public void saveProduct(Product product) { productMapper.saveProduct(product); //事件發布publisher.publishEvent(product); } ...}

事件監聽

@Slf4j@AllArgsConstructorpublic class ProductListener { private final NotifyService notifyServcie; @Async @Order @EventListener(ProductEvent.class) public void notify(ProductEvent event) { Product product = (Product) event.getSource(); notifyServcie.notify(product, 'product'); }}

在SpringBoot啟動類上增加@EnableAsync 注解

@Slf4j@EnableSwagger2@SpringBootApplication@EnableAsyncpublic class ApplicationBootstrap {...}

使用了Async后會使用默認的線程池SimpleAsyncTaskExecutor,一般我們會在項目中自定義一個線程池。

@Configurationpublic class ExecutorConfig { /** 核心線程數 */ private int corePoolSize = 10; /** 最大線程數 */ private int maxPoolSize = 50; /** 隊列大小 */ private int queueCapacity = 10; /** 線程最大空閑時間 */ private int keepAliveSeconds = 150; @Bean('customExecutor') public Executor myExecutor() {ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();executor.setCorePoolSize(corePoolSize);executor.setMaxPoolSize(maxPoolSize);executor.setQueueCapacity(queueCapacity);executor.setThreadNamePrefix('customExecutor-');executor.setKeepAliveSeconds(keepAliveSeconds);// rejection-policy:當pool已經達到max size的時候,如何處理新任務// CALLER_RUNS:不在新線程中執行任務,而是由調用者所在的線程來執行executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());executor.initialize();return executor; }}

到此這篇關于SpringBoot實現異步事件驅動的方法的文章就介紹到這了,更多相關SpringBoot 異步事件驅動內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
主站蜘蛛池模板: 快射视频网 | 自偷自偷自亚洲永久 | 亚洲日韩视频免费观看 | 免费一级毛片在播放视频 | 中文字幕亚洲综合久久202 | 国内自拍视频在线看免费观看 | 国产精品二 | 38pao强力打造永久免费高清视频 | 亚洲综合二区 | 午夜男男xx00视频免费 | 4388x17亚洲最大成人网 | 夜夜爽影院 | 中国黄色片一级 | 一级女性黄色生活片 | 国产成人亚洲精品老王 | 欧美在线一级毛片视频 | 黄色一级在线 | 欧美日韩国产高清 | 久久国产一区二区 | 在线观看91精品国产入口 | 久久免费看视频 | 亚洲精品色婷婷在线影院麻豆 | 国产美女精品三级在线观看 | 伊人久久中文字幕 | 精品老司机在线视频香蕉 | 国产一二三四区中 | 一级黄色免费观看 | 美女三级在线 | 毛片大全在线 | 91网站在线免费观看 | 91一区二区午夜免费福利网站 | 国产精品拍拍拍福利在线观看 | 达达兔午夜起神影院在线观看麻烦 | 日韩欧美第一页 | 男女晚上爱爱的视频在线观看 | 亚洲成网站www久久九 | 欧美黄色大片 | 精品免费视在线视频观看 | 在线精品国内视频秒播 | 免费观看三级毛片 | 在线高清视频18jin观看 |