使用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的一个入门介绍,其中大部分内容也是根据其官网的示例一步步操作完成的,对于复杂的服务(带参数以及复杂类型返回值),请查看其官网相关资料深入研究。
使用DOSGi在OSGi环境下发布Web Services的更多相关文章
- Linux环境下发布.net core
一.安装Linux环境 1. 安装VM虚拟机和操作系统 VM虚拟工具安装的过程详见:http://blog.csdn.net/stpeace/article/details/78598333.直接按照 ...
- ASP.NET 多环境下配置文件web.config的灵活配置
调试,发布Asp.net程序的时候,开发环境和发布环境的Web.Config往往不同,比如connectionstring等.如果常常有调试,发布的需求,就需要常常修改web.config文件,这往往 ...
- ASP.NET 多环境下配置文件web.config的灵活配置---转
注意:本功能在.Net Core中已经不可用,暂时需手动修改web.config中的信息,或者将其设置在appsettings.XXX.json中,然后再使用web.config中的环境变量来制定使用 ...
- .NET Framework 项目多环境下配置文件web.config
解决jenkins自动构建发布的问题,统一从git/svn库中获取项目文件,根据不同配置编译发布到多个运行环境中. 转自:https://www.cnblogs.com/hugogoos/p/6426 ...
- 将web容器置于OSGi框架下进行web应用的开发
将web容器置于OSGi框架下,其实就是将web容器做成OSGi支持的Bundle,再安装到OSGi框架中,这里使用的是Jetty容器. 1.创建一个Eclipse插件项目,在此插件下创建一个WebR ...
- idea环境下SpringBoot Web应用引入JSP
1. 环境 开发环境:idea2019.3 jkd版本:1.8 springboot版本:2.6.2 2. 引入JSP的步骤 2.1 新建工程,引入依赖 这里只是解析jsp,因此只需要引入spring ...
- centos6.5环境下的web项目mysql编码方式导致的中文乱码问题
最近在centos6.5下部署web项目时网页出现中文乱码的问题,在排除掉php之后,把问题锁定在mysql的编码方式上. 解决方法如下: 首先进入mysql命令行,输入命令:SHOW VARIABL ...
- 用Python+Django在Eclipse环境下开发web网站【转】
一.创建一个项目如果这是你第一次使用Django,那么你必须进行一些初始设置.也就是通过自动生成代码来建立一个Django项目--一个Django项目的设置集,包含了数据库配置.Django详细选项设 ...
- 【转载】django在eclipse环境下建web网站
一.创建一个项目如果这是你第一次使用Django,那么你必须进行一些初始设置.也就是通过自动生成代码来建立一个Django项目--一个Django项目的设置集,包含了数据库配置.Django详细选项设 ...
随机推荐
- Linux虚拟机基本操作
---恢复内容开始--- 一.输入法调整 实现步骤:Application ------> System Tools ------>Settings ------>Rejion&a ...
- The Interview Outline
************************* 一.基础部分************************* 1.1 常用数据类型 - 字符串 split/strip/replace/find/ ...
- 《Drools7.0.0.Final规则引擎教程》第4章 4.5RHS语法
RHS语法 使用说明 RHS是满足LHS条件之后进行后续处理部分的统称,该部分包含要执行的操作的列表信息.RHS主要用于处理结果,因此不建议在此部分再进行业务判断.如果必须要业务判断需要考虑规则设计的 ...
- keras系列︱seq2seq系列相关实现与案例(feedback、peek、attention类型)
之前在看<Semi-supervised Sequence Learning>这篇文章的时候对seq2seq半监督的方式做文本分类的方式产生了一定兴趣,于是开始简单研究了seq2seq.先 ...
- online learning,batch learning&批量梯度下降,随机梯度下降
以上几个概念之前没有完全弄清其含义及区别,容易混淆概念,在本文浅析一下: 一.online learning vs batch learning online learning强调的是学习是实时的,流 ...
- IE11降级到IE8
- 移动端 css 禁止长按屏幕选中
*{ -webkit-touch-callout:none; -webkit-user-select:none; -khtml-user-select:none; -moz-user-select:n ...
- Delphi格式化函数Format、FormatDateTime和FormatFloat详解
转自:http://outofmemory.cn/code-snippet/7631/Delphi-format-hua-function-Format-FormatDateTime-FormatFl ...
- 在 GitHub 公开仓库中隐藏自己的私人邮箱地址
GitHub 重点在开方源代码,其本身还是非常注重隐私的.这一点与面向企业的 GitLab 很不一样. 不过,你依然可能在 GitHub 上泄露隐私信息,例如企业内部所用的电子邮箱. GitHub 对 ...
- win8.1下安装双系统ubuntu14.04.3
一.去ubuntu官网下载长期支持版的系统,64位还是32位由物理内存而定,4G以下用32位,4G以上(包括4G)使用64位. 二.若64位的系统,下载下来的文件名应该是ubuntukylin-14. ...