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. c# 双问号运算

    model.id??0 ??运算:如果运算符左边的值为NULL侧返回右边的值,否则返回左边的值

  2. C# 通过hessian调Java注意事项

    照理说C#可以通过标准的web服务可以轻松地调用Java,但是鉴于hessian的高性能及开发效率,个人认为C#通过hessian调用java是很值得提倡的.之前完成的一个比较大型的企业应用项目就是采 ...

  3. oracle如何获取当年第一月,如今年是2015年,则需获取 201501

    当年第一个月 SQL> select to_char(sysdate,'yyyy')||'01' from dual;TO_CHA ------ 201501当前年,月 SQL> sele ...

  4. .net excel利用NPOI导入oracle

    1.链接数据库 引用System.Data.OracleClient: //数据库链接字符串   Data Source如:192.168.5.153:1521/orcl string linkStr ...

  5. Swift中共有74个内建函数

    Swift中共有74个内建函数,但是在Swift官方文档(“The Swift Programming Language”)中只记录了7中.剩下的67个都没有记录.   本文将列举Swift所有的内建 ...

  6. UITextView/UITextField检测并过滤Emoji表情符号

    UITextView/UITextField检测并过滤Emoji表情符号 本人在开发过程中遇到过这种情况,服务器端不支持Emoji表情,因此要求客户端在上传用户输入时,不能包含Emoji表情.在客户端 ...

  7. 查看sql语句执行时间/测试sql语句性能

    写程序的人,往往需要分析所写的SQL语句是否已经优化过了,服务器的响应时间有多快,这个时候就需要用到SQL的STATISTICS状态值来查看了. 通过设置STATISTICS我们可以查看执行SQL时的 ...

  8. 确认(confirm 消息对话框)

    confirm 消息对话框通常用于允许用户做选择的动作(包括一个确定按钮和一个取消按钮). 语法: confirm(str) str:在消息对话框中要显示的文本 返回值: 当用户点击"确定& ...

  9. SGU 276 Andrew's Troubles

    简单的题.直接找题意来就好了. #include <iostream> #include <cstdio> using namespace std; int s, n, ans ...

  10. 一个基于nodejs,支持http/https的中间人(MITM)代理,便于渗透测试和开发调试。

    源码地址:https://github.com/wuchangming/node-mitmproxy node-mitmproxy node-mitmproxy是一个基于nodejs,支持http/h ...