午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

SpringBoot實(shí)現(xiàn)異步事件驅(qū)動(dòng)的方法

瀏覽:151日期:2023-03-01 08:04:44
目錄Spring事件驅(qū)動(dòng)源碼實(shí)戰(zhàn)

在項(xiàng)目實(shí)際開(kāi)發(fā)過(guò)程中,我們有很多這樣的業(yè)務(wù)場(chǎng)景:一個(gè)事務(wù)中處理完一個(gè)業(yè)務(wù)邏輯后需要跟著處理另外一個(gè)業(yè)務(wù)邏輯,偽碼大致如下:

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

很簡(jiǎn)單并且很常見(jiàn)的一段業(yè)務(wù)邏輯:首先將產(chǎn)品先保存數(shù)據(jù)庫(kù),然后發(fā)送通知。

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

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

隨著業(yè)務(wù)需求的變化,代碼也需要跟著一遍遍的修改。而且還會(huì)存在另外一個(gè)問(wèn)題,如果通知系統(tǒng)掛了,那就不能再新增產(chǎn)品了。

對(duì)于上面這種情況非常適合引入消息中間件(消息隊(duì)列)來(lái)對(duì)業(yè)務(wù)進(jìn)行解耦,但并非所有的業(yè)務(wù)系統(tǒng)都會(huì)引入消息中間件(引入會(huì)第三方架構(gòu)組件會(huì)帶來(lái)很大的運(yùn)維成本)。

Spring提供了事件驅(qū)動(dòng)機(jī)制可以幫助我們實(shí)現(xiàn)這一需求。

Spring事件驅(qū)動(dòng)

spring事件驅(qū)動(dòng)由3個(gè)部分組成

ApplicationEvent:表示事件本身,自定義事件需要繼承該類,用來(lái)定義事件 ApplicationEventPublisher:事件發(fā)送器,主要用來(lái)發(fā)布事件 ApplicationListener:事件監(jiān)聽(tīng)器接口,監(jiān)聽(tīng)類實(shí)現(xiàn)ApplicationListener 里onApplicationEvent方法即可,也可以在方法上增加@EventListener以實(shí)現(xiàn)事件監(jiān)聽(tīng)。

實(shí)現(xiàn)Spring事件驅(qū)動(dòng)一般只需要三步:

自定義需要發(fā)布的事件類,需要繼承ApplicationEvent類 使用ApplicationEventPublisher來(lái)發(fā)布自定義事件 使用@EventListener來(lái)監(jiān)聽(tīng)事件

這里需要特別注意一點(diǎn),默認(rèn)情況下事件是同步的。即事件被publish后會(huì)等待Listener的處理。如果發(fā)布事件處的業(yè)務(wù)存在事務(wù),監(jiān)聽(tīng)器處理也會(huì)在相同的事務(wù)中。如果需要異步處理事件,可以onApplicationEvent方法上加@Aync支持異步或在有@EventListener的注解方法上加上@Aync。

源碼實(shí)戰(zhàn)

創(chuàng)建事件

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

發(fā)布事件

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

事件監(jiān)聽(tīng)

@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啟動(dòng)類上增加@EnableAsync 注解

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

使用了Async后會(huì)使用默認(rèn)的線程池SimpleAsyncTaskExecutor,一般我們會(huì)在項(xiàng)目中自定義一個(gè)線程池。

@Configurationpublic class ExecutorConfig { /** 核心線程數(shù) */ private int corePoolSize = 10; /** 最大線程數(shù) */ private int maxPoolSize = 50; /** 隊(duì)列大小 */ private int queueCapacity = 10; /** 線程最大空閑時(shí)間 */ 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:當(dāng)pool已經(jīng)達(dá)到max size的時(shí)候,如何處理新任務(wù)// CALLER_RUNS:不在新線程中執(zhí)行任務(wù),而是由調(diào)用者所在的線程來(lái)執(zhí)行executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());executor.initialize();return executor; }}

到此這篇關(guān)于SpringBoot實(shí)現(xiàn)異步事件驅(qū)動(dòng)的方法的文章就介紹到這了,更多相關(guān)SpringBoot 異步事件驅(qū)動(dòng)內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 国产视频一区二区三区四区五区 | www四虎com| 国产50页 | 国产精品成av人在线视午夜片 | 在线观看中文字幕亚洲 | 538国产视频 | 国外精品网站 | 日韩在线播放视频 | 国产免费自拍 | 午夜av毛片 | 波多野结衣久久精品 | 日本一区视频在线 | 男人的午夜天堂 | 欧美国产精品一二三 | 国产精品久久久久久久成人午夜 | 国产一级片a | a一级黄色| 麻豆久久久久久 | 日韩经典在线 | 日日狠狠久久偷偷四色综合免费 | 中文av在线播放 | 蜜臀av性久久久久蜜臀av麻豆 | 久久国产一区 | 日韩精品久久久久久久的张开腿让 | 国产在线久 | 中文字幕99| 丁香激情五月 | 亚洲男人影院 | 精品亚洲精品 | 久久精品一区二区 | 日韩精品在线观看免费 | 国产免费美女视频 | 国产三级精品三级观看 | 男人天堂2014 | 成人高清视频在线观看 | 日韩成人精品在线 | 在线亚洲自拍 | 久久丁香| 伊人影院中文字幕 | 国产在线二区 | 国内外成人在线视频 |