今天在公司开发一个webservice相关的项目使用RPC调用时候,由于报文返回过大,出现了内存溢出的情况。这里可以使用axis访问webservice,这里是我封装的工具类。
package com.handkoo.utils;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
/**
* 调用webservice(xml)工具类
*
* @author cp
*
*/
public class MyWebServiceUtils {
/**
* 封装的访问webservice的工具类:(有局限性,数据量小的xml报文可用,较大则会出现stackoverflow的异常)
*
* @param reqXML
* 请求的xml
* @param endPoint
* webservice的路径除去?wsdl:例如:http://1.202.235.66:7002/webService/PartsService(没有?wsdl)
* @param namespace
* 名称空间
* @param method
* 方法名称
* @return 返回报文结果(xml)
*/
public static String loadWebService(String reqXML, String endPoint, String namespace, String method) {
String responseXml = "";
RPCServiceClient serviceClient = null;
try {
/* 调用理赔系统的接口 */
String nameSpace = namespace;
QName qname = new QName(nameSpace, method);
serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
EndpointReference targetEPR = new EndpointReference(endPoint);
options.setTo(targetEPR);
options.setTimeOutInMilliSeconds(30000);
Object[] rtnObjs = serviceClient.invokeBlocking(qname, new Object[] { reqXML },
new Class[] { String.class });
responseXml = rtnObjs[0].toString();
} catch (Exception e) {
e.toString();
}
return responseXml;
}
/**
* axis方式访问webservice
*
* @param reqXML
* 请求的xml的报文
* @param endPoint
* webservice的wsdl地址,例如:http://1.202.235.66:7002/webService/PartsService?wsdl
* @param namespace
* 调用方法的命名空间
* @param method
* 调用的方法名
* @return 返回调用的报文(xml)
*
*/
public static String invokRemoveWSDLByAxis(String reqXML, String endPoint, String namespace, String method) {
String responseXML = "";
Service service = new Service();
try {
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(endPoint);
call.setOperationName(new javax.xml.namespace.QName(namespace, method));
call.addParameter("arg0", XMLType.XSD_STRING, ParameterMode.IN);
call.setReturnType(XMLType.XSD_STRING);
responseXML = (String) call.invoke(new Object[] { reqXML });
} catch (Exception e) {
e.printStackTrace();
}
return responseXML;
}
}
神奇的事情是,我使用第二种方式调用,内存溢出的情况就有效避免了!