CXF 服务调用之java客户端代码
来源:互联网 发布:php array key 编辑:程序博客网 时间:2024/05/21 00:18
package com.dawning.gridview.core.authmanagement.webapp.gvusermanagement.export.test;import java.util.List;//import javax.xml.bind.JAXBElement;import org.apache.cxf.endpoint.Client;import org.apache.cxf.frontend.ClientProxy;import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;import org.apache.cxf.transport.http.HTTPConduit;import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;import com.dawning.gridview.app.jobscheduler.commonresource.dataandmethod.export.data.ResultObjectWS;import com.dawning.gridview.app.jobscheduler.intf.jobservice.export.JobServiceIPortType;import com.dawning.gridview.core.authmanagement.webapp.gvusermanagement.export.po.ArrayOfGvUserInfo;import com.dawning.gridview.core.authmanagement.webapp.gvusermanagement.export.po.GvUserInfo;/** *获取服务的实例工厂 获取的服务是单列的 java 客户端调用云端服务的方式,首先要通过wsdl2java 命令将相关文件准备好,然后本地还需要相关cxf的jar包, * @author lvzh * */public class ServiceFactory {/** * 客户端调用的实例对象 通过wsdl生成的接口对象 */private static JobServiceIPortType service = null;private static String url;/** * 提供给外部使用的获取服务获取实例的方法 * @return */public synchronized static JobServiceIPortType getServiceInstance(){if(service == null){JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); //注册WebService接口 factory.setServiceClass(JobServiceIPortType.class); //设置WebService地址 //factory.setAddress("http://10.0.31.210:10005/jm_jobservice"); factory.setAddress(url);/*如果server端设置了验证,还需要 factory.setUserName(""); factory.setPasswd("");*/Map<String, Object> properties = new HashMap<String, Object>();properties.put("mtom-enabled", Boolean.TRUE);factory.setProperties(properties); service = (JobServiceIPortType)factory.create(); Client client =ClientProxy.getClient(service); HTTPConduit http = (HTTPConduit) client.getConduit(); HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy(); /*设置服务器和客户端的超时时间*/ httpClientPolicy.setConnectionTimeout(1000*60*7); httpClientPolicy.setReceiveTimeout(1000*60*7); httpClientPolicy.setAllowChunking(false); http.setClient(httpClientPolicy);}return service;}public static void setUrl(String url) {ServiceFactory.url = url;}public static void main(String[] args){/*服务地址*/ServiceFactory.setUrl("http://10.0.31.210:10005/jm_jobservice");JobServiceIPortType jspt = ServiceFactory.getServiceInstance();//一个登录的测试方法 String uKey = jspt.userLogin("admin", "admin");System.out.println(uKey);//查询用户列表 ResultObjectWS rows = jspt.queryUserList("ProjectManager",uKey); JAXBElement<ArrayOfGvUserInfo> gvinfo = rows.getGvUserInfoList(); List<GvUserInfo> gvList = gvinfo.getValue().getGvUserInfo(); System.out.println("userId\tusername\tdeptName\tuserType"); for(GvUserInfo info:gvList){ System.out.println(info.getGvUserId().getValue()+"\t"+info.getGvUserName().getValue()+ "\t"+info.getGvUserDeptName().getValue()+"\t"+info.getGvUserGroupNames().getValue()); }}}