一、使用cxf的JaxWsServerFactoryBean创建webservice的服务端和客户端

本文介绍了一个基于HBase的服务端与客户端交互实例,包括服务端的搭建、接口定义及实现,以及如何通过WSDL生成客户端调用代码并进行远程调用。

A。创建服务端

配置文件:


import com.enn.cn.util.PropertiesUtil;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;


/**
 * Created by Administrator on 2017/10/30.
 */
public class ExecServer {
    public static void main(String[] args) {
        try {
            PropertiesUtil util=new PropertiesUtil("/config.properties");
            JaxWsServerFactoryBean bean = new JaxWsServerFactoryBean();
            bean.setAddress(util.getProperty("deployAddress"));
            bean.setServiceClass(GetHbaseDate.class);
            bean.setServiceBean(new GetHbaseDateImpl());
            bean.create();
            System.err.println("服务启动成功");
//            gethbaseall("2011",1476054000000L,1509325844000L);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
接口类:
import javax.jws.WebService;

/**
 * Created by Administrator on 2017/10/30.
 */
@WebService
public interface GetHbaseDate {
     String GetPipeposition(String userid,long startStr, long endStr);
}

实现类:

import com.enn.cn.trail.MongPosition;
import com.enn.cn.util.CommonContent;
import com.enn.cn.util.HBaseUtil;
import com.enn.cn.util.KerberosUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by Administrator on 2017/10/30.
 */
public class GetHbaseDateImpl implements GetHbaseDate {

    @Override
    public String GetPipeposition(String userid, long startStr, long endStr) {
          
System.out.println(
userid
);
}

客户端生成,生产完之后删除.class文件,拷贝.java文件到指定目录:

——你看不懂 ,JDK看得懂,wsimportJDK自带的,可以根据WSDL文档生成客户端调用代码的工具。无论服务器端WebService使用什么语言编写的,豆浆在客户端生成Java代码。所以服务器用什么语言编写的并不重要。

wsimport.exe(jdk/bin文件夹中)命令参数熟知:

    -d:生成class文件。默认参数。

    -s:生成Java文件

   -p:自定义包结构

 

生成文件到指定盘:

WSDL文件内容:
 
<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://interfaces.cn.enn.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="GetHbaseDateService" targetNamespace="http://interfaces.cn.enn.com/">
<wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://interfaces.cn.enn.com/" elementFormDefault="unqualified" targetNamespace="http://interfaces.cn.enn.com/" version="1.0">
<xs:element name="GetPipeposition" type="tns:GetPipeposition"/>
<xs:element name="GetPipepositionResponse" type="tns:GetPipepositionResponse"/>
<xs:complexType name="GetPipeposition">
<xs:sequence>
<xs:element minOccurs="0" name="arg0" type="xs:string"/>
<xs:element name="arg1" type="xs:long"/>
<xs:element name="arg2" type="xs:long"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="GetPipepositionResponse">
<xs:sequence>
<xs:element minOccurs="0" name="return" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
<wsdl:message name="GetPipeposition">
<wsdl:part element="tns:GetPipeposition" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:message name="GetPipepositionResponse">
<wsdl:part element="tns:GetPipepositionResponse" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:portType name="GetHbaseDate">
<wsdl:operation name="GetPipeposition">
<wsdl:input message="tns:GetPipeposition" name="GetPipeposition"></wsdl:input>
<wsdl:output message="tns:GetPipepositionResponse" name="GetPipepositionResponse"></wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="GetHbaseDateServiceSoapBinding" type="tns:GetHbaseDate">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="GetPipeposition">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="GetPipeposition">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="GetPipepositionResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="GetHbaseDateService">
<wsdl:port binding="tns:GetHbaseDateServiceSoapBinding" name="GetHbaseDatePort">
<soap:address location="http://10.11.85.137:8080/getpipepositions"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
客户端调用服务端:
public class Client {
    public static void main(String[] args) {
           /* URL wsdlUrl = new URL("http://localhost:8080/getpipepositions?wsdl");
            Service s = Service.create(wsdlUrl, new QName("http://interfaces.cn.enn.com/","GetHbaseDateService"));
            GetHbaseDate hs = s.getPort(new QName("http://interfaces.cn.enn.com/","GetHbaseDatePort"), GetHbaseDate.class);
            String res=hs.GetPipeposition("2011",1476054000000L,1509325844000L);
            System.out.println(res);*/
            GetHbaseDateService sd=new GetHbaseDateService();
            com.enn.cn.interfaces.client.GetHbaseDate fs=sd.getGetHbaseDatePort();
            String res=fs.getPipeposition("2011",1476054000000L,1509325844000L);
        System.out.println(res);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值