osgi有4个比较出名的实现框架:Knopflerfish, Apache Felix, Equinox, Spring DM
本文简单记录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,修改编译文件位置
1.2 将felie framework下的conf,bundle,bin文件夹放到上面创建的项目的根目录下
1.3 配置classpath:将bin/felix.jar添加到运行环境
1.4 配置Run Configurations
new一个Java Application,并选择Main class如下图所示:
1.5 运行
2.开发plug-in project
2.1 创建插件工程(plug-in project)
修改生成的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
2.3 安装运行
在步骤1中启动felix
#安装bundle
install plugins/osgi-felix-plug1_1.0.0.201706262218.jar
#启动bundle
start bundle-id
#显示bundle状态
lb
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
- 大小: 67.9 KB
- 大小: 23.8 KB
- 大小: 101.2 KB
- 大小: 38.2 KB
- 大小: 61.3 KB
- 大小: 54.3 KB
- 大小: 51.6 KB
- 大小: 122.5 KB
- 大小: 68.7 KB
- 大小: 17.1 KB
- 大小: 35.8 KB
分享到:
相关推荐
maven-osgi-plugin-launcher-framework-felix-1.0.21.jar
maven-osgi-plugin-launcher-framework-felix-1.0.20.jar
maven-osgi-plugin-launcher-framework-felix-1.0.19.jar
maven-osgi-plugin-launcher-framework-felix-1.0.18.jar
maven-osgi-plugin-launcher-framework-felix-1.0.17.jar
maven-osgi-plugin-launcher-framework-felix-1.0.16.jar
maven-osgi-plugin-launcher-framework-felix-1.0.15.jar
maven-osgi-plugin-launcher-framework-felix-1.0.14.jar
maven-osgi-plugin-launcher-framework-felix-1.0.13.jar
Felix是一个遵循OSGi规范的轻量级、模块化的Service Oriented Runtime,由Apache软件基金会支持,它允许在运行时动态地安装和卸载OSGi模块。Felix是OSGi容器的一个实现,是Java开发者在进行模块化开发时的常用工具。...
1. **环境准备**:安装一个支持OSGi的运行时环境,比如Apache Felix或Equinox,以及相应的集成开发环境(IDE),如Eclipse,它有一个内置的OSGi插件。 2. **创建bundle**:创建一个新的Java项目,并将其转换为OSGi ...
jar包,官方版本,自测可用
jar包,官方版本,自测可用
jar包,官方版本,自测可用
jar包,官方版本,自测可用
这本书为想要入门OSGi和Apache Felix的开发人员提供了一个详细的学习路径,涵盖了基础知识到实践应用的各个环节。学习完这本书之后,开发人员将能够使用Felix框架构建自己的模块化应用程序,并理解OSGi技术如何在...
jar包,官方版本,自测可用
jar包,官方版本,自测可用
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装