簡體   English   中英

創建osgi實用程序包的用處

[英]how useful is to create an osgi utility bundle

我正在嘗試使用OSGi框架開發一個簡單的應用程序。 我的問題涉及框架中可用的“實用程序包”:讓我用一個非常詳細的示例進行解釋。 目前,我正在嘗試建立一個捆綁發送的事件。

據我了解,我需要做如下事情( event admin felix ):

public void reportGenerated(Report report, BundleContext context)
    {
        ServiceReference ref = context.getServiceReference(EventAdmin.class.getName());
        if (ref != null)
        {
            EventAdmin eventAdmin = (EventAdmin) context.getService(ref);

            Dictionary properties = new Hashtable();
            properties.put("title", report.getTitle());
            properties.put("path" , report.getAbsolutePath());
            properties.put("time", System.currentTimeMillis());

            Event reportGeneratedEvent = new Event("com/acme/reportgenerator/GENERATED", properties);

            eventAdmin.sendEvent(reportGeneratedEvent);
        }
    }

現在,由於OSGi應用程序可能有很多捆綁包,因此我想為每個捆綁包創建Event的子類(例如,我有一個名為“ BundleExample”的捆綁包?在其導出的類中將有一個“ BundleExampleEvent”)。 知道這不會添加任何信息,因為您可以通過查看“主題”來了解收到的事件,但是請耐心等待

現在, Event構造函數需要一個主題和一個Map<String, Object> 但是,為了“簡化”事件構造函數,我只想將主題和參數列表放入映射中。 例如,這可能是BundleExampleEvent類:

public class BundleExampleEvent extends Event{

    private int importantVariable;

    public BundleExampleEvent(String topic, int importantVariable) {
        super(topic, Utils.toMap("importantVariable", importantVariable));
        //here toMap is static
    }

    public int getImportantVariable() {
        return this.importantVariable;
    }   
}

好的,請注意Utils.toMap :這是一個允許您將String, Object序列轉換為Map的函數。 好的,現在Utils是實用程序類的一個示例(愚蠢,無用但仍然是實用程序類)。 本着OSGi的精神,我也希望使該實用程序類成為捆綁軟件 :我的想法是在框架啟動時啟動此Utils捆綁軟件,然后每當我需要其實用工具之一時,我都希望通過@Reference批注獲取引用。

可以在任何bundle接口實現中很好地工作,如下所示:

@Component
public class BundleExampleImpl implements BundleExample {
   @Reference
   private Utils utils;

   @Override
   public String sayHello() {
      return this.utils.fetchHello();
      //another useless utility function, but hopefully it conveys what i'm trying to do
   }
}

但是其他類(例如BundleExampleImpl在其工作期間調用)又如何呢? 例如, BundleExampleEvent怎么BundleExampleEvent 我需要從sayHello方法調用它,並且我想在該類中也使用此實用程序來計算Map! 在前面的示例中,我使用了靜態函數,但是我想使用Utils OSGi給我的引用。

當然,我可以在BundleExampleEvent的構造函數中添加一個參數以滿足鏈接的要求,但我不想這樣做,因為有些東西依賴於“實用程序類”,這很愚蠢。 我的問題是:

  1. 如果我需要“實用程序捆綁包”,這是唯一可用的方法嗎?
  2. 或者我可以做一些奇怪的事情,比如在BundleExampleEvent添加Utils的引用; 即這樣的事情:

     public class BundleExampleEvent extends Event{ @Reference private Utils utils; private int importantVariable; public BundleExampleEvent(String topic, int importantVariable) { super(topic, Utils.toMap("importantVariable", importantVariable)); //here toMap is static } public int getImportantVariable() { return this.importantVariable; } } 
  3. 也許擁有“公用事業捆綁包”的整個想法只是純粹的垃圾?

感謝您的任何答復。 希望我能以最清晰的方式傳達我的問題

我認為Utils作為服務沒有任何意義。 如果事物可以想象有多個實現,那么它們應該僅是一種服務。 在您的情況下,Util功能的使用者只需要一個實現...實現就是合同。

我什至不認為utils代碼應該捆綁在一起。 只需將其放入靜態鏈接到需要它的包的庫中即可。

在您的情況下,Utils utils將是OSGi服務。 然后,您想在不是BundleExampleEvent之類的服務的對象內部使用此服務。

您可以做的是創建一個服務,該服務創建BundleExampleEvent實例並將其提供給OSGi服務。 有點像工廠服務。 問題在於OSGi中的服務是動態的。 如果BundleExampleEvent實例所需的服務消失,則必須丟棄該對象。 因此,這僅適用於短暫的對象。

在eventadmin示例中,另一種解決方案是不使用特殊事件類,而是創建一個具有發送此類事件的方法的服務。 然后,所有魔術將在此方法內發生,結果將是沒有進一步邏輯的事件。 您也可以使用DS將EventAdmin注入該服務。 這在OSGI中效果很好,但缺點是貧血域模型( http://www.martinfowler.com/bliki/AnemicDomainModel.html )。

我不確定要選擇哪種變體。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM