eclipse + Felix 开发环境搭建 bundle 开发与调试
来源:互联网 发布:淘宝客服如何建议 编辑:程序博客网 时间:2024/06/05 09:51
Eclipse+Felix bundle 开发与调试
Eclipse 默认集成了equinox 框架,所以基于equinox框架的开发bundles 相对来说对eclipse的配置少点。由于我们平台将采用Felix 框架,所以这里结合实例介绍实现Eclipse Felix环境搭建,bundle调试。
Felix环境搭建
1. 下载Felix framework http://felix.apache.org/site/downloads.cgi
下载后解压,解压后如下图所示:
2、用eclipse新建一个Javaproject,并将该project建立在一个指定的文件夹(Felix):
3、修改工程默认设置
右击工程"Felix"--> "Properties",
打开"Propertiesfor Felix"视图后,点击"Java Build Path" -->"Source",将Default output folder改成"Felix/classes"。
4、将刚才解压的Felix Framework包下的bin、bundle、conf三个文件夹拷贝到Felix工程目录下,最终我们建的Felix工程目录结构如下:
5、切换到Package视图,将felix.jar加入到BuildPath中,如下图所示:
6、配置Run Configurations。new一个Java Application,选择Main class如下图所示:
7、运行后如下图所示:
输入命令"lb",可查看当前有哪些bundle在运行,如下图所示:
到此为止eclipse的Felix环境已经建立好了,可以看到其实就是建立了java application的工程。Felix shell命令与equinox shell指令有些不同,查看bundle状态指令在这里是lb,而在equinox里是ss,但关于bundles的安装的相关指令是一样的,(install start stop uninstall)。
Bundles开发
在前面搭建了一个Apache Felix的运行环境,下面是bundle开发介绍。
首先开发servicebundle,这个bundle会注册到osgi服务中心,为其他client bundles提供服务。也就相当于后面在onu里面开发的bundles。
1、新建一个插件工程,
2、给插件工程命名一个名字,这里叫hwfelixservice。This plug-in is targeted to run with中选择anOSGI framework -->standard(不要选equinox)
在工程里面分别实现接口,接口实现,及该bundle的activitor。如我的工程里面:
Hello.java实现接口:
package hwfelixservice.helloworld;
public interfaceHello {
voidsayHello();
}
HelloImpl.java是接口实现:
package hwfelixservice.helloworld.impl;
import hwfelixservice.helloworld.Hello;
public classHelloImpl implements Hello{
finalString hello_str;
publicHelloImpl(String str) {
// TODOAuto-generated constructor stub
this.hello_str =str;
}
@Override
publicvoid sayHello() {
// TODOAuto-generated method stub
System.out.println(hello_str);
}
}
Activitor.java实现服务的注册与注销:
packagehwfelixservice.helloworld.activater;
import java.util.*;
importorg.osgi.framework.ServiceRegistration;
import hwfelixservice.helloworld.Hello;
importhwfelixservice.helloworld.impl.HelloImpl;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
public class Activator implementsBundleActivator {
privatestatic BundleContext context;
privateList<ServiceRegistration> registrations = newArrayList<ServiceRegistration>();
staticBundleContext getContext() {
returncontext;
}
/*
* (non-Javadoc)
* @seeorg.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
*/
publicvoid start(BundleContext bundleContext) throws Exception {
Activator.context= bundleContext;
System.out.println("registerService!!!");
registrations.add(context.registerService(Hello.class.getName(),new HelloImpl("hello Felix!"), null));
}
/*
* (non-Javadoc)
* @seeorg.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
*/
publicvoid stop(BundleContext bundleContext) throws Exception {
Activator.context= null;
for(ServiceRegistrationregistration: registrations){
System.out.println("unregister"+registration);
registration.unregister();
}
}
}
Service bundle建立好后,需要暴露的接口出去,以方便其他bundle调用。所以设置MENIFEST.MF:
Export-Package:hwfelixservice.helloworld;version="1.0.0"
service bundle project建立好后我们再开发client bundle,操作步骤跟service bundle基本相同(这里工程名hwfelixclient),注意要设置选择anOSGI framework -->standard(不要选equinox)。
开发ClientActivator
HelloClientActivator.java:
package hwfelixclient;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import hwfelixservice.helloworld.Hello;
public class HelloClientActivatorimplements BundleActivator {
privatestatic BundleContext context;
staticBundleContext getContext() {
returncontext;
}
public void start(BundleContextbundleContext) throws Exception {
HelloClientActivator.context= bundleContext;
ServiceReferenceref = context.getServiceReference(Hello.class.getName());
if(ref!= null){
Hellohello = null;
try{
hello= context.getService(ref);
if(hello!= null){
hello.sayHello();
}else{
System.out.println("servicehello is null!!");
}
}catch(Exception e){
System.out.println("Service:Hello---notexist");
}
}
}
public void stop(BundleContextbundleContext) throws Exception {
HelloClientActivator.context= null;
}
}
从代码中可以看出它从OSGI服务中心获取Hello服务,并调用sayHello接口。
代码开发完后,需要配置MENIFEST.MF,设置依赖关系:
Import-Package:hwfelixservice.helloworld;version="1.0.0",
org.osgi.framework;version="1.3.0"
其中hwfelixservice.helloworld就是我们自己开发的service bundle。
Service bundle 和client bundle开发完成后,我们怎么才能让他们在Felix环境里面运行呢?
运行bundles
首先导出我们开发的bundles,选择这两个工程,右键点击export,选择Deployable plug-ins
… next:
注意这里的Destination里面的Directory要选择我们最开始建立的Felix工程目录。(也可以随便导出到哪里,然后copy到Felix工程里面)
Finish 之后会在我们的Felix工程目录自动生成plugins目录,并且我们开发的两个bundles就会生成在里面:
到此我们将自己开发的bundles部署到了Felix环境,那么怎么运行呢?
再回到我们的Felix工程,RunConfigurations。启动我们最开始建立的JavaApplication configuration:
输入lb:
g! lb
START LEVEL 1
ID|State |Level|Name
0|Active | 0|System Bundle (5.0.1)
1|Active | 1|Apache Felix Bundle Repository (2.0.4)
2|Active | 1|Apache Felix Gogo Command (0.14.0)
3|Active | 1|Apache Felix Gogo Runtime (0.16.2)
4|Active | 1|Apache Felix Gogo Shell (0.10.0)
我们会看到我们开发的两个bundlse并没有启动,下面我们要启动我们的bundles,
有两种方法来启动,一种就是在直接通过命令安装并启动:
g! install file:plugins/hwfelixservice_1.0.2.jar
bundle ID 5
g! start 5
g! install file:plugins/hwfelixclient_1.0.2.jar
bundle ID 6
g! start 6
g! lb
START LEVEL 1
ID|State |Level|Name
0|Active | 0|System Bundle (5.0.1)
1|Active | 1|Apache Felix Bundle Repository (2.0.4)
2|Active | 1|Apache Felix Gogo Command (0.14.0)
3|Active | 1|Apache Felix Gogo Runtime (0.16.2)
4|Active | 1|Apache Felix Gogo Shell (0.10.0)
5|Active | 1|hwfelixservice (1.0.2)
6|Active | 1|hwfelixclient (1.0.2)
另一种方法是使用Felix配置文件,打开conf/config.properties
打开config.properties,找到felix.auto.start.1参数,值写成
felix.auto.start.1=file:plugins/hwfelixservice_1.0.2.jar
felix.auto.start.1=file:plugins/hwfelixclient_1.0.2.jar
这样在启动Felix的时候就可以自动安装我们开发的bundle了。
到目前为止bundle已经可以在Felix框架中正常运行了,那么我们怎么根据源码调试我们的bundles呢?
调试bundles
首先在我们的bundle源码中设置断点,然后选中Felix工程,右键Debug As-> Debug configrations, 启动我们的debug模式:
这样我们的bundles运行的我们设置的断点处就会停止。首次运行调试时,debug会出现找不到源文件,点击attach source,选择add project,将我们的bundle project 添加进来就可以了。还有一个问题就是如何自动更新我们的bundle版本呢?
Bundle版本更新
关于bundle版本的更新,Felix已经提供了自动更新策略,那就是使用org.apache.felix.fileinstall bundle来维护,我们只需要去网上下载他的jar包,然后放在Felix的bundle文件夹下,那么在启动Felix后,该bundle就会自动启动。而他是如何管理bundles版本的呢,当然需要些配置:就是在我们的configurations里面配置启动参数-Dfelix.fileinstall.dir=./plugins。
意思是指定该bundle的监测目录。也可以设置检测周期。
这样我们每次的版本更新就需要做两件事,
1.将我们的新版本放到plugins(如hwfelixclient_1.0.2)
2.如果要删除旧版本我们就直接删除plugins里面的旧版本(如hwfelixclient_1.0.1)
org.apache.felix.fileinstall bundle会自动安装我们的新版本,如果我们删除了我们的旧版本,org.apache.felix.fileinstall bundle会自动从OSGI运行环境中uninstall 我们的旧版本。
到目前为止基本的功能点已经介绍完了,另外一个问题就是client bundle 如何实现对service bundle监听,以防止service bundle变化 (如停止、版本更新等)对client的影响。这个问题OSGI-action一书中有详细介绍,4.3节处理动态性,监听服务。由于这个我也是大致浏览了一下,理解的不深,关于这个还是请大家看书吧。
- eclipse + Felix 开发环境搭建 bundle 开发与调试
- 在Eclipse中开发OSGi(1)搭建Felix环境
- 在Eclipse中开发OSGi(3)在Felix环境中运行Bundle
- Apache Felix 的OSGi开发环境搭建
- Apache Felix 的OSGi开发环境搭建
- Apache Felix 的OSGi开发环境搭建
- OSGI企业应用开发(二)Eclipse中搭建Felix运行环境
- OSGI企业应用开发(二)Eclipse中搭建Felix运行环境
- eclipse集成开发环境搭建与裸机程序调试
- Android开发环境搭建——Eclipse ADT bundle
- 通过eclipse开发opencms-搭建调试环境
- Eclipse搭建stm32开发环境+jlink调试
- 在Eclipse中搭建Apache Felix环境
- Android adt-bundle开发环境搭建小记
- 快速搭建Android开发环境ADT-Bundle
- Android开发环境搭建ADT-Bundle
- Android开发环境搭建ADT-Bundle
- 快速搭建Android开发环境ADT-Bundle
- linux进程间通讯的几种方式的特点和优缺点
- linux下C语言编译为汇编代码
- 如何阅读优秀源代码
- CSS中绝对定位解释
- Spark机器学习——互动出版网
- eclipse + Felix 开发环境搭建 bundle 开发与调试
- c# 连接本地的sdf数据库文件
- ORA-01461: 仅能绑定要插入 LONG 列的 LONG 值
- 协方差矩阵
- nginx部署
- 对象的比较及hashCode、equals方法的使用
- Oracle expdp/impdp导出导入命令及数据库备份(转)
- 算法竞赛入门经典:第八章 高效算法设计 8.2归并排序
- 淘宝取代分布式事务的方案