RPC(RPCServiceClient)方式调用webservice SOAP调用

匿名 (未验证) 提交于 2019-12-02 23:03:14
 /**  * 应用rpc的方式调用 这种方式就等于远程调用,  * 即通过url定位告诉远程服务器,告知方法名称,参数等, 调用远程服务,得到结果。  * 使用 org.apache.axis2.rpc.client.RPCServiceClient类调用WebService  *  【注】:    如果被调用的WebService方法有返回值 应使用 invokeBlocking 方法 该方法有三个参数  第一个参数的类型是QName对象,表示要调用的方法名;  第二个参数表示要调用的WebService方法的参数值,参数类型为Object[];  当方法没有参数时,invokeBlocking方法的第二个参数值不能是null,而要使用new Object[]{}。  第三个参数表示WebService方法的 返回值类型的Class对象,参数类型为Class[]。    如果被调用的WebService方法没有返回值 应使用 invokeRobust 方法  该方法只有两个参数,它们的含义与invokeBlocking方法的前两个参数的含义相同。    在创建QName对象时,QName类的构造方法的第一个参数表示WSDL文件的命名空间名,  也就是 <wsdl:definitions>元素的targetNamespace属性值。  *  */ public String webserviceAxis2(String methodName,LinkedHashMap param) {    log.info("调用方法:"+methodName+",输入参数:"+JSON.toJSONString(param));    String result = "";    // 使用RPC方式调用WebService    RPCServiceClient serviceClient = null;    try {       // axis2 服务端地址       String htWsUrl = htDomain+"OAapp/WebObjects/OAapp.woa/ws/WSClientInterface";       serviceClient = new RPCServiceClient();         // 指定调用WebService的URL       EndpointReference targetEPR = new EndpointReference(htWsUrl);         /*MessageContext msgContext = new MessageContext();       msgContext.setServiceContext(serviceClient.getServiceContext());       MessageContext.setCurrentMessageContext(msgContext);*/         Options options = serviceClient.getOptions();         //确定目标服务地址       options.setTo(targetEPR);       //确定调用方法       options.setAction("urn:"+methodName);       options.setProperty(HTTPConstants.CHUNKED, "false");// 把chunk关掉后,会自动加上Content-Length       //解决高并发链接超时问题       options.setManageSession(true);       options.setProperty(HTTPConstants.REUSE_HTTP_CLIENT,true);         //设置响应超时,默认5s       options.setProperty(HTTPConstants.SO_TIMEOUT, TIMEOUT_MILLISECONDS);       //设置连接超时,默认5s       options.setProperty(HTTPConstants.CONNECTION_TIMEOUT, TIMEOUT_MILLISECONDS);         //options.setTimeOutInMilliSeconds(TIMEOUT_MILLISECONDS_AXIS2);       /**        * 指定要调用的getPrice方法及WSDL文件的命名空间        * 如果 webservice 服务端由axis2编写        * 命名空间 不一致导致的问题        * org.apache.axis2.AxisFault: java.lang.RuntimeException: Unexpected subelement arg0        */       QName qname = new QName("http://xml.apache.org/axis/wsdd/", methodName);         // 指定传递参数值       //Object[] parameters = new Object[] { "18911215691","ROOT" };       Object[] parameters = new Object[param.size()];         // 设置参数名称,具体参照从浏览器中看到的       if(param!=null&&!param.isEmpty()){          Set set = param.entrySet();          Iterator it = set.iterator();          int i=0;          while(it.hasNext()){             Entry entry = (Entry)it.next();             String key = entry.getKey().toString();               if(entry.getValue() instanceof byte[]){//把object转换成字节数组                parameters[i]=toByteArray(entry.getValue());             }else{                parameters[i]=entry.getValue();             }               i++;          }       }         // 指定方法返回值的数据类型的Class对象       Class[] returnTypes = new Class[] { String.class };         // 调用方法并输出该方法的返回值       Object[] response = serviceClient.invokeBlocking(qname, parameters, returnTypes);       result = (String) response[0];         log.info("webserviceAxis2 result:"+result);    } catch (org.apache.axis2.AxisFault e) {       log.error("第三方接口异常",e);       if(e.getCause()!=null&&e.getCause().toString().toLowerCase().contains("timeout")){          throw new BusinessException("第三方服务连接超时,请稍候重试!","WB_301");       }       throw new BusinessException("第三方服务异常,请稍候重试!","WB_301");    } finally {       try {          if(serviceClient != null){             serviceClient.cleanupTransport();          }       } catch (org.apache.axis2.AxisFault e) {          log.error("第三方接口异常 finally",e);          if(e.getCause()!=null&&e.getCause().toString().contains("SocketTimeoutException")){             throw new BusinessException("第三方服务连接超时,请稍候重试!","WB_301");          }          throw new BusinessException("第三方服务异常,请稍候重试!","WB_301");       }    }      return result; } 

 public static void main(String[] args) {    try {       //测试       LinkedHashMap map = new LinkedHashMap();       map.put("phone", "189000000");       map.put("parentId", "ROOT");       String result = new 类名.webserviceAxis2("getDMagazineDirList",map);    } catch (Exception e) {       e.printStackTrace();    } } 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!