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状态!

 
 

【OSGi】OSGi生命周期的更多相关文章

  1. OSGi:生命周期层

    前言 生命周期层在OSGi框架中属于模块层上面的一层,它的运作是建立在模块层的功能之上的.生命周期层一个主要的功能就是让你能够从外部管理应用或者建立能够自我管理的应用(或者两者的结合),并且给了应用本 ...

  2. 转:OSGi 入门篇:生命周期层

    OSGi 入门篇:生命周期层 前言 生命周期层在OSGi框架中属于模块层上面的一层,它的运作是建立在模块层的功能之上的.生命周期层一个主要的功能就是让你能够从外部管理应用或者建立能够自我管理的应用(或 ...

  3. osgi实战学习之路:5.生命周期及利用命令、装饰者模式实现基于socket交互Bundle命令demo

    生命周期中关键3个类: BundleActivator 入口点,类似main方法 BundleContext Bundle上下文对象,在执行期间,为应用程序提供操作osgi框架的方法 Bundle 代 ...

  4. OSGI 生命周期

    1 生命周期管理 对于非模块化应用,生命周期将应用作为一个整体来操作: 而对于模块化应用,则可以以细粒度的方式来管理应用的某一个独立部分. OSGi生命周期管理 OSGi生命周期层有两种不同的作用: ...

  5. 深入理解java虚拟机【Java虚拟机类生命周期】

    C/C++等纯编译语言从源码到最终执行一般要经历:编译.连接和运行三个阶段,连接是在编译期间完成,而java在编译期间仅仅是将源码编译为Java虚拟机可以识别的字节码Class类文件,Java虚拟机对 ...

  6. (转)《深入理解java虚拟机》学习笔记7——Java虚拟机类生命周期

    C/C++等纯编译语言从源码到最终执行一般要经历:编译.连接和运行三个阶段,连接是在编译期间完成,而java在编译期间仅仅是将源码编译为Java虚拟机可以识别的字节码Class类文件,Java虚拟机对 ...

  7. OSGi-入门篇之生命周期层(03)

    前言 生命周期层在OSGi框架中属于模块层上面的一层,它的运作是建立在模块层的功能之上的.生命周期层一个主要的功能就是让你能够从外部管理应用或者建立能够自我管理的应用(或者两者的结合),并且给了应用本 ...

  8. Liferay7 BPM门户开发之17: Portlet 生命周期

    Portlet 生命周期 init() =〉 render() =〉 processAction() =〉 processEvent() =〉 serveResource() =〉destroy() ...

  9. <JVM中篇:字节码与类的加载篇>03-类的加载过程(类的生命周期)详解

    笔记来源:尚硅谷JVM全套教程,百万播放,全网巅峰(宋红康详解java虚拟机) 同步更新:https://gitee.com/vectorx/NOTE_JVM https://codechina.cs ...

随机推荐

  1. Kettle的集群排序 2——(基于Windows)

    5.使用kettle集群模式对相关的数据进行排序 既然,基于Carte服务程序所搭建的集群已经在Spoon中设定好了, 可以首先,先来启动四个节点: "以管理员身份运行"打开 四个 ...

  2. UIAlertController (UIActionSheet, UIAlertView is deprecated in iOS 8.)

    iOS 8 后 UIAlertView 和  UIActionSheet 都被合并到了 UIAlertController里面. 文档原文: Important: UIAlertView is dep ...

  3. 获取当前页面的url

    var url = window.location.href; var b = url.substring(url.lastIndexOf('/')+1, url.length);

  4. Android 新版NDK环境搭建(免Cygwin)

    使用最新ndk,直接抛弃cygwin,以前做Android的项目要用到NDK就必须要下载NDK,下载安装Cygwin(模拟Linux环境用的),下载CDT(Eclipse C/C++开发插件),还要配 ...

  5. 【BZOJ2038】【莫队】小z的袜子

    Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小Z再也无法忍受这恼人的找袜子过程,于是他决定听天由命……具体来说,小Z把这N只袜 ...

  6. phpcms V9 数据模型基类(转)

    转自:http://www.cnblogs.com/Braveliu/p/5100421.html 在学习<phpcms V9首页模板文件解析>的第七步,我们看到content_model ...

  7. Eclipse启动Tomcat报错,系统缺少本地apr库

    Eclipse启动Tomcat报错,系统缺少本地apr库. Tomcat中service.xml中的设置情况. 默认情况是HTTP协议的值:protocol="HTTP/1.1" ...

  8. FC8下备份linux系统

    linux系统可以使用tar来备份.<br><br> 我在FC8上装好了totem, mplayer, audacious, 并搞定了wifi后,我觉得该备份一下FC8系统.& ...

  9. js 的其它运算符和优先级

    三元运算符: 语法为 exp1? exp2:exp3 判断 exp1是true 和 false  如果true,则返回exp2  ,如果false ,则返回exp3 <script> if ...

  10. JS和jQuery获取节点的兄弟,父级,子级元素

    原文转自http://blog.csdn.net/duanshuyong/article/details/7562423 先说一下JS的获取方法,其要比JQUERY的方法麻烦很多,后面以JQUERY的 ...