调用多个 Web 服务并读取第一个响应的最佳方式



我在单个页面中有超过 8 个 Web 服务要调用,一些 Web 服务动态延迟发送响应,这导致更多的等待时间,我需要了解哪种是异步或多线程的最佳方法

这是服务类之一

WebServiceSOAPClient.java这是Web服务客户端类之一

public class WebServiceSOAPClient {
private String action = "";
private String authorization = "";
private String endpointUrl = "";
private String method = "";
private String password = "";
private String requestXml = "";
private String username = "";
public WebServiceSOAPClient(String endpointUrl, String action, String method, String requestXml) {
this.action = action;
this.endpointUrl = endpointUrl;
this.requestXml = requestXml;
}
public String getResponse() {
try {
// Create the connection where we're going to send the file
HttpURLConnection httpConn = (HttpURLConnection) (( new URL(this.endpointUrl) ).openConnection() );
// Set the appropriate HTTP parameters
httpConn.setRequestProperty("Content-Length", String.valueOf((this.requestXml.getBytes(Charset.forName("UTF-8"))).length));
httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
httpConn.setRequestProperty("SOAPAction", this.action);
if(!this.authorization.equals(""))
httpConn.setRequestProperty("Authorization", this.authorization);
httpConn.setRequestMethod("POST");
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
// Send the XML now in the requestByteArray.
OutputStream out = httpConn.getOutputStream();
out.write(requestByteArray);
out.close();
// Read the response and write it to standard out
BufferedReader in = new BufferedReader( new InputStreamReader(httpConn.getInputStream()) );
String inputLine;
String response = "";
while ((inputLine = in.readLine()) != null)
response += inputLine;  
in.close();
// Return reponse
return response;
} catch (Exception e) {
return "ERROR: " + e.toString();
}
}
public void setAuthorization(String user, String pass) {
setAuthorization("Basic", user, pass);
}
public void setAuthorization(String type, String user, String pass) {
this.username = user;
this.password = pass;
String userpass = this.username + ":" + this.password;
this.authorization = type + " " + javax.xml.bind.DatatypeConverter.printBase64Binary(userpass.getBytes());
}
//Getter and setters code 
// to String code 
}

Web 服务.java所有 Web服务都被调用并附加来自具有不同配置的不同 Web 服务的响应...

String responseXml = "";

//------ Calling first web service -----//
String endpointUrl = "URL_1";
String requestXml = ""
+ "<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:mat="http://localhost:8989/v1/pl/MaterialList">"
+ "<soapenv:Header/>"
+ "<soapenv:Body>"
+ "<mat:MT_MaterialListRequest>"
+ "<MaterialList>"
+ "<UserID>" + userEID + "</UserID>"
+ "<Material_No>" + textSearch + "</Material_No>"
+ "</MaterialList>"
+ "</mat:MT_MaterialListRequest>"
+ "</soapenv:Body>"
+ "</soapenv:Envelope>";
WebServiceSOAPClient wsClient = new WebServiceSOAPClient(endpointUrl, "http://localhost:8080/xi/WebService/soap1.1", "POST", requestXml);
wsClient.setAuthorization("userName", "password"); 
//------ Waiting for the first web service  response -----//
responseXml = wsClient.getResponse();

//------ Calling Second web service -----//
endpointUrl = "URL_2";
requestXml = ""
+ "<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:mat="http://localhost:8988/v1/pl/MaterialList">"
+ "<soapenv:Header/>"
+ "<soapenv:Body>"
+ "<mat:MT_MaterialListRequest>"
+ "<MaterialList>"
+ "<Material_No>" + textSearch + "</Material_No>"
+ "</MaterialList>"
+ "</mat:MT_MaterialListRequest>"
+ "</soapenv:Body>"
+ "</soapenv:Envelope>";
WebServiceSOAPClient wsClient = new WebServiceSOAPClient(endpointUrl, "http://localhost:8081/xi/WebService/soap1.1", "POST", requestXml);
wsClient.setAuthorization("userName", "password"); 
//------ Waiting for the Second web service  response -----//
responseXml = wsClient.getResponse();
//goes on...

建议一种更好的方法来读取独立于等待队列的响应(多线程或异步(。

使用 Java 中的 ExecutorService,您可以在不同的线程中设置您的请求,就像这样,您必须为您的请求创建一个类,我在这里用字符串显示它:

String[] requests_to_process;
for(String s : requests_to_process){
//Starts independent thread for every runnable
ex.execute(()->{
WebServiceSOAPClient wsClient = new WebServiceSOAPClient(endpointUrl, 
"http://localhost:8080/xi/WebService/soap1.1", "POST", requests_to_process);
wsClient.setAuthorization("userName", "password"); 
//------ Waiting for the first web service  response -----//
responseXml = wsClient.getResponse();
});
}

相关内容

最新更新