Spring @RabbitListener 原理 -- 個人見解

調用層

  1. RabbitAutoConfiguration // 這個是自動配置引入
  2. RabbitAnnotationDrivenConfiguration
  3. @EnableRabbit
  4. RabbitBootstrapConfiguration
  5. RabbitListenerAnnotationBeanPostProcessor
  6. xx 略
  • 說明:
    • @RabbitListener 註解的類本身沒有被 AOP 代理
    • 只是 Spring 內部記錄下
    • 然後收到消息通過反射調用被註解的方法
    • 類似 SpringMVC 對 @RequestMapping 映射的處理
最終調用
  • SimpleMessageListenerContainer.AsyncMessageProcessingConsumer 循環監聽
    • receiveAndExecute(this.consumer) 類似 socket 阻塞式監聽
    • BlockingQueueConsumer
      • 內部再是通過阻塞隊列,實現生產者、消費者模式
      • 默認 1 秒試拉 1
  • MessagingMessageListenerAdapter 具體調用

測試 AOP 導出

    static void setProxySavePath() throws Exception {
        System.setProperty( "jdk.proxy.ProxyGenerator.saveGeneratedFiles", "true" );
        
        Path path = Paths.get( Consumer1MainApplication.class.getResource( "/" ).toURI() );
        Path savePath = path.resolve( "../export/spring-aop-proxy" )
                .normalize()
                .toAbsolutePath();
        Files.createDirectories( savePath );

        System.out.println( "class 存放路徑:" );
        System.out.println( savePath );
        System.out.println( "----------------------" );

        // 設置將 cglib 生成的代理類字節碼生成到指定位置
        System.setProperty( DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, savePath.toString() );
    }
  • 運行時並沒有輸出對應的代理類

最後

看源碼的一些忠告
  • IDEA 看源碼,查看被調用的地方比較方便
  • Eclipse 看被調用的點時,有時很坑
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章