gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 09:01:04 25 4
gpt4 key购买 nike

我在一个页面中要调用超过 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

}

webservice.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();
});
}

关于java - 调用多个 Web 服务并读取第一个响应的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60145451/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com