简体   繁体   中英

Best way to Invoke multiple web services and read the first response

I have more than 8 web services to invoke in a single page, some web services dynamically delay in sending the response which is leading for more waiting time, I need to understand which is the best way Asynchronous or MultiThreading

this is one of the services class

WebServiceSOAPClient.java this is one of the web service clients class

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 All web service is invoked and appending the response from different web services who have different configurations...

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...


Suggest a better way where to read the responses to be independent of waiting on queue ( MutiThreading or Asynchronous ) .

Use the ExecutorService in Java, you can setup your requests in different threads, like this, you have to create a class for your requests, im showing it with strings here:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM