osgi学习-felix

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

猜你喜欢

转载自newjava-sina-cn.iteye.com/blog/2381361