原文地址: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 test
Goodbye World!!
Running Common Service
 
osgi>

 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.jar
Bundle id is 321
 
osgi> ss New
 
Framework is launched.
 
id      State       Bundle
321     INSTALLED   NewBundle_1.0.0.201008182238
 
osgi> start NewBundle
New bundle start!!
 
osgi> uninstall NewBundle
New bundle stop!!
 
osgi> ss New
 
Framework is launched.
 
id      State       Bundle
 
osgi>

上面代码说明,在卸载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

最新文章

  1. JavaScript-join连接符
  2. iOS开发 判断用户是否开启了定位
  3. winmail安装完成后,SMTP/POP3/ADMIN/HTTP/IMAP/LDAP服务不能启动?
  4. ASP.NET MVC 入门1、简介
  5. DEVICE_OBJECT结构参数
  6. linux内存机制
  7. 这样就算会了PHP么?-11
  8. BZOJ 2748 音量调节
  9. ceph增加osd流程
  10. 图片上传插件用法,JS语法【三】
  11. ENC28j60以太网芯片驱动程序简介
  12. 固定表头带滚动条的HTML表格
  13. 201521123087《Java程序设计》 第八周学习总结
  14. MySQL中判断日期间隔的方法
  15. C语言第三次作业--嵌套循环
  16. Python小代码_1_九九乘法表
  17. Codeforces Round #483 (Div. 2)
  18. Python爬虫爬取网页图片
  19. 以Windows服务方式运行ASP.NET Core程序【转载】
  20. char和varchar、浮点数和定点数

热门文章

  1. java版本的Kafka消息写入与读取
  2. fsockopen反弹shell脚本
  3. JS While
  4. GO 全面解析 json tag 篇
  5. Azure 架构师认证考试
  6. 测试使用索引库crud和高级查询分页
  7. Node.js实现登录注册功能
  8. 201871010112-梁丽珍《面向对象程序设计(java)》第十三周学习总结
  9. 题解:SPOJ1026 Favorite Dice
  10. day87_11_7微信小程序之登录,支付(获取ip,requests使用),授权