前言

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. php远程文件包含截断问题

    今天在学习<白帽子讲web安全>一书是,提到一个php远程文件包含漏洞 可以从攻击者服务器中的一个写好的攻击脚本中远程执行命令 服务器中有漏洞的页面代码为: #test.php#error ...

  2. easychm生成帮助文件时出现的目录导航乱码问题

    将html生成帮助文件时出现乱码问题的主要原因是:文件编译格式的问题 (一般的网页都是utf-8格式的,将其改为GB2312就可以了):

  3. Android程序员学WEB前端(8)-CSS(3)-盒子内联块级定位浮动-Sublime

    转载请注明出处:http://blog.csdn.net/iwanghang/article/details/76618473 觉得博文有用,请点赞,请评论,请关注,谢谢!~ 盒子模型: <!D ...

  4. Unity 2d 的 SpriteMask为游戏表现带来多种可能性

    孙广东  2017.7.22 http://blog.csdn.NET/u010019717           SpriteMask 是Unity 2017.1 开始添加2d功能!,    Spri ...

  5. ansible资料

    ansible系列教程-强烈推荐看完 ansible官方编写的例子 ansible_ui Jenkins配置ansible galaxy 官方文档 中文教程1 中文教程2 playbook进阶 YAM ...

  6. KL散度(Kullback-Leibler_divergence)

    KL散度(Kullback-Leibler_divergence) 一. 概念 KL-divergence,俗称KL距离,常用来衡量两个概率分布的距离. 根据shannon的信息论,给定一个字符集的概 ...

  7. MPAndroidChart Wiki(译文)~Part 4

    16. 动画 注意:本章的动画效果只会在API 11(Android3.0.x)及以上的Android版本上生效 在低于上述的Android版本中,动画将不会被执行,并不会导致程序崩溃. 所有类型的图 ...

  8. iOS开发-UITextView文字排版

    UITextView文本排版 1.配置NSMutableParagraphStyle NSMutableParagraphStyle *MParaStyle = [[NSMutableParagrap ...

  9. Windows批处理笔记

    1. 路径类相关代号 %i提取第i个命令选项,例如%1提取第1个option,i可以取值从1到9 %~0: 取文件名(名+扩展名) %~f0:取全路径 %~d0:取驱动器名 %~p0:只取路径(不包驱 ...

  10. 条件和循环(More Control Flow Tools)

    1.if语句 >>>a=7 >>> if a<0: ... print 'Negative changed to zero' ... elif a==0: . ...