天天看点

cxf客户端调用webservice接口,服务器端重启后调用失败,改为Restful方式调用

之前用cxf客户端调用soap协议的webservice接口,发现一个问题:

如果服务端需要重启,在重启期间(服务未成功启动之前),cxf客户端调用了服务,返回失败结果(因为服务端未启动),等服务端重启 后,cxf客户端调用服务一直失败,只有cxf客户端重启后方可继续使用。

所以调用方式改为restful方式,post方式调用,接收返回的xml文件,服务端的重启就不会影响到客户端了 。

之前cxf客户端调用方式:

public static String sendfxBackEnd(String methodName,String json, String prokey) {
		String rs = "";
		try {
			// 这个是用cxf 客户端访问cxf部署的webservice服务
			// 千万记住,访问cxf的webservice必须加上namespace ,否则通不过
			// 现在又另外一个问题,传递过去的参数服务端接收不到
			logger.info("发送WS->sendBackEnd请求:" + rs);
			JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory
					.newInstance();
			Client client = dcf
					.createClient(ConfigUtil.getProperty("config.properties", prokey));

			// url为调用webService的wsdl地址
			QName qname = new QName(ConfigUtil.getProperty("config.properties", prokey+"Ns"),
					methodName);

			String strXml = "";
			// paramvalue为参数值
			logger.info("发送WS请求:sendfxBackEnd=" + json);
			Object[] result = client.invoke(qname, json);
			// 调用web Service//输出调用结果
			rs = result[0].toString();
			logger.info("接收WS应答:" + rs);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		
             return rs;		
	}
           

restful调用方式:

package com.market.common.protocol;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

public class RestfulClientUtils {
	
	/**
	 * 参考示例
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {
		String url = "http://127.0.0.1:8800/axis2/services/fx.adapter/crm";
		String param = "{\"op\":\"cashorder\", \"aid\":\"3000566\"}";
		String ret = call(url, param);
		System.out.println("ret: " + ret);
	}
	
	public static String call(String urlPath, String param) throws Exception {
		StringBuffer sb = new StringBuffer();
		// 建立连接
		URL url = new URL(urlPath);
		// 如有中文一定要加上,在接收方用相应字符转码即可
		param = "args0=" + URLEncoder.encode(param, "utf-8");		// 参数
		
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		// 设置参数
		conn.setDoOutput(true); // 需要输出
		conn.setDoInput(true); // 需要输入
		conn.setUseCaches(false); // 不允许缓存
		conn.setConnectTimeout(10000);	// 
		conn.setReadTimeout(10000);
		conn.setRequestMethod("POST"); // 设置POST方式连接
		// 设置请求属性
		conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
		conn.setRequestProperty("Connection", "Keep-Alive");// 维持长连接
		conn.setRequestProperty("Charset", "UTF-8");
		// 连接,也可以不用明文connect,使用下面的httpConn.getOutputStream()会自动connect
		conn.connect();
		// 建立输入流,向指向的URL传入参数
		DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
		dos.writeBytes(param);
		dos.flush();
		dos.close();
		// 获得响应状态
		int resultCode = conn.getResponseCode();
		if (HttpURLConnection.HTTP_OK == resultCode) {
			String readLine = new String();
			BufferedReader responseReader = new BufferedReader(
					new InputStreamReader(conn.getInputStream(), "UTF-8"));
			while ((readLine = responseReader.readLine()) != null) {
				sb.append(readLine).append("\n");
			}
			responseReader.close();
			return parseXmlStr(sb.toString());
		}
		return null;
	}
	
	
	/**
	 *  解析xml字符串,提取返回内容
	 * @param xmlStr
	 * @return
	 */
	public static String parseXmlStr(String xmlStr) {
		String ret = null;
		Document doc = null;
		try {
			doc = DocumentHelper.parseText(xmlStr); //将字符串转为XML
			Element root = doc.getRootElement(); // 获取根节点
			ret = root.elementTextTrim("return"); 
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ret;
	}
}
           

继续阅读