日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

OSGI 生命周期

發布時間:2023/11/30 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 OSGI 生命周期 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1 生命周期管理

對于非模塊化應用,生命周期將應用作為一個整體來操作; 而對于模塊化應用,則可以以細粒度的方式來管理應用的某一個獨立部分。

OSGi生命周期管理

OSGi生命周期層有兩種不同的作用:

  • 在應用程序外部,定義了對bundle生命周期的相關操作。OSGi生命周期層允許在執行時,從外部安裝、啟動、更新、停止、卸載不同的bundle,進而定制應用的配置。
  • 在應用程序內部,定義了bundle訪問其執行上下文的方式,為bundle提供了一種與OSGi框架交互的途徑以及一些執行時的便利條件。

標準Java生命周期管理

安裝:jar下載時被安裝; 執行:用戶啟動JVM時被執行;(雙擊) 更新:替換jar文件; 移除:刪除jar文件。

Servlet生命周期管理

生命周期由servlet容器管理

安裝:通過容器的特定進程被安裝; 執行:servlet容器調用各種生命周期API,如Servlet.init()、Servlet.destroy(); 更新:替換WAR文件; 移除:通過容器的特定進程被移除。

2 生命周期層的三個接口

BundleActivator

如果要對一個bundle進行生命周期管理,必須要在這個bundle中聲明一個bundle激活器類,實現org.osgi.framework.BundleActivator接口。這個接口為bundle提供了掛接到生命周期層的鉤子,同時自定義bundle在啟動或停止時執行的操作。

還必須在MANIFEST.MF中描述該bundle激活器:

?

Bundle-Activator:?org.alpha.MyActivator?? 注意:并非所有bundle都需要一個激活器。只有當明確需要與OSGI API進行交互,或者需要執行自定義的初始化/銷毀動作時,才必須要激活器。
  • 當bundle被安裝并啟動以后,框架將構建相應的BundleActivator的一個實例,觸發BundleActivator.start()
  • 當bundle停止后,框架會調用BundleActivator.stop();stop()方法應該取消start()中執行過的所有操作。

?

public???interface??BundleActivator {??
???public???void??start(BundleContext context)??throws??Exception;??
???public???void??stop(BundleContext context)??throws??Exception;??
}??

?

BundleContext

BundleActivator的兩個方法都接受一個BundleContext對象作為參數。BundleContext是bundle與OSGi framework通信的橋梁。BundleContext可以做的事有:

  • Look up system-wide configuration properties;
    ——獲取系統properties
  • Find another installed bundle by its ID;
    ——通過ID查詢已安裝的其他bundle
  • Obtain a list of all installed bundles;
    ——獲取已安裝的bundle列表
  • Introspect and manipulate other bundles programmatically: start them, stop them, un-install them, update them, etc;
    ——在程序中操作其他bundle(啟動、停止、卸載、更新)
  • Install new bundles programmatically;?
    ——在程序中安裝新bundle
  • Store or retrieve a file in a persistent storage area managed by the framework;?
    ——往框架管理的持久化存儲區中存儲或查詢文件
  • Register and unregister bundle listeners, which tell us when the state of any bundle in the framework changes;?
    ——注冊bundle listener,監聽bundle狀態的改變
  • Register and unregister service listeners, which tell us when the state of any service in the framework changes?
    ——注冊service listener,監聽service狀態的改變?
  • Register and unregister framework listeners, which tell us about general framework events.
    ——注冊framework listener,監聽一般框架事件

每個已激活的bundle都會接收到屬于自己的BundleContext對象,該對象不能在bundle直接自由傳遞!

只有當bundle處于激活狀態(從start()開始,到stop()結束),BundleContext對象才是有效的。其他狀態下調用該對象,會拋出異常。

?

public??interface??BundleContext {??
??String getProperty(String key);??
??Bundle getBundle();??
??Bundle getBundle(?long??id);??
??Bundle[] getBundles();??
??
??Bundle installBundle(String location)??throws??BundleException;??
??Bundle installBundle(String location, InputStream input)??throws??BundleException;??
??
???void??addBundleListener(BundleListener listener);??
???void??removeBundleListener(BundleListener listener);??
???void??addFrameworkListener(FrameworkListener listener);??
???void??removeFrameworkListener(FrameworkListener listener);??
}??

?

Bundle

對于每個已安裝的bundle,框架都會相應地創建一個邏輯上代表他的Bundle對象。Bundle接口定義了一系列API,用于管理已安裝bundle的生命周期。

?

public?interface?Bundle {??
?? BundleContext getBundleContext();??
???long??getBundleId();??
?? Dictionary getHeaders();??
???int??getState();??
?? String getSymbolicName();??
?? Version getVersion();??
??
???void??start(?int??options)??throws??BundleException;??
???void??start()??throws??BundleException;??
???void??stop(?int??options)??throws??BundleException;??
???void??stop()??throws??BundleException;??
???void??update(InputStream input)??throws??BundleException;??
???void??update()??throws??BundleException;??
???void??uninstall()??throws??BundleException;??
}??
注意:BundleId=0的bundle表示OSGi框架本身,稱為系統bundle
當停止系統bundle時,它會首先停止其他bundle,然后才將自身完全關閉;以友好的方式關閉框架。

3 生命周期狀態

INSTALLED

調用BundleContext.installBundle()后,會創建一個INSTALLED狀態的bundle

RESOLVED

如果其所依賴的所有bundle都存在,即解析成功,轉到RESOLVED狀態

注意:不能直接從INSTALLED --> STARTING

STARTING

當開始執行BundleActivator.start(),則處于STARTING狀態;

注意:這是一個瞬時狀態

ACTIVE

當執行BundleActivator.start()成功,則轉到ACTIVE狀態;

如果拋出異常,則回到RESOLVED狀態。

STOPPING

當開始執行BundleActivator.stop(),則處于STOPPING狀態;

如果執行成功,則轉到RESOLVED狀態。 注意:這是一個瞬時狀態

UNINSTALLED

INSTALLED狀態的bundle可以被卸載,轉到UNINSTALLED狀態;

如果卸載一個ACTIVE狀態的bundle,則框架會首先自動停止該bundle,使其轉到RESOLVED狀態;然后再卸載之前,將其狀態轉為INSTALLED。

Although the UNINSTALLED state is shown here, we can never see a bundle in that state, and?an UNINSTALLED bundle cannot transition to any other state. Even if we reinstall the same bundle JAR file, it will be considered a different bundle by the framework, and assigned a new bundle ID.?

刷新/更新

刷新或更新一個bundle,將使其狀態退回INSTALLED狀態!

轉載于:https://www.cnblogs.com/deepbreath/p/4383059.html

總結

以上是生活随笔為你收集整理的OSGI 生命周期的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。