繁体   English   中英

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

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

我在一个页面中有 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...


建议一种更好的方法来读取响应以独立于等待队列(MutiThreading 或异步)

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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