• [转]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 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:pluginsNewBundle_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:pluginsNewBundle_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
  • 相关阅读:
    UniGUI 之UniDBGrid(05)
    Spring事务管理
    MySQL数据库隔离级别
    MySQL并发控制
    MySQL存储引擎
    数据库事务(2)---ACID与并发问题
    数据库事务(1)----- JDBC事务与JTA事务
    navicat Window . MAC版常用快捷键
    重要博客
    yunw
  • 原文地址:https://www.cnblogs.com/wlzjdm/p/6882213.html
Copyright © 2020-2023  润新知