通用客户端实现方案-eclipse生成WebService客户端以及使用(https://blog.csdn.net/u014677702/article/details/85070833WebService)
eclipse生成webservice客户端高并发客户端调用-发送报文出现重复问题处理
package com.hand.wbs.ws.service.test;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import org.apache.axis.AxisFault;
import com.hand.wbs.ws.service.FileInfo;
import com.hand.wbs.ws.service.FileInfoGetLocator;
import com.hand.wbs.ws.service.FileInfoGetSoapBindingStub;
public class RunningTaskWorker implements Runnable {
private FileInfo fileInfo;
private String endpoint;
private CountDownLatch latch;
private Map<String, String> map;
public RunningTaskWorker(String endpoint,FileInfo fileInfo, CountDownLatch latch,
Map<String, String> map) {
this.fileInfo = fileInfo;
this.latch = latch;
this.map = map;
this.endpoint = endpoint;
}
public void run() {
try {
FileInfoGetSoapBindingStub stub = null;
try {
URL endpointURL = new URL(endpoint);
stub = new FileInfoGetSoapBindingStub(endpointURL, new FileInfoGetLocator());
} catch (AxisFault | MalformedURLException e) {
e.printStackTrace();
}
if (null == stub) {
System.out.println("接口初始化错误");
return;
}
System.out.println("开始发报文:" + fileInfo.getFileNm());
stub.ordinaryMsg(fileInfo);
//System.out.println("完成发报文:" + fileInfo.getFileNm());
} catch (Exception e) {
if (!e.getMessage().endsWith("org.xml.sax.SAXException: Bad envelope tag: service")) {
e.printStackTrace();
map.put(fileInfo.getFileNm(), fileInfo.getFileNm());
// System.out.println("发生异常1:" + e.getMessage());
} else {
//System.out.println("完成发报文:" + fileInfo.getFileNm());
}
} catch (Throwable e) {
if (!e.getMessage().endsWith("org.xml.sax.SAXException: Bad envelope tag: service")) {
e.printStackTrace();
map.put(fileInfo.getFileNm(), fileInfo.getFileNm());
// System.out.println("发生异常1:" + e.getMessage());
} else {
//System.out.println("完成发报文:" + fileInfo.getFileNm());
}
} finally {
latch.countDown();// 线程完成任务,计数器减一
} // end catch
}
}
问题原因,多线程高并发出现线程间资源不安全问题出现导致发报文重复:
根本原因是:FileInfoGetSoapBindingStub stub = new FileInfoGetSoapBindingStub(endpointURL, new FileInfoGetLocator()); 线程不安全,必须确保每次发送报文new一个响应的接口对象,才能解决重复报文问题