osgi简介
osgi是一个模块化应用程序的框架,可以在osgi的网站上找到对应的规范.用osgi开发的应用程序运行在osgi容器内.Knopflerfish, Equinox, 和 Apache Felix 是3个比较流行的osgi容器的实现.osgi最基本的部署单元式bundle,每一个应用程序被分割为多个bundle,运行在容器之中.不同之间不是完全独立的,某些bundle可能会为其他bundle提供服务.osgi容器中的bundle有三种状态:installed, resolved, 或者active.bundle可以通过注册的范式向其他bundle暴漏service,同时也可以暴漏java 包被其他bundle引用.
osgi的优点
- 可以在不重启容器的情况下安装、卸载、开启和停止模块。
- 应用程序可以同时运行特殊模块的不同版本。
- 基于OSGI开发的应用程序是面向服务的。
Hello World
下面我们创建一个helloworld的例子,同个这个例子我们可以进一步了解osgi。这个例子中,所有bundle运行在eclipse的Equinox容器中。所用的eclipse版本是3.6(helios).
- 通过file->new->project->Plug-in Development->plug-in project 创建项目.点击next.
- 这里设置项目名称为com.javaeye.osgi.helloworld,并在选择an osgi framework:standard.如图
- 在上面的图中点进next之后,进入下一设置页面,选择默认值,然后点击next,选择模板为Hello Osgi bundle,点击finish完成.
项目完成之看一下自动生成的类源代码
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!!");
}
}
包括两个方法start和stop,从字面就可以知道这两个什么意思了.这是eclipse中的一个模板,这样创建之后就可以运行了.
通过run-run configurations..打开如下图的窗口然后双击右边的osgi Framework,在bundles下我们创建bundles被自动选中,然后再target platform下选择org.eclipse.osgi,其他的不选,然后点击run按钮运行.
如果控制台view打开的话,控制台会输出"Hello World!!",然后通过ss命令可以查看所有bundle的状态,可以用过help查找可以使用命令.
osgi> Hello World!!
osgi> ss
Framework is launched.
id State Bundle
0 ACTIVE org.eclipse.osgi_3.6.0.v20100517
1 ACTIVE com.javaeye.osgi.helloworld_1.0.0.qualifier
osgi>
通过start命令和stop命令,可以开启bundle和停止bundle运行.
osgi> stop 1
Goodbye World!!
osgi> ss
Framework is launched.
id State Bundle
0 ACTIVE org.eclipse.osgi_3.6.0.v20100517
1 RESOLVED com.javaeye.osgi.helloworld_1.0.0.qualifier
osgi> start 1
Hello World!!
osgi>
运行多个版本的Hello World
打开META-INF/MANIFEST.MF,在eclipse编辑中在overview tab页中打修改version原来是1.0.0.qualifier,现在改为1.0.0。然后修改上面的源代码为
public class Activator implements BundleActivator {
public void start(BundleContext context) throws Exception {
System.out.println("Hello World!! Version:1.0.0");
}
public void stop(BundleContext context) throws Exception {
System.out.println("Goodbye World!! Version:1.0.0");
}
}
修改完毕后,通过Exeport->Plug-in Development->Deployable plug-ins and/or fragments导出新的版本的bundle.这里方的路径是D:\hello\plugins\com.javaeye.osgi.helloworld_1.0.0.jar.然后通过以下命令安装,运行.在下面可以清楚的看见,不同操作之后bundle的不同状态,可以了解其在容器中的生命周期.
osgi> install file:D:\hello\plugins\com.javaeye.osgi.helloworld_1.0.0.jar
Bundle id is 3
osgi> ss
Framework is launched.
id State Bundle
0 ACTIVE org.eclipse.osgi_3.6.0.v20100517
1 ACTIVE com.javaeye.osgi.helloworld_1.0.0.qualifier
3 INSTALLED com.javaeye.osgi.helloworld_1.0.0
osgi> start 3
Hello World!! Version:1.0.0
osgi> ss
Framework is launched.
id State Bundle
0 ACTIVE org.eclipse.osgi_3.6.0.v20100517
1 ACTIVE com.javaeye.osgi.helloworld_1.0.0.qualifier
3 ACTIVE com.javaeye.osgi.helloworld_1.0.0
osgi> stop 3
Goodbye World!! Version:1.0.0
osgi> ss
Framework is launched.
id State Bundle
0 ACTIVE org.eclipse.osgi_3.6.0.v20100517
1 ACTIVE com.javaeye.osgi.helloworld_1.0.0.qualifier
3 RESOLVED com.javaeye.osgi.helloworld_1.0.0
osgi>