所有工具類
項(xiàng)目中調(diào)用了別的系統(tǒng)的webservice接口,調(diào)用成功之后發(fā)現(xiàn)wsdlLocation的地址是寫死的,不方便修改,所以需要實(shí)現(xiàn)地址,包括用戶名密碼的可配置。項(xiàng)目的框架是Spring,調(diào)用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://m.dlhighland.cn">張軍個(gè)人網(wǎng)站</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();
}
/**
* 初使化調(diào)用
*/
private static void initInvoke() {
if (DYNAMIC_CLIENT_FACTORY == null) {
// DYNAMIC_CLIENT_FACTORY = JaxWsDynamicClientFactory.newInstance();
DYNAMIC_CLIENT_FACTORY = DynamicClientFactory.newInstance();
}
}
/**
* 創(chuàng)建webservice代理并且調(diào)用webservice
*
* @param wsdlAddress
* 調(diào)用webservice地址
* @param method
* 調(diào)用遠(yuǎn)程的方法名
* @param params
* 傳遞的參數(shù)
* @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 時(shí)間設(shè)置防止webservice在訪問時(shí)候再次超時(shí)
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;
}
}
/**
* 創(chuàng)建客戶端地址集合
*
* @param wsdlAddressList
* @return
*/
public static boolean addClientMap(List<String> wsdlAddressList) throws Exception {
logger.debug("創(chuàng)建WebService開始");
for (String wsdlAddress : wsdlAddressList) {
try {
Client client = DYNAMIC_CLIENT_FACTORY.createClient(wsdlAddress);
// 設(shè)置超時(shí)時(shí)間
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 + "創(chuàng)建成功");
} catch (Exception e) {
logger.error(wsdlAddress + "創(chuàng)建失敗:" + e.getMessage());
e.printStackTrace();
}
}
logger.debug("WebService創(chuàng)建成功MAP_CLIENTS集合:" + MAP_CLIENTS);
return true;
}
}
本文為張軍原創(chuàng)文章,轉(zhuǎn)載無需和我聯(lián)系,但請(qǐng)注明來自張軍的軍軍小站,個(gè)人博客http://m.dlhighland.cn
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元

