java 调用webservice(xml格式交换)工具类

今天在公司开发一个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;
	} 
 
}


神奇的事情是,我使用第二种方式调用,内存溢出的情况就有效避免了!

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值