课题Apache CXF 分布式OSGi部署HelloWorld.docx
- 文档编号:26523591
- 上传时间:2023-06-20
- 格式:DOCX
- 页数:17
- 大小:723.45KB
课题Apache CXF 分布式OSGi部署HelloWorld.docx
《课题Apache CXF 分布式OSGi部署HelloWorld.docx》由会员分享,可在线阅读,更多相关《课题Apache CXF 分布式OSGi部署HelloWorld.docx(17页珍藏版)》请在冰豆网上搜索。
课题ApacheCXF分布式OSGi部署HelloWorld
ApacheCXF分布式OSGi部署HelloWorld
摘要
要配置ApacheCXFOSGi的部署其实比较简单,但是我们一般都会在网上找资料,会遇到怎么也发现不了服务的情况,让人都很郁闷。
有了这次的经历,我要看官方的文档,以防上当。
一、环境准备
首先下载ApacheCXF的包,下载地址:
:
//cxf.apache.org/dosgi-releases.html
我们下载下面这两个就可以了,我们这次会用到cxf-dosgi-ri-singlebundle-distribution
Multi-bundledistribution(zip)
cxf-dosgi-ri-multibundle-distribution-1.3.1-dir.zip
Single-bundledistribution
cxf-dosgi-ri-singlebundle-distribution-1.3.1.jar
目前需要3个bundle
注意版本号
导入cxf-dosgi-ri-singlebundle-distribution-1.3.1.jar包,在Eclipse中菜单:
File->import,选择如下图所示
选择文件所在的路径
我们会在PackageExplorer视图中看到
好了,我们的开发环境就配置好了。
二、创建演示程序-生产服务
创建HelloWorld演示程序
Eclipse->File->new->project-
我们就写一个helloworld的接口
代码如下:
publicinterfaceHelloWorldService{
StringsayHello();
}
接下来我们用同样的方法来,做一个HelloworldService的实现工程
Eclipse->File->new->project
我们来写一个实现类如下,首先添加对接口的依赖
实现类如下:
publicclassHelloWorldServiceImplimplementsHelloWorldService{
@Override
publicStringsayHello(){
System.out.println("HelloWorld");
return(newDate()).toString();
}
}
现在实现类有了,我们现在要注册成web服务,打开实现工程中的Activator类,注册服务代码如下:
publicclassActivatorimplementsBundleActivator{
@SuppressWarnings("rawtypes")
privateServiceRegistrationregistration;
privatestaticBundleContextcontext;
staticBundleContextgetContext(){
returncontext;
}
@Override
publicvoidstart(BundleContextbundleContext)throwsException{
Activator.context=bundleContext;
//设置服务的属性
Dictionary
props.put("service.exported.interfaces","*");
props.put("service.exported.intents","SOAP");
props.put("service.exported.configs","org.apache.cxf.ws");
props.put("org.apache.cxf.ws.address",":
//localhost:
9000/hello_world");
//注册服务
registration=Activator.context.registerService(HelloWorldService.class.getName(),newHelloWorldServiceImpl(),props);
}
@Override
publicvoidstop(BundleContextbundleContext)throwsException{
Activator.context=null;
registration.unregister();
}
}
好了,我们的服务已经注册成功了,我们看看在浏览器中是什么样子的,
Eclipse-Run->Runconfiguration,配置结果如下图所示:
点击run后我们可以看到如下日志:
这就说明了我的web服务已经注册成功了,好了,我们在浏览器中看一下结果,在浏览器中输入:
:
//localhost:
9000/hello_world?
wsdl,就可以看到如下结果
接下来我们会消费服务
三、消费服务
我们建一个插件工程,命名为:
HelloWorldClient,创建好以后如下:
我们要使用服务就要先引用依赖组件,如下图所示
我们还要配置一下服务的发现,在工程下建立OSGI-INF文件夹,里面新建一个remote-service文件夹,在remote-service新建一个remote-services.xml,该文件内容如下:
xmlversion="1.0"encoding="UTF-8"?
>
//.osgi.org/xmlns/sd/v1.0.0"> --远程服务描述--> --定义远程服务映射到本地的服务的接口--> --远程服务的接口--> --远程服务的形式--> --服务的类型--> --服务的地址--> //localhost: 9000/hello_world 和我们在实现类里面注册服务的代码一样 我们来看一下HelloWorldClient的Activator类的内容,主要的内容就是获取服务,然后调用服务,很简单。 publicclassActivatorimplementsBundleActivator{ @SuppressWarnings("rawtypes") privateServiceTrackertracker; privatestaticBundleContextcontext; staticBundleContextgetContext(){ returncontext; } /* *(non-Javadoc) *@seeorg.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext) */ @SuppressWarnings({"unchecked","rawtypes"}) publicvoidstart(BundleContextbundleContext)throwsException{ Activator.context=bundleContext; //创建ServiceTracker,捕获HelloWorldService添加到OSGi框架的事件 tracker=newServiceTracker(Activator.context,HelloWorldService.class.getName(),null){ @Override publicObjectaddingService(ServiceReferencereference){ Objectresult=super.addingService(reference); //获取服务 HelloWorldServicehelloWorldService=(HelloWorldService)Activator.context.getService(reference); //使用服务 System.out.println("callsayhelloat"+helloWorldService.sayHello()); returnresult; } }; tracker.open(); } /* *(non-Javadoc) *@seeorg.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext) */ publicvoidstop(BundleContextbundleContext)throwsException{ Activator.context=null; System.out.println("helloworldclientclose"); tracker.close(); } } 好了,我们的消费者就创建好了,来看一下运行效果吧 先进行运行配置,如下图所示 我们在上图中可以看到结果 好了,我们的演示已经结束了,看起来还是比较简单的,但是在做的过程中还是遇到一些问题,比如使用了老的注册服务代码如下: props.put("osgi.remote.interfaces","*"); props.put("osgi.remote.configuration.type","pojo"); props.put("osgi.remote.configuration.pojo.address",": //localhost: 9000/hello_world"); 使用这个代码是注册服务不成功的,请大家注意!
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 课题Apache CXF 分布式OSGi部署HelloWorld 课题 Apache 分布式 OSGi 部署 HelloWorld