所有工具類
項目中調用了別的系統的webservice接口,調用成功之后發現wsdlLocation的地址是寫死的,不方便修改,所以需要實現地址,包括用戶名密碼的可配置。項目的框架是Spring,調用webservice使用的是CXF。
package zj.cxf.util;
import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.endpoint.dynamic.DynamicClientFactory;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
import org.apache.log4j.Logger;
/**
*
* webService工具類
*
* @version 1.00 (2014.09.15)
* @author SHNKCS 張軍 {@link <a target=_blank href="http://www.dlhighland.cn">張軍個人網站</a> <a target=_blank href="http://user.qzone.qq.com/360901061/">張軍QQ空間</a>}
*
*/
public class CxfUtil implements Serializable {
private static final long serialVersionUID = 1L;
private static Map<String, Client> MAP_CLIENTS = Collections.synchronizedMap(new HashMap<String, Client>());
private static DynamicClientFactory DYNAMIC_CLIENT_FACTORY = null;
private static final Logger logger = Logger.getLogger(CxfUtil.class);
static {
initInvoke();
}
/**
* 初使化調用
*/
private static void initInvoke() {
if (DYNAMIC_CLIENT_FACTORY == null) {
// DYNAMIC_CLIENT_FACTORY = JaxWsDynamicClientFactory.newInstance();
DYNAMIC_CLIENT_FACTORY = DynamicClientFactory.newInstance();
}
}
/**
* 創建webservice代理并且調用webservice
*
* @param wsdlAddress
* 調用webservice地址
* @param method
* 調用遠程的方法名
* @param params
* 傳遞的參數
* @return
*/
public static Object[] invoke(String wsdlAddress, String method, Object[] params) throws Exception {
Client client = (Client) MAP_CLIENTS.get(wsdlAddress);
if (client == null) {
client = DYNAMIC_CLIENT_FACTORY.createClient(wsdlAddress);
// add 時間設置防止webservice在訪問時候再次超時
HTTPConduit conduit = (HTTPConduit) client.getConduit();
HTTPClientPolicy policy = new HTTPClientPolicy();
policy.setConnectionTimeout(1800000);
policy.setReceiveTimeout(1800000);
conduit.setClient(policy);
MAP_CLIENTS.put(wsdlAddress, client);
}
Object[] results = client.invoke(method, params);
return results;
}
/**
*
* @param wsdlAddress
* 配置文件路徑
* @param method
* @param arg0
* @return
*/
public static String invoke(String wsdlAddress, String method, String params) throws Exception {
Object[] results = invoke(wsdlAddress, method, new Object[] { params });
if (results != null && results.length > 0 && results[0] != null) {
return String.valueOf(results[0]);
} else {
return null;
}
}
/**
* 創建客戶端地址集合
*
* @param wsdlAddressList
* @return
*/
public static boolean addClientMap(List<String> wsdlAddressList) throws Exception {
logger.debug("創建WebService開始");
for (String wsdlAddress : wsdlAddressList) {
try {
Client client = DYNAMIC_CLIENT_FACTORY.createClient(wsdlAddress);
// 設置超時時間
HTTPConduit conduit = (HTTPConduit) client.getConduit();
HTTPClientPolicy policy = new HTTPClientPolicy();
policy.setConnectionTimeout(1800000);
policy.setReceiveTimeout(1800000);
conduit.setClient(policy);
MAP_CLIENTS.put(wsdlAddress, client);
logger.debug(wsdlAddress + "創建成功");
} catch (Exception e) {
logger.error(wsdlAddress + "創建失敗:" + e.getMessage());
e.printStackTrace();
}
}
logger.debug("WebService創建成功MAP_CLIENTS集合:" + MAP_CLIENTS);
return true;
}
}
本文為張軍原創文章,轉載無需和我聯系,但請注明來自張軍的軍軍小站,個人博客http://www.dlhighland.cn
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

