[OSGI] Felix基本环境搭建及操作

645 阅读3分钟

Felix是Apache旗下的一款实现OSG规范I的框架。访问地址:felix.apache.org/

1、github下载代码

测试代码:yun81911/maven-apache-felix-tutorial: Mavenized version of the apache-felix-tutorial at http://felix.apache.org/documentation/tutorials-examples-and-presentations/apache-felix-osgi-tutorial.html (github.com)

Apache Felix OSGi 示例、githun中包含以下教程的代码实现

本教程将依次从简单到复杂、创建的 OSGi 捆绑包,以说明 OSGi 框架提供的大多数特性和功能。

2、下载felix:

搭建基本的OSGI框架只需要下载Felix Framework, 在Index of /dist/felix (apache.org)。下载 org.apache.felix.main.distribution-6.0.4.zip

3、启动felix:下载完成后解压至任意目录(如:D:\felix-framework-4.4.1),在felix-framework-4.4.1目录下打开CMD命令窗口,执行启动Felix框架命令
java -jar bin/felix.jar

完成启动后输入 lb 命令查看当前启动Felix的bundle

g! lb
START LEVEL 1
   ID|State      |Level|Name
    0|Active     |    0|System Bundle (4.4.1)
    1|Active     |    1|Apache Felix Bundle Repository (2.0.2)
    2|Active     |    1|Apache Felix Gogo Command (0.14.0)
    3|Active     |    1|Apache Felix Gogo Runtime (0.12.1)
    4|Active     |    1|Apache Felix Gogo Shell (0.10.0)
g!

停止Felix框架是输入 stop 0

4、安装服务:

安装Felix的bundle(helloworld.jar)是需要bundle的绝对路径,执行命令install file:/file_path

也可以:将第1步下载的代码打包后、执行以下命令:

install file:E:/temp/maven-apache-felix-tutorial/apache-felix-tutorial-example-1/target/felix-tut-1-1.0-SNAPSHOT.jar

以下继续使用自带示例

<pre name="code" class="plain">g! lb
START LEVEL 1
   ID|State      |Level|Name
    0|Active     |    0|System Bundle (4.4.1)
    1|Active     |    1|Apache Felix Bundle Repository (2.0.2)
    2|Active     |    1|Apache Felix Gogo Command (0.14.0)
    3|Active     |    1|Apache Felix Gogo Runtime (0.12.1)
    4|Active     |    1|Apache Felix Gogo Shell (0.10.0)
g! install file:/d:/felix-framework-4.4.1/myplugins/helloworld_1.0.0.201501121409.jar
Bundle ID: 5 
g!

执行lb查看安装结果

g! lb
START LEVEL 1
   ID|State      |Level|Name
    0|Active     |    0|System Bundle (4.4.1)
    1|Active     |    1|Apache Felix Bundle Repository (2.0.2)
    2|Active     |    1|Apache Felix Gogo Command (0.14.0)
    3|Active     |    1|Apache Felix Gogo Runtime (0.12.1)
    4|Active     |    1|Apache Felix Gogo Shell (0.10.0)
    5|Installed  |    1|Helloworld (1.0.0.201501121409)
g!

可以看出新安装的helloworld.jar的状态为Installed

5、 启动bundle命令start bundleID
g! start 5
Hello world!
g!

查看bundle状态 lb

g! lb
START LEVEL 1
   ID|State      |Level|Name
    0|Active     |    0|System Bundle (4.4.1)
    1|Active     |    1|Apache Felix Bundle Repository (2.0.2)
    2|Active     |    1|Apache Felix Gogo Command (0.14.0)
    3|Active     |    1|Apache Felix Gogo Runtime (0.12.1)
    4|Active     |    1|Apache Felix Gogo Shell (0.10.0)
    5|Active     |    1|Helloworld (1.0.0.201501121409)
g!

helloworld bundle状态为Active表示插件已激活

6、 停止bundle命令为stop bundleID
g! stop 5
g! lb
START LEVEL 1
   ID|State      |Level|Name
    0|Active     |    0|System Bundle (4.4.1)
    1|Active     |    1|Apache Felix Bundle Repository (2.0.2)
    2|Active     |    1|Apache Felix Gogo Command (0.14.0)
    3|Active     |    1|Apache Felix Gogo Runtime (0.12.1)
    4|Active     |    1|Apache Felix Gogo Shell (0.10.0)
    5|Resolved   |    1|Helloworld (1.0.0.201501121409)
g!

helloworld bundle状态变为Resolved

7、更新helloworld的源码,输出语句变为Hello World, OSGI !

更新操作分为两种方法

7.1 将修改后的bundle替换原有bundle时,执行update bundleID
g! update 5
g! lb
START LEVEL 1
   ID|State      |Level|Name
    0|Active     |    0|System Bundle (4.4.1)
    1|Active     |    1|Apache Felix Bundle Repository (2.0.2)
    2|Active     |    1|Apache Felix Gogo Command (0.14.0)
    3|Active     |    1|Apache Felix Gogo Runtime (0.12.1)
    4|Active     |    1|Apache Felix Gogo Shell (0.10.0)
    5|Installed  |    1|Helloworld (1.0.0.201501121422)
g! start 5
Hello World, OSGI !
g!
7.2 将现有bundle跟新至另一个位置的存放的bundle,执行 update bundleID file:/fiel_path
g! lb
START LEVEL 1
   ID|State      |Level|Name
    0|Active     |    0|System Bundle (4.4.1)
    1|Active     |    1|Apache Felix Bundle Repository (2.0.2)
    2|Active     |    1|Apache Felix Gogo Command (0.14.0)
    3|Active     |    1|Apache Felix Gogo Runtime (0.12.1)
    4|Active     |    1|Apache Felix Gogo Shell (0.10.0)
    5|Active     |    1|Helloworld (1.0.0.201501121409)
g! update 5 file:/d:/felix-framework-4.4.1/myplugins/helloworld_1.0.0.201501121409_new.jar
Hello World, OSGI !
g!
8、卸载bundle 命令 uninstall bundleID
g! uninstall 5
g! lb
START LEVEL 1
   ID|State      |Level|Name
    0|Active     |    0|System Bundle (4.4.1)
    1|Active     |    1|Apache Felix Bundle Repository (2.0.2)
    2|Active     |    1|Apache Felix Gogo Command (0.14.0)
    3|Active     |    1|Apache Felix Gogo Runtime (0.12.1)
    4|Active     |    1|Apache Felix Gogo Shell (0.10.0)
g!