您的位置:首页 > 编程语言 > Java开发

在Eclipse中开发OSGi(3)在Felix环境中运行Bundle

2015-01-22 12:00 477 查看
前面搭建了一个Apache Felix的运行环境,下面就写一个简单的bundle,测试测试。

1、新建一个插件工程,如下图:



点击下一步。

2、给插件工程命名一个名字,这里叫pig1。This plug-in is targeted to run with中选择an OSGI framework -->standard,如下图红框中所示:



点击下一步。

3、bundle中有一个启动类,默认是Activator,相当于普通工程中的Main类。你也可以把它更改成其他名字,这里使用默认的名字。如下图:



点击下一步。

4、去掉Create a plug-in using one of the templates,如下图:



点击Finish。

5、插件工程建好后,打开Activator类,可以看到里面有一个start方法和一个stop方法,可以在bundle启动和停止的时候做一些事情。这里只是简单地输出一个字符串,作为bundle启动和停止时的标识。

/*
* (non-Javadoc)
*
* @see
* org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext
* )
*/
public   void  start(BundleContext bundleContext)  throws  Exception
{
Activator.context = bundleContext;
System.out.println("start pig1" );
}

/*
* (non-Javadoc)
*
* @see
* org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
*/
public   void  stop(BundleContext bundleContext)  throws  Exception
{
Activator.context = null ;
System.out.println("stop pig1" );
}
6、代码也写好后,就可以导出插件工程发布了。如何让这个工程作为一个bundle被部署到Felix容器中呢?右击插件工程pig1,选择Export。可看下图:



出现Export视图之后,选择Plug-in Development下的Deployable plug-ins and fragments,如下图:



点击下一步,选择要导出的插件,Destination选项卡的Directory选择我们的Felix环境的物理地址,导出后,会在Felix工程的根目录自动创建一个plugins目录,bundle会默认导出这个目录。如下图:



点击Finish,你就可以看到Felix工程下面多了一个plugins目录,我们所导出的bundle就在里面,如下图:



7、接着就是安装、运行了。

有三种方法可以安装、运行一个bundle。

(1)使用命令。

首先,启动Felix,在Console中先使用install命令安装bundle,接着使用start命令启动bundle,如下图:



启动的时候,start命令后接着那个bundle的启动ID就可以启动bundle了,如上图的12。

可以看到,当启动bundle的时候,输出了Activator类中start方法的输出语句,即"start pig1"。

Pig1的状态为Active,说明bundle启动成功了。

当然,你也可以使用uninstall命令卸载一个bundle,用法如install命令。

(2)使用Felix配置文件,打开conf/config.properties,如下图:



打开config.properties,找到felix.auto.start.1参数,值写成file:plugins/pig1_1.0.0.201109291700.jar,如:

(如果你有多个bundle,之间用空格隔开)。

# The following property is a space-delimited list of bundle URLs
# to install and start when the framework starts. The ending numerical
# component is the target start level. Any number of these properties
# may be specified for different start levels.
felix.auto.start.1 =file:plugins/pig1_1. 0.0 . 201109291700 .jar
参数写好后,启动Felix,你就可以看到bundle Pig1自动安装并启动了,如下图所示:



(3)第三种方法就是使用File Install了,使用Apache Felix的File Install bundle,我们可以安装和启动bundle而无需启动Felix,这个将在下面的章节中讲解。

8、OK,完成了。

转自:/article/3761904.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐