前言

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

最新文章

  1. Cordova - 与iOS原生代码交互2(使用Swift开发Cordova的自定义插件)
  2. Linux内核-模块编译和安装
  3. LInux SSH远程文件/目录传输命令scp(转载)
  4. 轻松学Linux之使用转义字符
  5. 初涉Node.js
  6. 利用Javascript获取当前日期的农历日期
  7. Java中的NIO和IO的对比分析
  8. Mindjet MindManager 2012 从模板创建出现“Runtime Error pure virtual function call” 解决方法
  9. 【HDOJ】4932 Miaomiao&#39;s Geometry
  10. Android学习之Activity之间的数据传递
  11. Oracle定时执行存储过程(转)
  12. .net调用Outlook 批量发送邮件,可指定Outlook中的账号来发送邮件
  13. ReactiveCocoa应用篇(一)
  14. 【转】一则使用WinDbg工具调试iis进程调查内存占用过高的案例
  15. https浅析
  16. 是否只查看安全传送的网页内容? 去掉 IE弹出窗口
  17. Axel and Marston in Bitland CodeForces - 782F (bitset优化)
  18. Gitlab Issue Tracker and Wiki(二)
  19. Django接受ajax传过来的数组
  20. Python解析JSON数据的基本方法

热门文章

  1. vue开发环境搭建Mac版
  2. 策略模式-Java实现
  3. 联想THINKPAD E40的快捷键怎么关闭?哪些F1 F2 F3的键我需要用到 但是每次都按FN 太烦人了
  4. CSS样式让元素填充剩余部分为自己的高度或宽度
  5. MySQL 存储过程,游标,临时表创建
  6. mdev USB disk auto mount demo
  7. java 输入输出流 关于InputStream 和 OutputSteam 实现文本一行一行读入和 文本一行一行输出
  8. ueditor使用小结【来源网络】
  9. new Date()相关获取当月天数和当月第一天
  10. (转载)从MVC到前后端分离