Утечка памяти в Java SAAJ

Я разработал REST Api с Java, и одна из служб должна сделать SOAP-вызов к конечной точке. Для этого я реализовал SAAJ-клиент для создания всего сообщения xml. Однако каждый раз при вызове потребляется дополнительно 1 МБ памяти.

import javax.xml.soap.*;import javax.xml.transform.* import javax.xml.transform.stream.*;
public class SOAPClientSAAJ {



public void makeCall() {
    try {
        // Create SOAP Connection
        SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
        SOAPConnection soapConnection = soapConnectionFactory.createConnection();

        // Send SOAP Message to SOAP Server
        String url = "http://ws.cdyne.com/emailverify/Emailvernotestemail.asmx";
        SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url);

        // Process the SOAP Response
        printSOAPResponse(soapResponse);

        soapConnection.close();
    } catch (Exception e) {
        System.err.println("Error occurred while sending SOAP Request to Server");
        e.printStackTrace();
    }
}

private static SOAPMessage createSOAPRequest() throws Exception {
    MessageFactory messageFactory = MessageFactory.newInstance();
    SOAPMessage soapMessage = messageFactory.createMessage();
    SOAPPart soapPart = soapMessage.getSOAPPart();

    String serverURI = "http://ws.cdyne.com/";

    // SOAP Envelope
    SOAPEnvelope envelope = soapPart.getEnvelope();
    envelope.addNamespaceDeclaration("example", serverURI);

    // SOAP Body
    SOAPBody soapBody = envelope.getBody();
    SOAPElement soapBodyElem = soapBody.addChildElement("VerifyEmail", "example");
    SOAPElement soapBodyElem1 = soapBodyElem.addChildElement("email", "example");
    soapBodyElem1.addTextNode("mutantninja@gmail.com");
    SOAPElement soapBodyElem2 = soapBodyElem.addChildElement("LicenseKey", "example");
    soapBodyElem2.addTextNode("123");

    MimeHeaders headers = soapMessage.getMimeHeaders();
    headers.addHeader("SOAPAction", serverURI  + "VerifyEmail");

    soapMessage.saveChanges();

    /* Print the request message */
    System.out.print("Request SOAP Message = ");
    soapMessage.writeTo(System.out);
    System.out.println();

    return soapMessage;
}

/**
 * Method used to print the SOAP Response
 */
private static void printSOAPResponse(SOAPMessage soapResponse) throws Exception {
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    Source sourceContent = soapResponse.getSOAPPart().getContent();
    System.out.print("\nResponse SOAP Message = ");
    StreamResult result = new StreamResult(System.out);
    transformer.transform(sourceContent, result);
}}

При вызове этого сервиса SOAP у меня в объекте отдыха и запускается часть детектора памяти;

    public void restService(){
        SOAPClientSAAJ test= new SOAPClientSAAJ();  
        test.makeCall();

       Runtime runtime = Runtime.getRuntime();
       runtime.gc();
       long memory = runtime.totalMemory() - runtime.freeMemory();
        System.out.println("Used memory is bytes: " + memory);

      }

Он показывает, что каждый вызов потребляет 1 МБ памяти, а в конце приложение Rest ломает. Я много раз отлаживал и видел, что SOAPMessage soapResponse = soapConnection.call(createSOAPRequest(), url); эта часть является причиной утечки, но я не смог найти решение. Любая помощь по этому поводу была бы великолепна.

0 ответов

Другие вопросы по тегам