【问题标题】:Soap call slower on remote server远程服务器上的肥皂调用速度较慢
【发布时间】:2019-04-17 07:58:41
【问题描述】:

我有一个 Java 服务器应用程序,它对另一个远程服务器进行一些 Soap 调用。

我已将我的 Java 服务器应用程序放在带有 tomcat 8.5 的远程服务器上。当我进行 Soap 调用时,它需要双倍的时间,而在我的本地机器上使用 Eclipse 上的 tomcat 8.5 会更快。

你知道为什么吗?

这是我的肥皂客户端的代码:

public class SoapHelper {

public String server   = "";
public String username = "";
public String password = "";
public String session  = "";  // this is the session id returned by the server upon successful login
private SOAPConnection con = null;
private MessageFactory mf = null;

public String service = "";
public String method  = "";
public String request  = "";  // this is what we send to the server
public String response = "";  // this is what the server return to us

public SoapHelper(String server) {
  this.server = server;
}

private String getURI() {
  return "https://" + this.server + this.session;
}

private SOAPMessage makeMessage(String nodeName, String xmlStr) throws Exception {

  SOAPMessage message = this.mf.createMessage();
  SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();

  envelope.addNamespaceDeclaration("xsi", "http://www.w3.org/1999/XMLSchema-instance");
  envelope.addNamespaceDeclaration("xsd", "http://www.w3.org/1999/XMLSchema");

  SOAPBody body = envelope.getBody();

  SOAPElement element = body.addChildElement(envelope.createName("ns1:" + this.method));
  element.addAttribute(envelope.createName("xmlns:ns1"), "urn:" + this.service);
  element.addAttribute(envelope.createName("ns1"), "http://schemas.xmlsoap.org/soap/encoding");

  SOAPElement ele2 = element.addChildElement(envelope.createName(nodeName));
  ele2.addAttribute(envelope.createName("xsi:type"), "xsd:string");
  ele2.addTextNode(xmlStr);

  message.saveChanges();

  return message;
}

private void doConnect()
{
  try {
    SOAPConnectionFactory conFactory = SOAPConnectionFactory.newInstance();
    this.con = conFactory.createConnection();
    this.mf = MessageFactory.newInstance();
  } catch(Exception e) {}
}

public boolean doRequest(String service, String method, String xml) 
{
this.service = service;
this.method  = method;
this.request = "";
this.request = xml;

try {
    URL endpoint = new URL(this.getURI());
    SOAPMessage message = this.makeMessage("msgstr", this.request);
    SOAPMessage retval = this.con.call(message, endpoint);
    //extraction du XML en String lisible du message SOAP
    this.response = extractXML(retval);
} catch (Exception e) {
    this.response = e.getMessage();
}
return true;
}

private String extractXML(SOAPMessage message) throws Exception {

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    message.writeTo(out);
    String returnxml = new String(out.toByteArray());
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document document = db.parse(new InputSource(new StringReader(returnxml)));
    Element root = document.getDocumentElement();
    Node msg = root.getLastChild();

    return msg.getTextContent();
  }

private String getSession() throws Exception {

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document document = db.parse(new InputSource(new StringReader(this.response)));
    Element root = document.getDocumentElement();

    return root.getAttribute("sessionid");
}

public void authenticate(String username, String password){

this.username = username;
this.password = password;

 try {
   String xml="<Message messageid='0'><Entity name='REF_LOGIN'>";
   xml+="<Property name='login_cd' value='" + this.username + "' type='string'/>";
   xml+="<Property name='password' value='" + this.password + "' type='string'/>";
   xml+="<Property name='machine_name' value='" + getMachineName() + "' type='string'/>";
   xml+="</Entity></Message>";
   doConnect();
   doRequest("Login","Authenticate",xml);
   this.session = this.getSession();
 }catch(Exception e)
 {
   this.session = e.getMessage();
 }
}

【问题讨论】:

  • 你能提供一些代码吗?再具体一点?
  • 什么类型的代码?我的肥皂客户?

标签: java xml soap


【解决方案1】:

很可能是它的网络带宽问题(如果不是其他问题),因为当您将 SOAP 消息从一台机器发送到另一台远程机器时,它需要来回传输到网络,如果消息大小很大,则网络带宽一定是个问题,而且要花很多时间。

举个例子。

如果网络行程时间 =500 毫秒

SOAP 服务 = 400 毫秒

那么远程服务器的总时间 = 400+500 毫秒(约 2*400 毫秒)。

这里有一些可以研究它的指针。

1)尝试记录服务器处理的时间,服务结束的入口点,如果它与你在本地得到的完全相同或接近,那么它证明它的网络问题只是。

可以通过多种方式解决网络延迟问题。选择附近的数据中心、隧道、专用网络连接等。

2)如果不是上述情况,您可能正在尝试连接到远程数据库或 SOAP 服务中的其他东西,这可能再次出现网络带宽问题或其他问题(如数据库容量或索引丢失或与local) else 但这证明它不是网络带宽问题,你需要解决这个问题。

希望这能提供一些背景信息,但要获得准确的答案,您需要在问题中添加更多详细信息。

【讨论】:

  • 感谢您的回答。我为我的服务做了一些日志。而且我可以确认远程服务的时间比本地慢。而在远程需要双倍时间的是肥皂过程。
  • 那么如果是网络带宽问题,我该如何解决呢?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-02
相关资源
最近更新 更多