OSGI (Open Service Gateway Initiative)
历史:
1999年发起OSGI,开始只出现在嵌入式系统和网络设备市场,2004年IBM发布采用OSGI搭建eclipse3.0,不再使用原来的插件体系,WebSphere 6.1 也全面改用 OSGI;JBoss、WebLogic、Spring DM,甚至是 BMW 车的控制系统中都得到了很好的应用。
Eclipse3.0推翻了以前的插件体系结构,直接采用OSGI作为其插件体系结构
Eclipse插件体系结构和OSGI都强调微核+系统插件+应用插件
BMW汽车应用控制系统(2006年),用来控制汽车上的音箱、灯光等等设备,共由1000多个Bundle构成,启动时间3.5秒
插件比模块更独立,有自描述文件MANIFEST.MF,用于插件容器。
好处:热插拔,动态插件化,使用OSGI容器管理web应用模块间的交叉依赖,可以在不用重启前提下,更新DAO层。
OSGI是个规范,常见的开源OSGI有:
1,Equinox容器是参照OSGi规范第4版实现的,它构成了Eclipse IDE的核心—模块化的Java运行时
2,Knoflerfish
3,Apache的Felix Karaf
OSGI把资源称作Bundle,框架从概念上可以分为三层:
Module Layer 共享Bundle包和代码;
Bundle是OSGi中的基本组件,表现形式仍然为Jar包,META-INF/MANIFEST.MF作为其元数据
Lifecycle Layer 管理Bundle的运行时生命周期;
状态:installed,resolved,uninstalled,starting,active,stopping
Service Layer 服务层主要涉及模块之间的交互和通信。
实例____________________________________________________________
环境:在本文附件或http://archive.eclipse.org/equinox/drops/R-3.4.2-200902111700/index.php下载eclipse-equinox-SDK-3.4.2.zip
例子,hello world未实现,报MESSAGE Problems occurred when invoking code from plug-in: "org.eclipse.equinox.preferences".
一.创建一个bundle
1.创建一个eclipse plug-in development工程 file->new->project
工程名为com.osgi.sample.HelloWorld
目标平台:osgi framework->standard
2.next默认配置就可以了
3.选择Hello OSGI Bundle,完成即可
这时会在com.osgi.sample.helloworld包在创建一个默认的Activator.java文件
public class Activator implements BundleActivator {
/*
* (non-Javadoc)
* @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
*/
public void start(BundleContext context) throws Exception {
System.out.println("Hello World!!");
}
/*
* (non-Javadoc)
* @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
*/
public void stop(BundleContext context) throws Exception {
System.out.println("Goodbye World!!");
}
}
在META-INF下创建一个MANIFEST.MF文件
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: Helloworld Plug-in
Bundle-SymbolicName: com.javaworld.sample.helloworld
Bundle-Version: 1.0.0
Bundle-Activator: com.javaworld.sample.helloworld.Activator
Bundle-Vendor: JAVAWORLD
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Import-Package: org.osgi.framework;version="1.3.0"
4.如果你想关注在bundle启动,停止.你就要创建一个类实现BundleActivator接口
BundleActivator类必须提供一个公共的无参构造方法,osgi framework会创建一个对像通过Class.newInstance();
容器会调用你的Activator start()启动bundle,这个bundle可能获取某些资源如数据库连接.这个方法有一个参数
BundleContext,他允许这些bundles与框架进行访问.
stop()方法将停止一个bundle,这时你可以释放资源.
5.MANIFEST.MF中各属性含义网上一大把.
6.执行这个bundle
run->run configuration->osgi framework ->bundles下面的workspace选中hellworld 执行
这时osgi的控制台就会打印Hello World!!
osgi控制台命令
ss:列出所有安装过的bundles和他们的状态
start <bundleid> :启动指定bundle
stop <bundleid>: 停止指定bundle
install <bundleURL>:安装一个bundle到osgi容器
uninstall <bundleid>:卸载指定的bundle
二.依赖管理
osgi允许你插入多个模块然后独立的管理他们.默认情况下任何bundle对其它的都是不可见的.一个bundle想调用另一个
bundle就需要导包
和上面的一样我们创建另一个bundle com.osgi.sample.HelloService.并且创建一个接口com.osgi.sample.service.
HelloService.java
public interface HelloService {
public String sayHello();
}
创建他的一个实现类HelloServiceImpl
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello() {
System.out.println("inside HelloServiceImpl.sayHello()");
return "say Hello";
}
}
打开MANIFEST.MF文件.增加Export-Package: com.javaworld.sample.service
这样容器中的com.javaworld.sample.service包中的HelloService bundle就可以被外面访问.
我们的helloworld bundle需要使用这个.此时修改他的MANIFEST.MF文件
Import-Package: com.javaworld.sample.service,...
创建类com.javaworld.sample.service.impl.HelloServiceActivator.java
public class HelloServiceActivator implements BundleActivator {
ServiceRegistration helloServiceRegistration;
public void start(BundleContext context) throws Exception {
HelloService helloService = new HelloServiceImpl();
helloServiceRegistration = context.registerService(HelloService.class.getName(), helloService , null);
}
public void stop(BundleContext arg0) throws Exception {
helloServiceRegistration.unregister();
}
}
1.我们要注册的接口,如果你要注册一个接口的多个服务,你必须传递一个String数组做为第一个参数.
2.HelloServiceImpl是你真正要注册的对象.
3.修改MANIFEST.MF 在HelloService bundle;Bundle-Activator:com.javaworld.sample.service.impl.HelloServiceActivator
现在HelloService bundle以经导入了HelloServiceImpl ,当osgi容器启动时,HelloService bundle 将控制交给HelloServiceActivator,
接下来是创建服务消费者,我们修改Activator.java,使用HelloService 服务
public class Activator implements BundleActivator {
ServiceReference helloServiceReference;
HelloServiceTracker helloServiceTracker;
/*
* (non-Javadoc)
* @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
*/
public void start(BundleContext context) throws Exception {
System.out.println("Hello World!!");
helloServiceReference = context.getServiceReference(HelloService.class.getName());
HelloService helloService = (HelloService) context.getService(helloServiceReference);
System.out.println(helloService.sayHello());
}
/*
* (non-Javadoc)
* @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
*/
public void stop(BundleContext context) throws Exception {
System.out.println("Goodbye World!!");
context.ungetService(helloServiceReference);
}
}
BundleContext.getServiceReference()返回一个ServiceReference对象,如果一个接口有多个服务存在,他将返回最高级别的对象.
三,创建服务工厂
我们创建一个类实现ServiceFactory接口并且注册来代替具体的服务对象
com.javaworld.sample.service.HelloServiceFactory.java
public class HelloServiceFactory implements ServiceFactory {
private int usageCounter = 0;
public Object getService(Bundle bundle, ServiceRegistration registration) {
System.out.println("create object of HelloService for"+bundle.getSymbolicName());
usageCounter++;
System.out.println("number of bundles using service "+usageCounter);
HelloService helloService = new HelloServiceImpl();
return helloService;
}
public void ungetService(Bundle bundle, ServiceRegistration registration, Object service) {
System.out.println("release object of HelloService for "+bundle.getSymbolicName());
usageCounter--;
System.out.println("number of bundles using service "+usageCounter);
}
}
1.getService():osgi框架第一次调用这个方法通过BundleContext.getService(ServiceReference)方法.
2.ungetService():osgi框架调用此方法在服务取消的时候
修改HelloServiceActivator的start()方法
public class HelloServiceActivator implements BundleActivator {
ServiceRegistration helloServiceRegistration;
public void start(BundleContext context) throws Exception {
HelloServiceFactory helloServiceFactory = new HelloServiceFactory();
helloServiceRegistration = context.registerService(HelloService.class.getName(), helloServiceFactory , null);
}
public void stop(BundleContext arg0) throws Exception {
helloServiceRegistration.unregister();
}
}
四 跟踪服务
1.修改MANIFEST.MF helloWorld bundle 导入org.osgi.util.tracker包
2.创建HelloServiceTracker.java
public class HelloServiceTracker extends ServiceTracker {
public HelloServiceTracker(BundleContext context) {
super(context, HelloService.class.getName() , null);
}
@Override
public Object addingService(ServiceReference reference) {
System.out.println("inside HelloServiceTracker.addingService "+reference.getBundle());
return super.addingService(reference);
}
@Override
public void removedService(ServiceReference reference, Object service) {
System.out.println("inside HelloServiceTracker.removedService "+reference.getBundle());
super.removedService(reference, service);
}
}
3.修改Activator.java用HelloServiceTracker
public class Activator implements BundleActivator {
ServiceReference helloServiceReference;
HelloServiceTracker helloServiceTracker;
/*
* (non-Javadoc)
* @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
*/
public void start(BundleContext context) throws Exception {
System.out.println("Hello World!!");
helloServiceTracker = new HelloServiceTracker(context);
helloServiceTracker.open();
helloServiceReference = context.getServiceReference(HelloService.class.getName());
HelloService helloService = (HelloService) context.getService(helloServiceReference);
System.out.println(helloService.sayHello());
}
/*
* (non-Javadoc)
* @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
*/
public void stop(BundleContext context) throws Exception {
System.out.println("Goodbye World!!");
context.ungetService(helloServiceReference);
helloServiceTracker.close();
}
}