前言

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能够导入该服务接口。

  1. public interface IHelloService {
  2. String echo();
  3. }

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

  1. public class HelloServiceImplementation implements IHelloService {
  2. @Override
  3. public String echo() {
  4. return "This is hello service implementation";
  5. }
  6. }

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

  1. public class Activator implements BundleActivator {
  2.  
  3. private static BundleContext context;
  4. private ServiceRegistration<IHelloService> registerService;
  5.  
  6. static BundleContext getContext() {
  7. return context;
  8. }
  9.  
  10. @SuppressWarnings("unchecked")
  11. public void start(BundleContext bundleContext) throws Exception {
  12. Activator.context = bundleContext;
  13.  
  14. Dictionary<String, String> properties = new Hashtable<String, String>();
  15. properties.put("service.exported.interfaces", "*");
  16. properties.put("service.exported.configs", "org.apache.cxf.ws");
  17. properties.put("org.apache.cxf.ws.address", "http://localhost:9090/hello");
  18. registerService = (ServiceRegistration<IHelloService>) context.registerService(IHelloService.class.getName(), new HelloServiceImplementation(),properties);
  19. }
  20.  
  21. public void stop(BundleContext bundleContext) throws Exception {
  22. registerService.unregister();
  23. Activator.context = null;
  24. }
  25.  
  26. }

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

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

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

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

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

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

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

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

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

使用DOSGi在OSGi环境下发布Web Services的更多相关文章

  1. Linux环境下发布.net core

    一.安装Linux环境 1. 安装VM虚拟机和操作系统 VM虚拟工具安装的过程详见:http://blog.csdn.net/stpeace/article/details/78598333.直接按照 ...

  2. ASP.NET 多环境下配置文件web.config的灵活配置

    调试,发布Asp.net程序的时候,开发环境和发布环境的Web.Config往往不同,比如connectionstring等.如果常常有调试,发布的需求,就需要常常修改web.config文件,这往往 ...

  3. ASP.NET 多环境下配置文件web.config的灵活配置---转

    注意:本功能在.Net Core中已经不可用,暂时需手动修改web.config中的信息,或者将其设置在appsettings.XXX.json中,然后再使用web.config中的环境变量来制定使用 ...

  4. .NET Framework 项目多环境下配置文件web.config

    解决jenkins自动构建发布的问题,统一从git/svn库中获取项目文件,根据不同配置编译发布到多个运行环境中. 转自:https://www.cnblogs.com/hugogoos/p/6426 ...

  5. 将web容器置于OSGi框架下进行web应用的开发

    将web容器置于OSGi框架下,其实就是将web容器做成OSGi支持的Bundle,再安装到OSGi框架中,这里使用的是Jetty容器. 1.创建一个Eclipse插件项目,在此插件下创建一个WebR ...

  6. idea环境下SpringBoot Web应用引入JSP

    1. 环境 开发环境:idea2019.3 jkd版本:1.8 springboot版本:2.6.2 2. 引入JSP的步骤 2.1 新建工程,引入依赖 这里只是解析jsp,因此只需要引入spring ...

  7. centos6.5环境下的web项目mysql编码方式导致的中文乱码问题

    最近在centos6.5下部署web项目时网页出现中文乱码的问题,在排除掉php之后,把问题锁定在mysql的编码方式上. 解决方法如下: 首先进入mysql命令行,输入命令:SHOW VARIABL ...

  8. 用Python+Django在Eclipse环境下开发web网站【转】

    一.创建一个项目如果这是你第一次使用Django,那么你必须进行一些初始设置.也就是通过自动生成代码来建立一个Django项目--一个Django项目的设置集,包含了数据库配置.Django详细选项设 ...

  9. 【转载】django在eclipse环境下建web网站

    一.创建一个项目如果这是你第一次使用Django,那么你必须进行一些初始设置.也就是通过自动生成代码来建立一个Django项目--一个Django项目的设置集,包含了数据库配置.Django详细选项设 ...

随机推荐

  1. gradle 删除指定目录中的文件和目录

    // 删除bakAPk下的所有非母包文件 task deleTask(type: Delete){ FileTree tree = fileTree(dir: bakPath) tree.each { ...

  2. C++11_shared_ptr

    版权声明:本文为博主原创文章,未经博主允许不得转载. shared_ptr智能指针的一种,它的使用类似于auto_ptr. shared_ptr它有两个指针,一个指向引用计数,一个指向data.由于拥 ...

  3. New Concept English there (4)

    20w/m These days, people who do manual work often receive far more money than people who work in off ...

  4. LaText中插入带上下限的求和符号

    效果如下: LaTex命令如下: \begin{equation} \label{8} z_{i}(k+1)=\sum_{j\in N_{i}(k)} a_{ij}(k)z_{i}(k),z_{i}( ...

  5. c++下基于windows socket的服务器客户端程序(基于UDP协议)

    前天写了一个基于tcp协议的服务器客户端程序,今天写了一个基于UDP协议的,由于在上一篇使用TCP协议的服务器中注释已经较为详细,且许多api的调用是相同的,故不再另外注释. 使用UDP协议需要注意几 ...

  6. 表单隐藏域与display:none

    有时候前端进行表单填写是分步骤的,每一步的时候其他步骤相关的表单视图不可见: 针对"不可见",以下有两种处理方式: ①display:none 这种方式呢,比较简单,就是将三个步骤 ...

  7. 依存可视化︱Dependency Viewer——南京大学自然语言处理研究组

    来源网页:http://nlp.nju.edu.cn/tanggc/tools/DependencyViewer.html 视频演示网页:http://nlp.nju.edu.cn/tanggc/to ...

  8. TeamTalk源码分析(十一) —— pc客户端源码分析

           --写在前面的话  在要不要写这篇文章的纠结中挣扎了好久,就我个人而已,我接触windows编程,已经六七个年头了,尤其是在我读研的三年内,基本心思都是花在学习和研究windows程序上 ...

  9. DevExpress GridControl 显示外部图片

    如果数据源中只包含图片的链接,如何在DevExpress GridControl的一列中显示外部图片? 要实现该功能,可通过非绑定列的方式来实现.具体实现方法如下: 1.    创建了一个非绑定列并设 ...

  10. SLIP 协议

    SLIP 协议 SLIP 英文原义:Serial Line Internet Protocol 中文释义:串行线路网际协议 注解:该协议是Windows远程访问的一种旧工业标准,主要在Unix远程访问 ...