使用DOSGi在OSGi环境下发布Web Services

前言

Apache CXF是一个开源的服务框架项目,而Distributed OSGi子项目提供了基于OSGi远程服务规范的分布式组件实现。它使用Web Services,HTTP上的SOAP手段实现了远程服务的功能,对外暴露了WSDL规约。本篇就是介绍使用dosgi在OSGi环境下将OSGi的服务暴露成Web Services的过程。

 

DOSGi的项目主页:http://cxf.apache.org/dosgi-single-bundle-distribution.html

环境搭建

DOSGi本身提供了三种实现:Apache Karaf Feature,Multi Bundle Distribution和Single Bundle Distribution,这里为了介绍方便,选择了最简单的Single Bundle Distribution,它提供了一个单独的bundle,其中内置了所有需要的依赖,下载后解压,找到其中的文件cxf-dosgi-ri-singlebundle-distribution-1.4.0.jar。

 

 

 

在eclipse(必须包含PDE环境)中Import->Plug-in Development->Plug-ins and Fragements,选择刚才下载dosgi的目录,选择对应的bundle,Finish进行导入之后,该bundle就已经在工作区中并可以使用。




程序实例

这里我们建立几个最简单的实例,这个实例中有3个bundle:

bundle名称

用途

com.clamaa.dosgi.sample.service

服务接口

com.clamaa.dosgi.sample.implementation

服务实现

 

首先,在bundle:com.clamaa.dosgi.sample.service中声明服务接口IHelloService,并将该Service所在的包Export出来,以便服务实现的bundle能够导入该服务接口。

public interface IHelloService {
   String echo();
}

第二步,在bundle: com.clamaa.dosgi.sample.implementation中导入service bundle中导出的接口包,并进行简单的实现:

public class HelloServiceImplementation implements IHelloService {
	@Override
	public String echo() {
		return "This is hello service implementation";
	}
}

 

在Activator中注册服务,注意使用dosgi时需要设置相应的属性properties,其中的url:http://localhost:9090/hello就是web服务的地址。

public class Activator implements BundleActivator {

	private static BundleContext context;
	private ServiceRegistration<IHelloService> registerService;

	static BundleContext getContext() {
		return context;
	}

	@SuppressWarnings("unchecked")
	public void start(BundleContext bundleContext) throws Exception {
		Activator.context = bundleContext;
		
		Dictionary<String, String> properties = new Hashtable<String, String>();
		properties.put("service.exported.interfaces", "*");
		properties.put("service.exported.configs", "org.apache.cxf.ws");
		properties.put("org.apache.cxf.ws.address", "http://localhost:9090/hello");
		registerService = (ServiceRegistration<IHelloService>) context.registerService(IHelloService.class.getName(), new HelloServiceImplementation(),properties);
	}

	public void stop(BundleContext bundleContext) throws Exception {
		registerService.unregister();
		Activator.context = null;
	}

}

 

在eclipse中进行调试,新建Debug Configuration,注意这里我们使用内置的jetty作为web服务器对web服务进行发布:

 

 

启动后,控制台输出信息,表示服务发布成功。

osgi> 六月 13, 2014 3:42:40 下午 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://service.sample.dosgi.clamaa.com/}IHelloService from class com.clamaa.dosgi.sample.service.IHelloService
六月 13, 2014 3:42:40 下午 org.apache.cxf.endpoint.ServerImpl initDestination
信息: Setting the server's publish address to be http://localhost:9090/hello

 

我们可以在浏览器中查看wsdl:

 

 

下面,我们使用Soap UI对该web service进行测试,可以查看其输出,表明该web服务工作正常。

 


 

我们刚才发布的服务是硬编码完成的,其实还可以通过声明式服务(Declarative Service)来将OSGi中的服务发布成Web Service,此时,声明式服务的配置文件:

 

<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="com.clamaa.dosgi.sample.implementation">
   <implementation class="com.clamaa.dosgi.sample.implementation.HelloServiceImplementation"/>
   <property name="service.exported.interfaces" value="*" />
   <property name="service.exported.configs" value="org.apache.cxf.ws" />
   <property name="org.apache.cxf.ws.address" value="http://localhost:9090/hello" />
   <service>
      <provide interface="com.clamaa.dosgi.sample.service.IHelloService"/>
   </service>
</scr:component>

 

同样可以正常发布Web服务并正常访问。

 

本篇只是DOSGi的一个入门介绍,其中大部分内容也是根据其官网的示例一步步操作完成的,对于复杂的服务(带参数以及复杂类型返回值),请查看其官网相关资料深入研究。

 

 

猜你喜欢

转载自brandnewuser.iteye.com/blog/2079495