现在的位置: 首页 > 综合 > 正文

使用CXF-DOSGi将Bundle发布为WebService

2017年12月20日 ⁄ 综合 ⁄ 共 4056字 ⁄ 字号 评论关闭

《使用CXF-DOSGiBundle发布为WebService

 

一、本文摘要

本文讲述了通过Apache CXF DOSGi的部署,将有依赖的Bundle发布为WebService的方法。

cxf-dosgiApache CXF 的分布式 OSGi 框架)将和 Eclipse 的 Equinox OSGi 框架一起用于开发和部署服务包。

 

 

二、环境准备

l 下载CXF的包:cxf-dosgi-ri-singlebundle-distribution-1.3.1.jar

网址:http://cxf.apache.org/dosgi-releases.html

l 下载osgi compendium :org.osgi.compendium-1.2.0.jar

网址:OSGi Compendium 

l 在Eclipse中,把 Perspective 设置为 “Plug-In Development”

l 然后导入上两个包。File/Import/Plug-In Development/Plug-ins and Fragments

l 接下来我们需要把 osgi compendium 包作为所需的包指定到 dosgi 包中。双击 cxf-dosgi-ri-singlebundle-distribution 项目中的 META-INF/MANIFEST.MF 文件。当 Eclipse 在设计模式中打开清单文件时,选择 Dependencies 选项卡,然后添加 org.osgi.compendium 作为 “Required Plug-ins”

 

 

三、开发一个服务Bundle,并且发布为WebService

l 使用一个方法创建一个基于 POJO 的简单 Web 服务。

l Eclipse->File->new->project->Plug-in Project

创建Bundle名为:HelloWprldService

l 新建一个HelloWorldService类,代码如下:

package com.edu.scut;

public interface HelloWorldService {

String sayHello();

}

l 导出这个Bundle以提供服务。

 

l 同样的方法创建一个HelloworldService的实现工程:

l 整个项目的目录结构如下:

 

首先添加对接口的依赖

 

l 创建Activator类:

package helloworldimpl;

 

import java.util.Dictionary;

import java.util.Hashtable;

 

import org.osgi.framework.BundleActivator;

import org.osgi.framework.BundleContext;

import org.osgi.framework.ServiceRegistration;

 

import com.edu.scut.HelloWorldService;

 

public class Activator  implements BundleActivator {

 

@SuppressWarnings("rawtypes")

private ServiceRegistration registration;

private static BundleContext context;

 

static BundleContext getContext() {

return context;

}

@Override

public void start(BundleContext bundleContext) throws Exception {

Activator.context = bundleContext;

//设置服务的属性

    Dictionary<String, String> props = new Hashtable<String, String>();

 

    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","http://localhost:8080/hello_world");  

 

        //注册服务

        registration = Activator.context.registerService(HelloWorldService.class.getName(), new HelloWorldServiceImpl(), props);

}

 

@Override

public void stop(BundleContext bundleContext) throws Exception {

Activator.context = null;

 registration.unregister();

}

 

}

l 创建HelloWorldServiceImpl类:

package helloworldimpl;

 

import java.util.Date;

 

import com.edu.scut.HelloWorldService;

 

public class HelloWorldServiceImpl implements HelloWorldService {

 

@Override

public String sayHello() {

System.out.println("HelloWorld");

return (new Date()).toString();

}

}

l Eclipse-Run->Run configuration,配置结果如下图所示:

 

l 点击run运行OSGi框架。

 

说明了我的web服务已经注册成功了,好了,我们在浏览器中看一下结果,在浏览器中输入:http://localhost:8080/hello_world?wsdl,就可以看到如下结果

 

四、创建客户端调用WebService

l Run->launch the web service explorer->WSDL page->GO

 

l 服务器端打印出了:

 

l 编写普通的Java类,添加Axis2的包。

l 在Client工程下创建SayHello类:

package com.edu.scut.client;

 

import javax.xml.namespace.QName;  

import org.apache.axis2.AxisFault;  

import org.apache.axis2.addressing.EndpointReference;  

import org.apache.axis2.client.Options;  

import org.apache.axis2.rpc.client.RPCServiceClient; 

 

public class SayHello {

 

public static void main(String[] args) throws AxisFault {  

        // TODO Auto-generated method stub  

  

        // 使用RPC方式调用WebService  

        RPCServiceClient serviceClient = new RPCServiceClient();  

        Options options = serviceClient.getOptions();  

        

        // 指定调用WebServiceURL  

        EndpointReference targetEPR = new EndpointReference(  

                "http://localhost:8080/hello_world");  

        options.setTo(targetEPR);  

  

        // 指定要调用的计算机器中的方法及WSDL文件的命名空间:helloworldimpl 

        QName opAddEntry = new QName("http://helloworldimpl","sayHello");//加法  

      

        // 指定sayhello方法的参数值为两个,分别是加数和被加数  

        Object[] opAddEntryArgs = new Object[] { };  

        

        // 指定sayhello方法返回值的数据类型的Class对象  

        Class<?>[] classes = new Class[] { String.class };  

        

        // 调用sayhello方法并输出该方法的返回值  

      System.out.println(serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs, classes)[0]);  

        

    }  

}

l 运行后正确返回信息,并在服务器打印HelloWorld

 

 

 

 

五、总结

本文描述了以 OSGi 包的形式开发和部署 WebService的详细方法。

 

 

抱歉!评论已关闭.