osgi实战项目(osmp)一步一步玩转osgi之服务发布和引用(4)

阅读更多

 

今天主要讲一下在osgi环境下的服务注册、和服务引用。

 

其实osgi只是一个java动态化、模块化的一系列规范,根据不同厂商制定了不同的规范,如Felix和Equinox则分别是Apache和Eclipse开源社区给出的标准规范的实现!而osgi的魅力也在于动态化和模块化,我个人认为要实现动态化最简单的就是通过classload动态加载。我个人理解而已,osgi只是在传统开发的基础上抽象了一个bundle组件的概念,每一个bundle组件都有自己的生命周期,bundle实现动态化是基于bundle classloader实现的。理论性的东西我不想讲太多,大家可以去看一下osgi基础方面的东西,如bundle生命周期,bundle classloader等内容。而服务的注册,服务的引用等内容恰恰是bundle生命周期最好的诠释!

 

先说一下osgi环境下的服务注册,其实这部分内容在第二讲的时候已经大概说过一点儿,这次我们讲解一下传统的osgi服务注册和使用框架的方式来注册等。

 

 一个bundle被部署到osgi容器里有几个最要的阶段也可以称之为bundle的生命周期,主要包括 INSTALLED(调用BundleContext.installBundle()后,会创建一个INSTALLED状态的bundle)、RESOLVED(如果其所依赖的所有bundle都存在,即解析成功,转到RESOLVED状态)、STARTING(当开始执行BundleActivator.start(),则处于STARTING状态)、ACTIVE(当执行BundleActivator.start()成功,则转到ACTIVE状态)、STOPPING(当开始执行BundleActivator.stop(),则处于STOPPING状态)、UNINSTALLED(INSTALLED状态的bundle可以被卸载,转到UNINSTALLED状态)。

可以看到一个bundle的整个生命周期都离不开BundleActivatorBundleContext 这两个类。

 

BundleContext:Bundle上下文,主要功能是与OSGI容器交互,包括服务的注册,监听器的注册,获取osgi下所有发布的服务,服务的引用等功能。建议大家好好的看一下这个类。

BundleActivator:BundleActivator是osgi提供的一个获取BundleContext的接口,同时提供bundle启动和停止的处理方法。

 

知道这两个类我们就可以实现BundleActivator 在bundle启动的时候通过BundleContext注册服务到osgi容器里以供其它bundle调用了


实现一个自定义的BundleActivator :

 

 public class HelloServiceActivator implements BundleActivator {
	ServiceRegistration serviceRegistration;
	@Override
	public void start(BundleContext context) throws Exception {
		HelloService helloService = new HelloServiceImpl();
		serviceRegistration = context.registerService(HelloService.class.getName(), helloService, null);
	}

	@Override
	public void stop(BundleContext context) throws Exception {
		serviceRegistration.unregister();
	}

}

 

在这个bundle启动的时候我们通过实现BundleActivator里的start方法 实例化一个服务类并通过BundleContext的registerService注册到osgi容器里,分别传入服务名称,服务实例,服务别名(可以理解为这个的tag标签),通过stop方法注销到这个服务。

 

完成这一步后我们还在MANIFEST.MF(Bundle元数据描述文件,定义此Bundle依赖的jar包,导入和导出的包等基础元数据)定义Bundle-Activator为我们自己的Activator

Bundle-Activator:com.osmp.demo.HelloServiceActivator

 

 这样我们在将此bundle部署到osgi容器的时候在bundle启动的时候就会将HelloService注册到osgi容器里供别的bundle服务调用。再简单的说一下bundle服务的引用。与发布基本相同,在启动的时候通过start方法获取到bundle上下文,通过BundleContext获取ServiceReference 服务引用,再通过此类获取我们需要的服务引用,代码如下:

 

public class Activator implements BundleActivator {
ServiceReference helloServiceReference;
	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());
	}
	public void stop(BundleContext context) throws Exception {
		System.out.println("Goodbye World!!");
		context.ungetService(helloServiceReference);
	}
}

 

 以上部分写到一大半的时候,想找两个例子,结果发现 http://longdick.iteye.com/blog/457310 这篇文章讲的很好,大家可以看一下,我也就不再专门讲解了。

 

我就说一下在osmp里是怎么实现服务的注册和服务的发现和服务路由的。

 

osmp里服务的发布我们是通过spring-dm来发布的,代码很简单,

 spring配置文件里引入spring-osgi的schema,通过 标签来将服务发布到osgi容器里,配置文件如下:

 

 



	
	
	
	
	
	
	
	
		
			
			
		
	


 

pom.xml 里依赖

 


            org.springframework.osgi
            spring-osgi-core
        
        
            org.osgi
            org.osgi.core
 

 

pom.xml将工程打包为bundle,需要使用到 maven-bundle-plugin 这个插件:

 

	
		
			
				org.apache.felix
				maven-bundle-plugin
				true
				
					
						${project.artifactId}
						
						
							org.springframework.aop,
							org.springframework.aop.framework,
							org.springframework.cglib,
							org.springframework.cglib.proxy,
							org.springframework.cglib.core,
							org.springframework.cglib.reflect,
							org.aopalliance.aop,
							org.aopalliance.intercept,
							*;resolution:=optional
						
					
				
			
		
	

 

 

 PS:

1、配置文件引入spring-osgi  xmlns:osgi="http://www.springframework.org/schema/osgi" http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi-1.2.xsd

2、osgi:service-properties 标签对应 上面讲到的context.registerService()方法的第三个参数。我的理解是给这个服务打上tag标签,以k/v键值对的形式。

3、maven-bundle-plugin打bundle时,我这里使用的servicemix作为osgi容器,已经集成了spring,此时需要将spring的包显示的引入进来。这里比较坑的是我原本以为通过*;resolution:=optional能自动的将需要依赖的包给import进来,但是试过多次发现,只有在代码里显示的import的包才会被自动的引进去,如配置文件里依赖的包是不会自动的import进来的,而且只能一级一级的import进来。

4、spring-dm 服务引用通过这里不作详讲。

 

上面的例子可以查看osmp-demo源码!!!!

相对来说通过spring-dm的方式来发布服务比上面通过BundleContext的方式前进了一大步,此外还可以通过blueprint的方式来发布服务,感兴趣的自己百度。
 

 

 

你可能感兴趣的:(osgi,服务注册,BundleActivator,BundleContext,soa)