本文简单记录felix的环境的搭建及简单部署
1.eclipse搭建felix运行环境
2.开发plug-in project简单实例
3.plug-in project间通信简单实例-订单
环境准备:
下载felix,最新版本felix5.6.4需要jdk1.8环境
http://mirrors.hust.edu.cn/apache//felix/org.apache.felix.main.distribution-5.6.4.zip
shell启动felix
1.eclipse搭建felix运行环境
1.1 创建普通java app,修改编译文件位置
![](http://dl2.iteye.com/upload/attachment/0125/6926/bebe463a-e8eb-3b85-abbf-c1e8c0ccdbf3.png)
1.2 将felie framework下的conf,bundle,bin文件夹放到上面创建的项目的根目录下
![](http://dl2.iteye.com/upload/attachment/0125/6930/6509ae73-ec2f-3855-8c42-e098bf32630a.png)
1.3 配置classpath:将bin/felix.jar添加到运行环境
1.4 配置Run Configurations
new一个Java Application,并选择Main class如下图所示:
![](http://dl2.iteye.com/upload/attachment/0125/6952/3851f08c-2879-34c1-b1b5-59e5df796f9c.png)
1.5 运行
![](http://dl2.iteye.com/upload/attachment/0125/6960/4fd4d0c2-09ef-3940-80cb-0640874bba80.png)
2.开发plug-in project
2.1 创建插件工程(plug-in project)
![](http://dl2.iteye.com/upload/attachment/0125/6962/c9130595-9756-3dab-a8bf-a335c9419351.png)
![](http://dl2.iteye.com/upload/attachment/0125/6966/5c0199a2-b372-31c1-87e4-f4f4866e84e1.png)
![](http://dl2.iteye.com/upload/attachment/0125/6968/789dc66c-e251-34b4-abc7-f40ae76b490f.png)
修改生成的Activator.java
package com.byron; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; public class Activator implements BundleActivator { private static BundleContext context; static BundleContext getContext() { return context; } /* * (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..."); } /* * (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..."); } }
2.2 导出plug-in project
![](http://dl2.iteye.com/upload/attachment/0125/6970/b3d183b5-cbea-3ba9-a78f-a18da4f9193f.png)
![](http://dl2.iteye.com/upload/attachment/0125/6972/edf738c4-df6a-366c-93f8-8baf8749876b.png)
![](http://dl2.iteye.com/upload/attachment/0125/6974/11e8f144-e3d4-3fd3-95f0-a018a547cb8c.png)
2.3 安装运行
在步骤1中启动felix
#安装bundle
install plugins/osgi-felix-plug1_1.0.0.201706262218.jar
#启动bundle
start bundle-id
#显示bundle状态
lb
![](http://dl2.iteye.com/upload/attachment/0125/6976/a48cec30-7378-3d9e-8b1b-6170ed4be3a4.png)
3.plug-in project间通信简单实例-订单
3.1 创建服务端程序(bundle)-order
Activator.java:
package com.byron; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceRegistration; public class Activator implements BundleActivator { private static BundleContext context; private ServiceRegistration registration; static BundleContext getContext() { return context; } /* * (non-Javadoc) * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext) */ public void start(BundleContext bundleContext) throws Exception { Activator.context = bundleContext; registration = context.registerService(OrderService.class.getName(), new OrderServiceImpl(), null); System.out.println("Order Service registered"); } /* * (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("Order Service stopped"); } }
OrderServiceImpl.java:
package com.byron; public class OrderServiceImpl implements OrderService { @Override public void processOrder() { // TODO Auto-generated method stub System.out.println("Order id: ORDER-TEST") ; } }
导出,并暴露服务接口(配置MANIFEST.MF)供其他bundle调用
MANIFEST.MF:
Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: Osgi-felix-order Bundle-SymbolicName: osgi-felix-order Bundle-Version: 1.0.0.qualifier Bundle-Activator: com.byron.Activator Bundle-Vendor: byron Bundle-RequiredExecutionEnvironment: JavaSE-1.8 Import-Package: org.osgi.framework;version="1.3.0" Export-Package: com.byron
Export-Package: com.byron导出接口服务
3.2 创建客户端程序(bundle)-client
同3.1创建客户端bundle
OrderClient.java:
package com.byron; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; import org.osgi.util.tracker.ServiceTracker; public class OrderClient implements BundleActivator { private ServiceTracker orderTracker; private OrderService orderService; public void setService(OrderService orderService) { this.orderService = orderService; } public void removeService() { this.orderService = null; } public void start(BundleContext context) throws Exception { orderTracker = new ServiceTracker(context, OrderService.class.getName(), null); orderTracker.open(); OrderService order = (OrderService) orderTracker.getService(); if (order == null) { System.out.println("Order service not available"); } else { order.processOrder(); } } public void stop(BundleContext context) { System.out.println("Bundle stopped"); orderTracker.close(); } }
导出,并配置MANIFEST.MF(引入order服务包)
Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: Osgi-felix-order-client Bundle-SymbolicName: osgi-felix-order-client Bundle-Version: 1.0.0.qualifier Bundle-Activator: com.byron.Activator Bundle-Vendor: byron Bundle-RequiredExecutionEnvironment: JavaSE-1.8 Import-Package: org.osgi.framework;version="1.3.0", org.osgi.util.tracker, com.byron
Import-Package: org.osgi.framework;version="1.3.0", org.osgi.util.tracker, com.byron
引入服务包,以便被发现(如果这2个bundle在同一个应用下,就相当于import就行)
参考:
OSGi 和 Spring,第 1 部分: 使用 Apache Felix 构建和部署 OSGi 包
https://www.ibm.com/developerworks/cn/webservices/ws-osgi-spring1/
http://blog.csdn.net/xo_zhang/article/details/9192345
eclipse中利用Felix调试osgi HelloWorld
http://blog.csdn.net/wobendiankun/article/details/24876257
http://zqc-0101.iteye.com/blog/1153291
http://zqc-0101.iteye.com/blog/1183452