前言

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

使用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. elasticsearch关于索引切分的实现

    [背景信息] ES一直以来对于已经创建好的索引的分片是不可以进行分割的,简单的说,当你创建了一个索引,并指定了number_of_shards为2,当随着数据量的不断增大,是无法将索引的shard扩充 ...

  2. L159

    Waves are the children of the struggle between ocean and atmosphere, the ongoing signatures of infin ...

  3. BASE理论

    BASE是Basiclly Available(基本可用),Soft state(软状态),Eventually consistent(最终一致性)三个短语的缩写. BASE是对CAP中一致性和可用性 ...

  4. Cannot mix incompatible Qt library (version 0x40805) with this library (version 0x40801)

    问题描述 今天运行我的 linux 上的 go 语言 IDE liteide 突然报错,错误如下: Cannot mix incompatible Qt library (version 0x4080 ...

  5. 解决错误:This bundle is invalid - The file extension must be .zip

    近期在 iOS 开发中遇到了一个很蛋疼的问题,Xcode 工程中接入了iMessage 壁纸的功能后,每次上传 ipa 包都会显示此包无效,并报上述的错误描述:This bundle is inval ...

  6. 国内知名的自然语言处理(NLP)团队

    工业界 腾讯人工智能实验室(Tencent AI Lab) 百度自然语言处理(Baidu NLP):对外提供了百度AI开放平台,王海峰(现任百度副总裁,AI技术平台体系AIG总负责人) 微软亚洲研究院 ...

  7. UICollectionView功能使用

    UICollectionView在2012年被提出,已经不是什么新技术了,在此只是做一下简单的实现. 集合视图:UICollectionViewUICollectionView和UITableView ...

  8. learn go passing variable-length arguments

    package main // 参考文档: // https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/06.3.md im ...

  9. confd与etcd的使用

    Add keys This guide assumes you have a working etcd, or consul server up and running and the ability ...

  10. oracle中查询结果集为空,则得到一个默认值

    有同事问我上述问题,我把我的实现思路写出来.子查询把查询的结果和默认的结果全部显示.父查询通过伪列rownum来筛选,如果查询有结果,就有几条就显示几条,而不去显示子查询中的默认值:如果查询没有结果, ...