<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Spring Event,賊好用的業(yè)務(wù)解耦神器!

          共 10413字,需瀏覽 21分鐘

           ·

          2022-07-23 15:59

          關(guān)注我們,設(shè)為星標(biāo),每天7:40不見不散,架構(gòu)路上與您共享

          回復(fù)架構(gòu)師獲取資源


          大家好,我是你們的朋友架構(gòu)君,一個(gè)會(huì)寫代碼吟詩的架構(gòu)師。

          'javajgs.com';


          目錄


          • 前言

          • Spring Event 同步使用

          • Spring Event 異步使用



          前言



          實(shí)際業(yè)務(wù)開發(fā)過程中,業(yè)務(wù)邏輯可能非常復(fù)雜,核心業(yè)務(wù) + N 個(gè)子業(yè)務(wù)。如果都放到一塊兒去做,代碼可能會(huì)很長,耦合度不斷攀升,維護(hù)起來也麻煩,甚至頭疼。還有一些業(yè)務(wù)場景不需要在一次請求中同步完成,比如郵件發(fā)送、短信發(fā)送等。

          MQ 確實(shí)可以解決這個(gè)問題,但 MQ 重啊,非必要不提升架構(gòu)復(fù)雜度。針對這些問題,我們了解一下 Spring Event。

          Spring Event 同步使用


          Spring Event(Application Event)其實(shí)就是一個(gè)觀察者設(shè)計(jì)模式,一個(gè) Bean 處理完成任務(wù)后希望通知其它 Bean 或者說一個(gè) Bean 想觀察監(jiān)聽另一個(gè)Bean 的行為。

          Spring Event 用來解耦業(yè)務(wù)真的賊好用!

          Demo 地址:
          https://gitee.com/csps/mingyue-springboot-learning

          | 自定義事件

          定義事件,繼承 ApplicationEvent 的類成為一個(gè)事件類:

          import lombok.Data;
          import lombok.ToString;
          import org.springframework.context.ApplicationEvent;

          /**
           * @author Strive
           * @date 2022/4/22 18:00
           * @description
           */

          @Data
          @ToString
          public class OrderProductEvent extends ApplicationEvent {

            /** 該類型事件攜帶的信息 */
            private String orderId;

            public OrderProductEvent(Object source, String orderId) {
              super(source);
              this.orderId = orderId;
            }
          }

          | 定義監(jiān)聽器

          監(jiān)聽并處理事件,實(shí)現(xiàn) ApplicationListener 接口或者使用 @EventListener 注解:

          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
           */

          @Slf4j
          @Component
          public class OrderProductListener implements ApplicationListener<OrderProductEvent{

            /** 使用 onApplicationEvent 方法對消息進(jìn)行接收處理 */
            @SneakyThrows
            @Override
            public void onApplicationEvent(OrderProductEvent event) {
              String orderId = event.getOrderId();
              long start = System.currentTimeMillis();
              Thread.sleep(2000);
              long end = System.currentTimeMillis();
              log.info("{}:校驗(yàn)訂單商品價(jià)格耗時(shí):({})毫秒", orderId, (end - start));
            }
          }

          | 定義發(fā)布者

          發(fā)布事件,通過 ApplicationEventPublisher 發(fā)布事件:

          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
           */

          @Slf4j
          @Service
          @RequiredArgsConstructor
          public class OrderService {

            /** 注入ApplicationContext用來發(fā)布事件 */
            private final ApplicationContext applicationContext;

            /**
             * 下單
             *
             * @param orderId 訂單ID
             */

            public String buyOrder(String orderId) {
              long start = System.currentTimeMillis();
              // 1.查詢訂單詳情

              // 2.檢驗(yàn)訂單價(jià)格 (同步處理)
              applicationContext.publishEvent(new OrderProductEvent(this, orderId));

              // 3.短信通知(異步處理)

              long end = System.currentTimeMillis();
              log.info("任務(wù)全部完成,總耗時(shí):({})毫秒", end - start);
              return "購買成功";
            }
          }

          | 單測執(zhí)行

          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
           */

          @SpringBootTest
          public class OrderServiceTest {
            @Autowired private OrderService orderService;

            @Test
            public void buyOrderTest() {
              orderService.buyOrder("732171109");
            }
          }

          執(zhí)行結(jié)果如下:

          2022-04-24 10:13:17.535  INFO 44272 --- [           main] c.c.m.e.listener.OrderProductListener    : 732171109:校驗(yàn)訂單商品價(jià)格耗時(shí):(2008)毫秒
          2022-04-24 10:13:17.536  INFO 44272 --- [           main] c.c.mingyue.event.service.OrderService   : 任務(wù)全部完成,總耗時(shí):(2009)毫秒

          Spring Event 異步使用


          有些業(yè)務(wù)場景不需要在一次請求中同步完成,比如郵件發(fā)送、短信發(fā)送等。

          | 自定義事件

          import lombok.AllArgsConstructor;
          import lombok.Data;

          /**
           * @author Strive
           * @date 2022/4/24 10:18
           * @description
           */

          @Data
          @AllArgsConstructor
          public class MsgEvent {

            /** 該類型事件攜帶的信息 */
            public String orderId;
          }

          | 定義監(jiān)聽器

          推薦使用 @EventListener 注解:

          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
           */

          @Slf4j
          @Component
          public class MsgListener {

            @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ā)送短信、郵件耗時(shí):({})毫秒", orderId, (end - start));
            }
          }

          | 定義發(fā)布者

          /**
            * 下單
            *
            * @param orderId 訂單ID
            */

          public String buyOrder(String orderId) {
              long start = System.currentTimeMillis();
              // 1.查詢訂單詳情

              // 2.檢驗(yàn)訂單價(jià)格 (同步處理)
              applicationContext.publishEvent(new OrderProductEvent(this, orderId));

              // 3.短信通知(異步處理)
              applicationContext.publishEvent(new MsgEvent(orderId));

              long end = System.currentTimeMillis();
              log.info("任務(wù)全部完成,總耗時(shí):({})毫秒", end - start);
              return "購買成功";
          }

          | 單測執(zhí)行(同步)

          @Test
          public void buyOrderTest() {
              orderService.buyOrder("732171109");
          }

          執(zhí)行結(jié)果如下:

          2022-04-24 10:24:13.905  INFO 54848 --- [           main] c.c.m.e.listener.OrderProductListener    : 732171109:校驗(yàn)訂單商品價(jià)格耗時(shí):(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ā)送短信、郵件耗時(shí):(4002)毫秒
          2022-04-24 10:24:17.908  INFO 54848 --- [           main] c.c.mingyue.event.service.OrderService   : 任務(wù)全部完成,總耗時(shí):(6008)毫秒

          | 開啟異步

          啟動(dòng)類增加 @EnableAsync 注解:

          @EnableAsync
          @SpringBootApplication
          public class MingYueSpringbootEventApplication {

            public static void main(String[] args) {
              SpringApplication.run(MingYueSpringbootEventApplication.class, args);
            }
          }

          Listener 類需要開啟異步的方法增加 @Async 注解:

          @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ā)送短信、郵件耗時(shí):({})毫秒", orderId, (end - start));
          }

          | 單測執(zhí)行(異步)

          發(fā)送短信的線程顯示 task-1,主線程結(jié)束后(總耗時(shí):(2017)毫秒)控制臺(tái)停止打印了:
          2022-04-24 10:30:59.002  INFO 59448 --- [           main] c.c.m.e.listener.OrderProductListener    : 732171109:校驗(yàn)訂單商品價(jià)格耗時(shí):(2009)毫秒
          2022-04-24 10:30:59.009  INFO 59448 --- [           main] c.c.mingyue.event.service.OrderService   : 任務(wù)全部完成,總耗時(shí):(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ā)送郵件


          到此文章就結(jié)束了。Java架構(gòu)師必看一個(gè)集公眾號(hào)、小程序、網(wǎng)站(3合1的文章平臺(tái),給您架構(gòu)路上一臂之力,javajgs.com)。如果今天的文章對你在進(jìn)階架構(gòu)師的路上有新的啟發(fā)和進(jìn)步,歡迎轉(zhuǎn)發(fā)給更多人。歡迎加入架構(gòu)師社區(qū)技術(shù)交流群,眾多大咖帶你進(jìn)階架構(gòu)師,在后臺(tái)回復(fù)“加群”即可入群。



          這些年小編給你分享過的干貨


          1.idea永久激活碼(親測可用)

          2.優(yōu)質(zhì)ERP系統(tǒng)帶進(jìn)銷存財(cái)務(wù)生產(chǎn)功能(附源碼)

          3.優(yōu)質(zhì)SpringBoot帶工作流管理項(xiàng)目(附源碼)

          4.最好用的OA系統(tǒng),拿來即用(附源碼)

          5.SBoot+Vue外賣系統(tǒng)前后端都有(附源碼

          6.SBoot+Vue可視化大屏拖拽項(xiàng)目(附源碼)


          轉(zhuǎn)發(fā)在看就是最大的支持??

          瀏覽 49
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評論
          圖片
          表情
          推薦
          點(diǎn)贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  久久久一区二区三区四区 | 欧美黄色日逼视频 | 久久艹精品视频 | 学生妹毛毛片 | 国产 日韩 欧美视频 |