Java 调用 webservice 接口,为什么这么慢,有没有好的优化方案,本人目前改成 http 方式发 soap 消息调用,速度依旧很慢,维持在平均 100ms 下不来。

之前用 cxf 的 JaxWsDynamicClientFactory 速度更加慢,400ms 、500ms 都出现过。

这是现在使用 httpClient 的调用代码

public static String callWebSV(String urlWsdl,String soap,String soapAction){

String result=null;

try {

CloseableHttpClient httpClient = HttpClients.createDefault();

HttpPost httpPost = new HttpPost(urlWsdl);

httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8");

httpPost.setHeader("SOAPAction", soapAction);

InputStreamEntity entity = new InputStreamEntity(new ByteArrayInputStream(soap.getBytes()));

httpPost.setEntity(entity);

CloseableHttpResponse response = httpClient.execute( httpPost);

if (response.getStatusLine().getStatusCode() == org.apache.http.HttpStatus.SC_OK) {

HttpEntity responseEntity = response.getEntity();

String back = EntityUtils.toString(responseEntity);

//log.info("httpClient 返回 soap:" + back);

result=parseResult(back);

log.info("httpClient 返回结果:" + result);

} else {

log.error("HttpClinet 返回状态码:" + response.getStatusLine().getStatusCode());

}

} catch (Exception e) {

log.error("调用错误:",e);

}

return result;

}

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐