当前位置:   article > 正文

WebService 工具类_webservice工具有哪些

webservice工具有哪些
经常使用WebService来调接口的工具类:
  1. </pre><pre name="code" class="java">package com.thunisoft.hlwzbdb.mgr.business.ajgl.record;
  2. import java.net.MalformedURLException;
  3. import java.net.URL;
  4. import java.util.Arrays;
  5. import java.util.HashMap;
  6. import java.util.Map;
  7. import org.apache.commons.lang.StringUtils;
  8. import org.apache.commons.logging.Log;
  9. import org.apache.commons.logging.LogFactory;
  10. import org.codehaus.xfire.client.Client;
  11. import org.dom4j.Document;
  12. import org.dom4j.DocumentException;
  13. import org.dom4j.DocumentHelper;
  14. import org.dom4j.Element;
  15. /**
  16. * webservice 工具类
  17. *
  18. * 该工具类使用了Xfire单例的方式,问了解决cas中webservice访问慢的问题;
  19. *
  20. * 目前Xfire单例方式使用可能会有多线程并发问题,但是考虑实际业务场景,用户并发的可能性比较小,所以暂时提取公用工具类。
  21. *
  22. * 后续计划加入webservice客户端实例池解决并发问题
  23. *
  24. *
  25. */
  26. public class WSUtil {
  27. /**
  28. * 请求成功
  29. */
  30. final public static String WSCALL_STATUS_SUCCESS = "1";
  31. /**
  32. * 无权限
  33. */
  34. final public static String WSCALL_STATUS_NORIGHT = "2";
  35. /**
  36. * 参数错误
  37. */
  38. final public static String WSCALL_STATUS_BADPARAMS = "3";
  39. /**
  40. * 异常
  41. */
  42. final public static String WSCALL_STATUS_EXCEPTION = "4";
  43. /**
  44. * 日志
  45. */
  46. private static final Log logger = LogFactory.getLog(WSUtil.class);
  47. /**
  48. * 生成代理客户端
  49. *
  50. *
  51. */
  52. private final static class ProxyClient extends Client {
  53. /**
  54. * 构造器
  55. *
  56. * @param url
  57. * @throws Exception
  58. */
  59. private ProxyClient(URL url) throws Exception {
  60. super(url);
  61. }
  62. @Override
  63. public void close() {
  64. logger.warn("单例模式的webservice客户端,不需要关闭!");
  65. }
  66. /**
  67. * 真实关闭
  68. */
  69. public void reallyClose() {
  70. super.close();
  71. }
  72. }
  73. /**
  74. * webservice 客户端连接池
  75. *
  76. * @author yangkai
  77. *
  78. */
  79. private static class ClientPool {
  80. /**
  81. * 连接池缓存
  82. */
  83. private static Map<String, ClientPool> pools = new HashMap<String, ClientPool>();
  84. /**
  85. * 池对象
  86. */
  87. private Client[] pool;
  88. /**
  89. * 连接URL
  90. */
  91. private String url;
  92. /**
  93. * 当前连接标记
  94. */
  95. private int current;
  96. /**
  97. * 构造器,初始化连接池
  98. *
  99. * @param url
  100. * webservice地址
  101. * @param size
  102. * 连接池大小
  103. */
  104. public ClientPool(String url, int size) {
  105. this.url = url;
  106. pool = new Client[size];
  107. current = 0;
  108. pools.put(url, this);
  109. }
  110. /**
  111. * 从连接池中获取一个连接
  112. *
  113. * @return
  114. */
  115. public synchronized Client get() {
  116. Client client = null;
  117. synchronized (this) {
  118. if (current >= 0) {
  119. client = pool[current--];
  120. }
  121. }
  122. System.out.println("池大小:" + current);
  123. if (client == null) {
  124. client = initWSClient(url);
  125. }
  126. return client;
  127. }
  128. /**
  129. * 将连接放回连接池
  130. *
  131. * @param client
  132. */
  133. public void reuse(Client client) {
  134. if (client == null) {
  135. return;
  136. }
  137. synchronized (this) {
  138. if (current < pool.length - 1) {
  139. pool[++current] = client;
  140. } else {
  141. destoryWSClient(client);
  142. }
  143. }
  144. }
  145. /**
  146. * 销毁某个连接
  147. *
  148. * @param client
  149. */
  150. public void destory(Client client) {
  151. if (client != null) {
  152. destoryWSClient(client);
  153. }
  154. }
  155. /**
  156. * 获取连接池实例
  157. *
  158. * @param url
  159. * @return
  160. */
  161. public static ClientPool getInstance(String url) {
  162. if (StringUtils.isEmpty(url)) {
  163. return null;
  164. }
  165. ClientPool pool = pools.get(url);
  166. if (pool == null) {
  167. synchronized (pools) {
  168. pool = pools.get(url);
  169. if (pool == null) {
  170. pool = new ClientPool(url, 5);
  171. pools.put(url, pool);
  172. } else {
  173. pool = pools.get(url);
  174. }
  175. }
  176. }
  177. return pool;
  178. }
  179. }
  180. private WSUtil() {
  181. // 单例模式工具类,防止外部实例化
  182. }
  183. /**
  184. * 初始化webservice客户端
  185. *
  186. * @param url
  187. * wsdl地址
  188. */
  189. private static Client initWSClient(String url) {
  190. if (StringUtils.isBlank(url)) {
  191. return null;
  192. }
  193. ProxyClient client = null;
  194. try {
  195. client = new ProxyClient(new URL(url)); // 根据WSDL创建客户实例
  196. } catch (MalformedURLException e) {
  197. logger.error("生成webservice客户端出错,URL:" + url, e);
  198. } catch (Exception e) {
  199. logger.error("生成webservice客户端出错,URL:" + url, e);
  200. } finally {
  201. if (client != null) {
  202. try {
  203. client.reallyClose();
  204. } catch (Exception e) {
  205. logger.error("释放webservice客户端出错,URL:" + url, e);
  206. }
  207. }
  208. }
  209. return client;
  210. }
  211. /**
  212. * 释放webservice客户端
  213. *
  214. * @param url
  215. */
  216. private static void destoryWSClient(Client client) {
  217. if (client != null) {
  218. try {
  219. ((ProxyClient) client).reallyClose();
  220. } catch (Exception e) {
  221. logger.error("释放webservice客户端失败,URL:" + client.getUrl());
  222. }
  223. }
  224. }
  225. /**
  226. * 用指定客户端执行远程调用
  227. *
  228. * @param Client
  229. * Client
  230. * @param invokeFunc
  231. * 调用方法
  232. * @param objArray
  233. * 参数
  234. * @return
  235. * @throws Exception
  236. */
  237. private static String executeRemoteCall4String(Client client,
  238. String invokeFunc, Object[] objArray) throws Exception {
  239. if (client == null) {
  240. return null;
  241. }
  242. Object[] results = client.invoke(invokeFunc, objArray);
  243. if (results != null && results.length > 0) {
  244. String resultStr = (String) results[0];
  245. if (logger.isDebugEnabled()) {
  246. logger.debug("执行webservice返回结果:" + resultStr);
  247. }
  248. return resultStr;
  249. }
  250. return null;
  251. }
  252. /**
  253. * 执行远程调用,单例模式调用
  254. *
  255. * @param url
  256. * wsdl URL
  257. * @param invokeFunc
  258. * 调用方法
  259. * @param objArray
  260. * 参数
  261. * @return
  262. * @throws Exception
  263. */
  264. public static String remoteCall(String url, String invokeFunc,
  265. Object... param) {
  266. Client client = null;
  267. ClientPool pool = ClientPool.getInstance(url);
  268. logger.info("执行webservice使用参数:" + Arrays.toString(param));
  269. logger.info("执行webservice调用方法:" + invokeFunc);
  270. try {
  271. client = pool.get();
  272. return executeRemoteCall4String(client, invokeFunc, param);
  273. } catch (Exception e) {
  274. logger.error("执行webservice调用失败,尝试重新初始化客户端后调用", e);
  275. pool.destory(client);
  276. client = pool.get();
  277. try {
  278. return executeRemoteCall4String(client, invokeFunc, param);
  279. } catch (Exception e1) {
  280. logger.error("error ro invoke function: " + invokeFunc
  281. + ", where remote call url=" + url + " and params:"
  282. + Arrays.toString(param));
  283. }
  284. } finally {
  285. pool.reuse(client);
  286. }
  287. return null;
  288. }
  289. public static synchronized String remoteInvoke(String url,
  290. String invokeFunc, Object... param) {
  291. Client client = null;
  292. try {
  293. client = new Client(new URL(url));
  294. logger.info("执行webservice使用参数:" + Arrays.toString(param));
  295. logger.info("执行webservice调用方法:" + invokeFunc);
  296. return executeRemoteCall4String(client, invokeFunc, param);
  297. } catch (MalformedURLException e) {
  298. logger.error("error invoke function " + invokeFunc + ", params :"
  299. + Arrays.toString(param));
  300. } catch (Exception e) {
  301. logger.error("error invoke function " + invokeFunc + ", params :"
  302. + Arrays.toString(param));
  303. } finally {
  304. //client.close();
  305. }
  306. return null;
  307. }
  308. /**
  309. * 调用webservice,并封装返回结果
  310. *
  311. * @param url
  312. * wsdl
  313. * @param invokeFunc
  314. * 执行的方法
  315. * @param objArray
  316. * 参数数组
  317. * @return
  318. */
  319. public static Document remoteCallForXmlDocument(String url,
  320. String invokeFunc, Object... objArray) {
  321. String result = remoteCall(url, invokeFunc, objArray);
  322. if (result != null) {
  323. try {
  324. return DocumentHelper.parseText(result);
  325. } catch (DocumentException e) {
  326. logger.error("webservice调用返回结果不是XML格式,XML解析出错", e);
  327. }
  328. }
  329. return null;
  330. }
  331. /**
  332. * 判断远程调用是否成功
  333. *
  334. * @param result
  335. * @return
  336. */
  337. public static boolean isRemoteCallSuccess(String result) {
  338. try {
  339. return isRemoteCallSuccess(DocumentHelper.parseText(result));
  340. } catch (DocumentException e) {
  341. logger.error("webservice调用返回结果不是XML格式,XML解析出错", e);
  342. }
  343. return false;
  344. }
  345. /**
  346. * 判断远程调用是否成功
  347. *
  348. * @param result
  349. * @return
  350. */
  351. public static boolean isRemoteCallSuccess(Document doc) {
  352. Element e = doc.getRootElement();
  353. // 如果不是<Result status='errorCode'></Result>格式的返回,则不进行校验
  354. if (e.element("Result") == null) {
  355. return true;
  356. }
  357. String status = e.attributeValue("status");
  358. if (WSCALL_STATUS_SUCCESS.equals(status)) {
  359. return true;
  360. }
  361. return false;
  362. }
  363. }
在具体调用的时候使用:WSUtil.remoteCall(wsdl, "getAjList", req);//“wsdl”为调用的url,“getList”为方法名字,“req”为传递的参数(json)
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/187007
推荐阅读
相关标签
  

闽ICP备14008679号