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. linux 脚本编写基础(一)

    1. Linux 脚本编写基础 1.1 语法基本介绍 1.1.1 开头 程序必须以下面的行开始(必须方在文件的第一行): #!/bin/sh 符号#!用来告诉系统它后面的参数是用来执行该文件的程序.在 ...

  2. Xaml中的资源(1 样式)

    <Window x:Class="MyResoures.MainWindow" xmlns="http://schemas.microsoft.com/winfx/ ...

  3. 浅谈html5某些新元素的用途

    大家都知道html是一种前端网页语言,从出现到现在已经经历了很多的版本了,但是随着html的不断发展,现在的html5已经不再是单一的前端页面语言了,html,javascript,css不再单纯的只 ...

  4. node 裁剪图片

    1.前端一般用Jcrop这个jq插件 要返回 x: 图片 x坐标 y: 图片 y坐标 w: 图片 宽度 h: 图片 高度 2.node 实现 var images = require("im ...

  5. error C2220: warning treated as error - no 'object' file generated解决方法

    error C2220: warning treated as error - no 'object' file generated 警讯视为错误 - 生成的对象文件 / WX告诉编译器将所有警告视为 ...

  6. codeforces 165D.Beard Graph 解题报告

    题意: 给一棵树,树的每条边有一种颜色,黑色或白色,一开始所有边均为黑色,有两个操作: 操作1:将第i条边变成白色或将第i条边变成黑色. 操作2 :询问u,v两点之间仅经过黑色变的最短距离. 树链剖分 ...

  7. 常见ORACLE错误,及解决方案(遇则即时更新)

    1.当登陆时提示“ORA-03113:通信通道的文件结束”时:        解决方案:                     需在X:\oraclexe\app\oracle\product\10 ...

  8. 自动化工具word文档批量转html

    企业有很多的科室,科室的每个人或多或少都会写一些文档,有些文档领导需要浏览,解决的办法是将编辑的文档打印出来,供领导浏览,或是为了节约企业成本,文档就在人与人这间或部门之间copy过来,copy过去. ...

  9. 【开源】前端练手笔记,Chrome扩展应用程序(html+CSS+JS) (1)

    项目名称:github-notification 项目地址:https://github.com/WQTeam/github-notification 说明:本人打算抽时间学习前端(html + cs ...

  10. 简单的html5 File base64 图片上传

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...