ThingsBoard 二次開發之源碼分析 1- 基礎知識


thingsboard聚集地

Thingsboard 話題討論區:https://forum.iotschool.com/topics/node8

歡迎大家加入thingsboard 二次開發討論群:121202538

thingsboard交流QQ群 121202538

ThingsBoard源碼分析1-基礎知識

預備知識

  • JDK1.8

  • 注解

  • Protobuf

  • Guava

JDK1.8

  • Lambda表達式

  • 方法引用

    方法引用的唯一用途是支持Lambda表達式的簡寫,調用方法的時候使用::, 對於一些單個參數,可以自動推斷;

  • Consumer

    Consumer的作用是給定義一個參數,對其進行(消費)處理,處理的方式可以是任意操作,無返回值;

  • Predicate

    斷言接口,根據傳入的Lambda表達式返回boolean;

  • Supplier

    根據提供的Lambda表達式返回需要的對象;

  • Function

    函數式編程接口,根據提供的Lambda表達式進行相應的操作並返回結果;

注解

  • @PostConstruct

    @postConstruct 注解並非為spring提供, 該注解用來修飾非靜態void()方法,該注解的執行時機為在對象加載完依賴注入后執行,即Constructor > @Autowired > @postConstruct;

  • @EventListener

    由spring提供,spring為我們提供了事件的監聽與實現,內部的實現原理是觀察者設計模式,實現了系統解耦,時間發布者不需要考慮誰在監聽,發布者只關心自己消息的發布;

  • @Conditional及包括其子注解

    spring提供,對滿足條件進行注入;

  • @Builder

    lombok插件提供,目的是簡化構造者模式的代碼。Builder Pattern可輕松創建復雜對象;

Protobuf

protocol buffers 是一種語言無關、平台無關、可擴展的序列化結構數據的方法,它可用於(數據)通信協議、數據存儲等。相比JSON,XML占用內存小,解析速度更快。在使用過程中,首先創建xx.proto文件,通過protobuf-maven-plugin創建相應的類。

Guava

Guava 是一組來自谷歌的核心Java庫,其中包括新的集合類型、不可變集合、一個圖庫,以及用於並發、I/O、散列、緩存、原語、字符串等的實用工具。

  • ListenalbeFuture

    ListenalbeFuture是對JDK的future進行增強,可以監聽任務的執行狀況:

    1. 使用MoreExecutors創建線程池

      ListeningExecutorService  executorService = MoreExecutors.listeningDecorator(Executors.newCachedThreadPool());
      
    2. 提交任務

       final ListenableFuture<Integer> listenableFuture = executorService.submit(new Callable<Integer>() {
                  public Integer call() throws Exception {
                      System.out.println("call execute..");
                      TimeUnit.SECONDS.sleep(3);
                      return 7;
                  }
              });
      
    3. 添加監聽任務執行狀態①

      		listenableFuture.addListener(()->{
                  try {
                      System.out.println(listenableFuture.get());
                  } catch (InterruptedException e) {
                      e.printStackTrace();
                  } catch (ExecutionException e) {
                      e.printStackTrace();
                  }
              },executorService);
      
    4. 添加監聽任務執行狀態②

        Futures.addCallback(listenableFuture, new FutureCallback<Integer>() {
                  @Override
                  public void onSuccess(@Nullable Integer integer) {
      						//返回future的執行結果
                  }
                  @Override
                  public void onFailure(Throwable throwable) {
      
                  }
              }, executorService);
      
  • Futures.transform

    如果需要對返回值做處理,可以使用Futures.transform方法,它是同步方法,另外還有一個異步方法Futures.transformAsync:

    			ListenableFuture<String> future = executorService.submit(() -> "hello, future");
          ListenableFuture<Integer> future2 = Futures.transform(future2, String::length, executorService);
         //future2返回的是’hello, future‘的長度
    
  • SettableFuture

    SettableFuture可以認為是一種異步轉同步工具,可以它在指定時間內獲取ListenableFuture的計算結果:

    SettableFuture<Integer> settableFuture = SettableFuture.create();
    ListenableFuture<Integer> future11 = executorService.submit(() -> {
        int sum = 5 + 6;
        settableFuture.set(sum);
        return sum;
    });
    // get設置超時時間 
    System.out.println(settableFuture.get(2, TimeUnit.SECONDS));
    


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM