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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/398356.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

tomcat+nginx+redis实现均衡负载、session共享

在项目运营时,我们都会遇到一个问题,项目需要更新时,我们可能需先暂时关闭下服务器来更新。但这可能会出现一些状况:1.用户还在操作,被强迫终止了(我们可以看日志等没人操作的时候更新,但总可能会有万一)2.不知道的用户…

洛谷 P3184 [USACO16DEC]Counting Haybales数草垛

洛谷 P3184 [USACO16DEC]Counting Haybales数草垛 题目描述 Farmer John has just arranged his NN haybales (1 \leq N \leq 100,0001≤N≤100,000 ) at various points along the one-dimensional road running across his farm. To make sure they are spaced out appropria…

关于笔试的一些博客

这里收集了一些阿里的网上笔试题目 阿里笔试题(2015)持续更新中腾讯阿里实习生招聘笔试总结阿里2014研发实习生笔试题解析【阿里】算法工程师笔试题整理(13&14年)【阿里】算法工程师笔试【2015.04.02】转载于:https://www.cnblogs.com/chen310/p/438…

Entity Framework 6 Recipes 2nd Edition(13-2)译 - 用实体键获取一个单独的实体

问题 不管你用DBFirst,ModelFirst或是CodeFirst的方式,你想用实体键获取一个单独的实体.在本例中,我们用CodeFirst的方式. 解决方案 假设你有一个模型表示一个Painting(绘画)类型的实体,如Figure 13-2所示: Figure 13-2. The Painting entity type in our model 在代码In Listi…

C#心得与经验(二)

本周学到很多C#关于Interface, Array的知识&#xff0c;在这里简单复习一下几个易混的地方&#xff0c;重在理解。 一、Interface 使用as来避免多态时没有接口的Exception&#xff1a; Document [] folder new Document[5]; for (int i 0; i < 5; i) {if (i % 2 0){fold…

java实例化对象

摘要&#xff1a;分享牛&#xff0c;分享牛分享&#xff0c;java类加载机制&#xff0c;java实例化对象&#xff0c;java实例化对象机制&#xff0c;java基础。 java是如何实例化对象的呢&#xff1f;以及实例化对象的先后顺序是什么&#xff1f;下面我们以测试的方式说明. 1.1…

项目总结(3.28)

项目是用vuewebpackelementUI 完成的。虽然没有什么深奥的技术和难点&#xff0c;但是有些细节还是值得注意的。 1、满足不同屏幕尺寸下缩放全屏显示。 单单只靠宽度、高度百分比是不可以实现的&#xff0c;比如如果宽度设置百分比&#xff0c;当屏幕宽度比较小时&#xff0c;这…

Android开发删除短信

本人一直有一个需求&#xff0c;想要手机自动拦截黑名单里联系人的信息并自动删除这些短信&#xff0c;手机管家之类的软件可以拦截但是没找到能删除这些短信的&#xff0c;于是就萌生了想自己写一个android软件的想法。 加上物联网的兴起&#xff0c;安卓设备开发肯定前景很好…

让你提升命令行效率的 Bash 快捷键 [完整版]

生活在 Bash shell 中&#xff0c;熟记以下快捷键&#xff0c;将极大的提高你的命令行操作效率。 编辑命令 Ctrl a &#xff1a;移到命令行首Ctrl e &#xff1a;移到命令行尾Ctrl f &#xff1a;按字符前移&#xff08;右向&#xff09;Ctrl b &#xff1a;按字符后移&…

Okhttp的封装和回调

public class HttpUtil {static HttpUtil util;private final OkHttpClient client;// 私有化构造方法private HttpUtil(){client new OkHttpClient();}public static HttpUtil getInstance(){if(util null){synchronized (HttpUtil.class){util new HttpUtil();}}return u…

将Session写入Memcache

通过session_set_save_handler()方法自定义Session写入Memcache 1 <?php 2 class MemSession{3 private static $handler null;4 private static $lifetime null;5 private static $time null;6 const MS session;7 8 …

微软解决方案全景

我遇到过一家企业的CTO&#xff0c;做过很多知名大企业的信息化规划设计&#xff0c;但他基本是一个Like Unix派系的技术&#xff0c;而我是一个Microsoft派系的技术&#xff0c;在这里我不是讨论哪个派系的技术好和不好&#xff0c;我只是在和他的聊天中发现了一些非常有意思的…

Going Dutch BAPC( 状态转移DP)

题目描述 You and your friends have just returned from a beautiful vacation in the mountains of the Netherlands. When on vacation, it’s annoying to split the bill on every expense every time, so you just kept all the receipts from the vacation, and wrote d…

查询指定目录下的文件中是否包含指定字符串

cd /etc/apache2/site-enable/rep -rl test.com ./*意为查询apache虚拟主机中是否包含test.com的域名转载于:https://blog.51cto.com/linuxtips/1773938

使用 C# 编程对RTF文档的支持

http://www.68design.net/Development/Aspnet/Basis-AspNet/26011-1.html 转载于:https://www.cnblogs.com/faxian/p/4402910.html

algorand共识协议_【Filecoin】理解预期共识 - 及它的优缺点

摘 要预期共识就是上帝掷飞镖预期共识的优点在于简单&#xff0c;而且每一次选举胜出者数量的平均数为1但预期共识不能保证每次选举的胜出者数量&#xff0c;这是其最大的问题期待有更好的基于可验证随机函数的共识算法出现&#xff0c;设计者可获得20万美金奖赏预期共识 就是 …

Android软件安全与逆向分析之Dalvik

注意点 首先&#xff0c;让我们来思考下面几个问题&#xff1a; 什么是Dalvik虚拟机? Dalvik VM与JVM有什么区别&#xff1f; Dalvik VM有什么新的特点&#xff1f; Dalvik VM的架构是怎么样的&#xff1f; 首先&#xff0c;我得承认第一个问题问得很傻&#xff1a;什么是Dalv…

在java中实现日期类型和字符串类型的转换大全(Date String Timestamp Datetime)

用Timestamp来记录日期时间还是很方便的&#xff0c;但有时候显示的时候是不需要小数位后面的毫秒的&#xff0c;这样就需要在转换为String时重新定义格式。 Date、String、Timestamp之间的转换&#xff01; [java] view plaincopyprint? public static void main(String[] ar…

c++ 多个线程操作socket要同步吗_基础知识深化:NIO优化原理和Tomcat线程模型

1、I/O阻塞书上说BIO、NIO等都属于I/O模型&#xff0c;但是I/O模型这个范围有点含糊&#xff0c;我为此走了不少弯路。我们日常开发过程中涉及到NIO模型应用&#xff0c;如Tomcat、Netty中等线程模型&#xff0c;可以直接将其视为 网络I/O模型 。本文还是在基础篇章中介绍几种I…

Http中的同步请求和异步请求

最近在上springmvc的JSON数据交换的时候&#xff0c;老师下课提了一个课后问题&#xff1a;什么是异步请求&#xff1f;什么是同步请求&#xff1f;我想大部分同学听到这个问题的时候应该和我一样不知所云。现在&#xff0c;给大家分享一篇关于同步请求和异步请求的小知识。举个…