Spring Event,賊好用的業(yè)務(wù)解耦神器!
閱讀本文大概需要 5 分鐘。
import lombok.Data;import lombok.ToString;import org.springframework.context.ApplicationEvent;/*** @author Strive* @date 2022/4/22 18:00* @description*/@Data@ToStringpublic class OrderProductEvent extends ApplicationEvent {/** 該類型事件攜帶的信息 */private String orderId;public OrderProductEvent(Object source, String orderId) {super(source);this.orderId = orderId;}}
import com.csp.mingyue.event.events.OrderProductEvent;import lombok.SneakyThrows;import lombok.extern.slf4j.Slf4j;import org.springframework.context.ApplicationListener;import org.springframework.stereotype.Component;/*** 實(shí)現(xiàn) ApplicationListener 接口,并指定監(jiān)聽的事件類型** @author Strive* @date 2022/4/24 09:09* @description*/4j@Componentpublic class OrderProductListener implements ApplicationListener<OrderProductEvent> {/** 使用 onApplicationEvent 方法對消息進(jìn)行接收處理 */public void onApplicationEvent(OrderProductEvent event) {String orderId = event.getOrderId();long start = System.currentTimeMillis();Thread.sleep(2000);long end = System.currentTimeMillis();log.info("{}:校驗(yàn)訂單商品價格耗時:({})毫秒", orderId, (end - start));}}
import com.csp.mingyue.event.events.OrderProductEvent;import lombok.RequiredArgsConstructor;import lombok.extern.slf4j.Slf4j;import org.springframework.context.ApplicationContext;import org.springframework.stereotype.Service;/*** @author Strive* @date 2022/4/24 09:25* @description*/4j@Service@RequiredArgsConstructorpublic class OrderService {/** 注入ApplicationContext用來發(fā)布事件 */private final ApplicationContext applicationContext;/*** 下單** @param orderId 訂單ID*/public String buyOrder(String orderId) {long start = System.currentTimeMillis();// 1.查詢訂單詳情// 2.檢驗(yàn)訂單價格 (同步處理)applicationContext.publishEvent(new OrderProductEvent(this, orderId));// 3.短信通知(異步處理)long end = System.currentTimeMillis();log.info("任務(wù)全部完成,總耗時:({})毫秒", end - start);return "購買成功";}}
import org.junit.jupiter.api.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;/*** @author Strive* @date 2022/4/24 09:28* @description*/@SpringBootTestpublic class OrderServiceTest {@Autowired private OrderService orderService;@Testpublic void buyOrderTest() {orderService.buyOrder("732171109");}}
2022-04-24 10:13:17.535 INFO 44272 --- [ main] c.c.m.e.listener.OrderProductListener : 732171109:校驗(yàn)訂單商品價格耗時:(2008)毫秒2022-04-24 10:13:17.536 INFO 44272 --- [ main] c.c.mingyue.event.service.OrderService : 任務(wù)全部完成,總耗時:(2009)毫秒
import lombok.AllArgsConstructor;import lombok.Data;/*** @author Strive* @date 2022/4/24 10:18* @description*/@Data@AllArgsConstructorpublic class MsgEvent {/** 該類型事件攜帶的信息 */public String orderId;}
import com.csp.mingyue.event.events.MsgEvent;import lombok.SneakyThrows;import lombok.extern.slf4j.Slf4j;import org.springframework.context.event.EventListener;import org.springframework.stereotype.Component;/*** @author Strive* @date 2022/4/24 10:20* @description*/4j@Componentpublic class MsgListener {(MsgEvent.class)public void sendMsg(MsgEvent event) {String orderId = event.getOrderId();long start = System.currentTimeMillis();log.info("開發(fā)發(fā)送短信");log.info("開發(fā)發(fā)送郵件");Thread.sleep(4000);long end = System.currentTimeMillis();log.info("{}:發(fā)送短信、郵件耗時:({})毫秒", orderId, (end - start));}}
/*** 下單** @param orderId 訂單ID*/public String buyOrder(String orderId) {long start = System.currentTimeMillis();// 1.查詢訂單詳情// 2.檢驗(yàn)訂單價格 (同步處理)applicationContext.publishEvent(new OrderProductEvent(this, orderId));// 3.短信通知(異步處理)applicationContext.publishEvent(new MsgEvent(orderId));long end = System.currentTimeMillis();log.info("任務(wù)全部完成,總耗時:({})毫秒", end - start);return "購買成功";}
@Testpublic void buyOrderTest() {orderService.buyOrder("732171109");}
2022-04-24 10:24:13.905 INFO 54848 --- [ main] c.c.m.e.listener.OrderProductListener : 732171109:校驗(yàn)訂單商品價格耗時:(2004)毫秒2022-04-24 10:24:13.906 INFO 54848 --- [ main] c.c.mingyue.event.listener.MsgListener : 開發(fā)發(fā)送短信2022-04-24 10:24:13.907 INFO 54848 --- [ main] c.c.mingyue.event.listener.MsgListener : 開發(fā)發(fā)送郵件2022-04-24 10:24:17.908 INFO 54848 --- [ main] c.c.mingyue.event.listener.MsgListener : 732171109:發(fā)送短信、郵件耗時:(4002)毫秒2022-04-24 10:24:17.908 INFO 54848 --- [ main] c.c.mingyue.event.service.OrderService : 任務(wù)全部完成,總耗時:(6008)毫秒
@EnableAsync@SpringBootApplicationpublic class MingYueSpringbootEventApplication {public static void main(String[] args) {SpringApplication.run(MingYueSpringbootEventApplication.class, args);}}
@Async@SneakyThrows@EventListener(MsgEvent.class)public void sendMsg(MsgEvent event) {String orderId = event.getOrderId();long start = System.currentTimeMillis();log.info("開發(fā)發(fā)送短信");log.info("開發(fā)發(fā)送郵件");Thread.sleep(4000);long end = System.currentTimeMillis();log.info("{}:發(fā)送短信、郵件耗時:({})毫秒", orderId, (end - start));}
2022-04-24 10:30:59.002 INFO 59448 --- [ main] c.c.m.e.listener.OrderProductListener : 732171109:校驗(yàn)訂單商品價格耗時:(2009)毫秒2022-04-24 10:30:59.009 INFO 59448 --- [ main] c.c.mingyue.event.service.OrderService : 任務(wù)全部完成,總耗時:(2017)毫秒2022-04-24 10:30:59.028 INFO 59448 --- [ task-1] c.c.mingyue.event.listener.MsgListener : 開發(fā)發(fā)送短信2022-04-24 10:30:59.028 INFO 59448 --- [ task-1] c.c.mingyue.event.listener.MsgListener : 開發(fā)發(fā)
推薦閱讀:
Spring Security 實(shí)現(xiàn)動態(tài)權(quán)限菜單方案(附源碼)
互聯(lián)網(wǎng)初中高級大廠面試題(9個G) 內(nèi)容包含Java基礎(chǔ)、JavaWeb、MySQL性能優(yōu)化、JVM、鎖、百萬并發(fā)、消息隊(duì)列、高性能緩存、反射、Spring全家桶原理、微服務(wù)、Zookeeper......等技術(shù)棧!
?戳閱讀原文領(lǐng)取! 朕已閱
評論
圖片
表情


