前言
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的一个入门介绍,其中大部分内容也是根据其官网的示例一步步操作完成的,对于复杂的服务(带参数以及复杂类型返回值),请查看其官网相关资料深入研究。
相关推荐
cxf-dosgi-ri-singlebundle-distribution-1.4.0.jar
CXF DOSGi允许轻松发布和使用SOAP和REST服务,而无需使用CXF蓝图扩展或使用Java代码发布服务。 因此,这是在OSGi中将CXF与声明式服务一起使用的推荐方法。 查看示例以了解使用的简单程度。 模组 。 意向 意图允许...
Karaf Cellar Kubernetes 示例这是一个在开发环境中使用 Kubernetes 集群的 Karaf Cellar DOSGi 应用程序的简单示例。 如果您正在寻找不使用 Kubernetes 的 DOSGi 示例,请更改此注意:这仅适用于提供的 Karaf ...
cxf-dosgi-ri-multibundle-distribution-1.5.0-dir
shrinkwrap-container-openejb-31.zip,openejb 3.1.x项目的包覆面提取集成扩展包覆面提取openejb 3.1.x集成扩展包
1、 在eclipse.ini中添加VM启动参数,制定eclipse启动使用的vm,修改后的文件内容如下 -showsplash org.eclipse.platform --launcher.XXMaxPermSize 256M -vm C:\Program Files\Java\jdk1.5.0_12\bin\javaw....
在注册机中输入你的英文名,点Generate Subscription Code,然后再点Generate Activation Code,这样注册码和激活码就都生成了 在MyEclipse中注册用户名和注册码,然后激活,选择用激活码激活,把注册机中的激活码贴...
第五步:打开菜单Tools->ReplaceJarFile,弹出文件选择对话框,到myeclipse的安装目录common文件夹下选择plugins文件夹点击打开,程序正在替换,一会后才会输出信息! 第六步:点菜单Tools->SaveProperites 恭喜你...
5、在刚才下载的附件中,执行myeclipse9_keygen_activator.exe(注册机),在注册机输入英文名,点Generate Subscription Code, 再点Generate Activation Code,这样注册码和激活码都生成。赶紧打开MyEclipse填写信息...
-Dosgi.dev=true 3、启动MyEclipse,执行myeclipse9_keygen_activator.exe(注册机) 在注册机中输入你的英文名,点Generate Subscription Code,然后再点Generate Activation Code,这样注册码和激活码就都生成了 ...
-Dosgi.dev=true 3、启动MyEclipse,执行myeclipse9_keygen_activator.exe(注册机) 在注册机中输入你的英文名,点Generate Subscription Code,然后再点Generate Activation Code,这样注册码和激活码就都生成了 ...
-Dosgi.dev=true 3、启动MyEclipse,执行myeclipse9_keygen_activator.exe(注册机),运行前确认你装了Java6 在注册机中输入你的英文名,点Generate Subscription Code,然后再点Generate Activation Code,这样...
-Dosgi.dev=true 3、启动MyEclipse,执行myeclipse9_keygen_activator.exe(注册机),运行前确认你装了Java6 在注册机中输入你的英文名,点Generate Subscription Code,然后再点Generate Activation Code,这样...
在...\MyEclipse\MyEclipse 10\myeclipse.ini中最后添加 -Dgenuitec.honorDevMode=true -Dosgi.dev=true 把com.genuitec.eclipse.export.wizard_9.0.0.me201211011550.jar 复制替换到 myeclipse文件夹 common\...
适用于Android应用程序的JIT污染分析-在ISSTA 2017上发布 作者:Lisa Nguyen Quang Do 该软件已获得EPL许可,有关更多信息,请访问: : 要运行分析: 猎豹是包含分析的插件项目。 使用以下配置运行它:-Dosgi....
您可以在以下情况下进行转换: 1C存储中最新版本的配置包含“托管应用程序”启动模式您计划切换到1C:EDT以将启动模式切换到“托管应用程序”。所需组件可以使用1C运行该配置:企业开发工具1.8.3( ) 平台1C:企业...
在文件最后2行添加: -Dgenuitec.honorDevMode=true -Dosgi.dev=true c) 生成序列号和激活码 打开注册机 输入用户名(英文),如“LiHeLin” 点击生成序列号和激活码 d) 注册和激活 打开myeclipse ...
Eclipse的安装配置 基础篇 附有图解和说明 没有涉及有关eclipse.ini的内容(如下) -startup plugins/org.eclipse.equinox.launcher_1.1.0.v20100507.jar ...-Dosgi.requiredJavaVersion=1.5 -Xms40m -Xmx512m
3.在...\MyEclipse\MyEclipse 10\myeclipse.ini中添加 -Dgenuitec.honorDevMode=true -Dosgi.dev=true ************************************MyEclipse 10.1破解步骤***************************************** 至此...
面料课程示例 在这些文件夹中,我收集了 JBoss Fuse 6.1 上可用的示例配置文件的经验 骆驼 香草 DOSGI 快速入门 发展HelloWorld 开发示例