[转]Eclipse插件开发之基础篇(5) 制作OSGi Bundle
原文地址:http://www.cnblogs.com/liuzhuo/archive/2010/08/18/eclipse_plugin_1_2_2.html
1. 生成OSGi工程
首先打开新工程向导,选择创建Eclipse插件工程。在[目标平台(Target Platform)]选项中选择[OSGi 框架(OSGi framework)]。
图5-1 创建插件工程

在选择模板的时候选择,Hello OSGi Bundle后点击完成。
图5-2 选择OSGi模板

生成工程后,在Manifest编辑器中点击[启动框架(Launch the framework)]运行新生成的OSGi Bundle。执行后可能会出现大量的错误。原因是Eclipse中的OSGiBundle在OSGi框架中注册了,但是UI相关的部分没能启动。我们进入[运行设置(Run Configuration)],取消所有的Bundle绑定,只选中我们当前要测试的Bundle。(我自己作的工程即使都取消了运行也报错:(~~)
在框架运行时,向控制台窗口输入开始和停止指令,看一下效果吧。
图5-4 Hello OSGi

2. OSGi Service和Tracker
上一节我们制作了一个简单的在启动和停止时输出消息的Bundle。代码很简单的实现了BundleActivator接口的sart()和stop()方法,在其中利用System.out.println打出了信息。
代码1
|
1
2
3
4
5
6
7
8
9
10
11
|
public class Activator implements BundleActivator { public void start(BundleContext context) throws Exception { System.out.println("Hello World!!"); } public void stop(BundleContext context) throws Exception { System.out.println("Goodbye World!!"); }} |
在OSGi框架中为了生命周期管理提供了开始和停止方法,仅此而已,OSGi Bundle就可以运行起来了。OSGi框架有一种可以为多个Bundle提供作为共通(Common)使用的Service功能,称为OSGi Service。
代码2 最简单的OSGi Service
|
1
2
3
4
5
|
public class OSGiService { public void doSomething(){ System.out.println("Running Common Service"); }} |
代码3 执行OSGi Service
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
public class Activator implements BundleActivator { public void start(BundleContext context) throws Exception { System.out.println("Hello World!!"); // 注册OSGi Service context.registerService(OSGiService.class.getName(), new OSGiService(), new Properties()); } public void stop(BundleContext context) throws Exception { System.out.println("Goodbye World!!"); // 生成Service Tracker ServiceTracker tracker = new ServiceTracker(context, OSGiService.class.getName(), null); tracker.open(); // 取得Service OSGiService service = (OSGiService) tracker.getService(); // 执行Service service.doSomething(); }} |
我们看一下上面的Bundle执行的效果。
代码4
|
1
2
3
4
5
6
|
Hello World!!stop testGoodbye World!!Running Common Serviceosgi> |
3. Bundel的安装和更新
OSGi框架在JavaVM不重启的情况下也可以安装和卸载Bundle。下面我们做一个新的Bundle,NewBundle在启动和停止时输出一行消息。
代码5 NewBundle
|
1
2
3
4
5
6
7
8
9
10
11
|
public class Activator implements BundleActivator { public void start(BundleContext context) throws Exception { System.out.println("New bundle start!!"); } public void stop(BundleContext context) throws Exception { System.out.println("New bundle stop!!"); }} |
将该工程导出到c盘根目录下,启动OSGi控制台输入install file:\\\C:\plugins\NewBundle_1.0.0.201008182238.jar。再通过ss命令确认bundle的安装。
代码6 Bundle的安装和卸载
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
osgi> install file:\\\C:\plugins\NewBundle_1.0.0.201008182238.jarBundle id is 321osgi> ss NewFramework is launched.id State Bundle321 INSTALLED NewBundle_1.0.0.201008182238osgi> start NewBundleNew bundle start!!osgi> uninstall NewBundleNew bundle stop!!osgi> ss NewFramework is launched.id State Bundleosgi> |
上面代码说明,在卸载Bundle的时候会先调用stop方法。对Bundle的MANIFEST文件的描述方法作一个简单的总结如下表:
| 项目名 | 说明 |
| Manifest-Version | Jar包的Manifest文件指定了版本号,通常是1.0 |
| Bundle-ManifestVersion | Bundle的Manifest文件指定了版本,通常是2 |
| Bundle-Name | Bundle的名称 |
| Bundle-SymbolicName | Bundle的Synbo名称,OSGi中以这个名称注册 |
| Bundle-Version | Bundle的版本。在OSGi中有可能多个不同版本的同一Bundle共存 |
| Bundle-Activator | 管理Bundle生命周期的类名 |
| Bundle-Vendor | 定义了制作Bundle的组织名称 |
| Bundle-ActivationPolicy | 指定了Bundle启动的策略。 |
| Import-Package | 指定了Bundle引用的包 |
| Export-Package | 指定了Bundle向起他Bundle公开的包 |
| Required-Bundle | 指定了Bundle引用的Bundle |
[转]Eclipse插件开发之基础篇(5) 制作OSGi Bundle的更多相关文章
- Eclipse插件开发之基础篇(4) OSGi框架
转载出处:http://www.cnblogs.com/liuzhuo. 1. 什么是OSGi框架 OSGi(Open Service Gateway Initiative)框架是运行在JavaVM环 ...
- [转]Eclipse插件开发之基础篇(4) OSGi框架
原文地址:http://www.cnblogs.com/liuzhuo/archive/2010/08/18/eclipse_plugin_1_2_1.html 1. 什么是OSGi框架 OSGi(O ...
- [转]Eclipse插件开发之基础篇(2) 第一个Eclipse插件
原文地址:http://www.cnblogs.com/liuzhuo/archive/2010/08/15/eclipse_plugin_1_1_1.html 在Eclipse中使用PDE(Plug ...
- [转]Eclipse插件开发之基础篇(1) 插件开发的基础知识
原文地址:http://www.cnblogs.com/liuzhuo/archive/2010/08/13/eclipse_plugin_1_0_2.html 名词翻译 有一些名词在翻译的过程中可能 ...
- [转]Eclipse插件开发之基础篇(6) SWT简介
原文地址:http://www.cnblogs.com/liuzhuo/archive/2010/09/01/eclipse_plugin_1_3_1.html SWT(Standard Widget ...
- [转]Eclipse插件开发之基础篇(3) 插件的测试与调试
原文地址:http://www.cnblogs.com/liuzhuo/archive/2010/08/17/eclipse_plugin_1_1_2.html 1. 使用JUnit对插件进行测试 E ...
- 关于Eclipse使用Git基础篇
一:Git的下载与安装与基本使用 1.打开eclipse->help->Eclipse Markplace->search->fiind输入Egit 你会看到如下截图(我的为已 ...
- Eclipse插件开发 学习笔记 PDF 第一篇到第四篇 免分下载 开发基础 核心技术 高级进阶 综合实例
<<Eclipse插件开发 学习笔记>>,本书由浅入深.有重点.有针对性地介绍了Eclipse插件开发技术,全书分为4篇共24章.第一篇介绍Eclipse平台界面开发的基础知识 ...
- Eclipse插件基础篇一
名词翻译 有一些名词在翻译的过程中可能会出现歧义,在这里统一一下. アーキテクチャ=architecture=架构 プラットフォーム=platform=平台 コンポーネント=component=组件 ...
随机推荐
- python-Redis模块常用的方法汇总
Redes模块常用的方法汇总 一.创建建Redis对象 1.直接使用 import redis r = redis.Redis(host='127.0.0.1', port=6379) 2.连接池使用 ...
- Netty服务端Channel注册Selector及绑定服务器端口
当服务端Channel 创建并且初始化完成之后,会将其注册到 selector,通过语句config().group().register(channel)进行注册工作,该方法最终调用 Abstrac ...
- Python:有参装饰器与多个装饰器装饰一个函数
有参装饰器 def timmerout(flag1): #flag1 =flag def timmer(f): def inner(*args,**kwargs): if flag1: start_t ...
- NoiseSystem数据库设计心得-洋芋好想飞
团队:洋芋好想飞 成员:乔祥硕 石高飞 杨慧慧 梁家豪 潘景渝 整理:乔祥硕 PM乔祥硕: 10月25日14:30到17:30,10月27日14:30到17:30,11月1日14:30到17:30,这 ...
- 北航软件学院Java历届期末考题整理
文章目录 abstract static Thread finally package Exception I/O 子类和父类 关键字 标识符 垃圾收集 数据类型 环境配置 网路编程 initial ...
- 解决element-ui的表格设置固定栏后,边框线消失的bug
如上图所示,边框线消失了,解决方法如下 添加css代码,如果是修改全局,则到全局样式文件添加 .el-table__row{ td:not(.is-hidden):last-child{ right: ...
- SQL server利用脚本添加链接服务器,可设置别名
USE [master]GO EXEC master.dbo.sp_addlinkedserver @server = N'你的别名', @srvproduct=N'', @provider=N'SQ ...
- Pymysql的常见使用方法
cursor.fetchone()与cursor.fetchall()的区别: cursor.fetchone():只能显示一个数据 cursor.fetchall():才能显示查出来的所有数据 P ...
- githup常用备份
https://github.com/ https://github.com/doumeki/ThrExcel https://github.com/xinxi1990/MyMonkey https: ...
- 04-Node.js学习笔记-相对路径VS绝对路径
4.1相对路径VS绝对路径 大多数情况下使用绝对路径,因为相对路径有时候相对的是命令行工具的当前工作目录 在读取文件或者设置文件路径时都会选择绝对路径 4.2使用__dirname 获取当前文件所在的 ...