corner imagecorner image
IDEPlatformPluginsDocs & SupportCommunityPartners

Web Service Passing Binary Data, pt7: Logging and Optimizing the Service

Download the sample

JAX-WS provides an easy and effective way to optimize binary data transfer. It is known as "message optimization", provided by the Message Transmission Optimization Mechanism (MTOM). Message optimization is the process of transmitting web service messages in the most efficient manner. It is achieved in web service communication by encoding messages prior to transmission and then de-encoding them when they reach their final destination. MTOM uses XOP (XML-binary Optimized Packaging) to transmit binary data to and from the web service. The Web Service Designer lets you enable MTOM easily, as shown in this section and described here.

Lessons In This Tutorial

Content on this page applies to NetBeans IDE 6.5
  1. Overview
  2. Creating the EJB Module
  3. Creating the Web Service
  4. Testing the Web Service
  5. Modifying the Schema and WSDL Files to Pass Binary Data
  6. Creating the Swing Client
  7. => Logging and Optimizing the Web Service

Logging and Optimizing the Web Service

To be able to verify that MTOM is working correctly, create a new, simple client that will let you examine the response messages in the IDE Output window.

To Log and Optimize the Web Service:

  1. Create a new Java Application project. Name it FlowerMtomClient.
  2. Right-click the project node and choose Properties from the context menu. The Project Properties dialog opens.
  3. Go to the Run category. In the VM Options field, type or paste -Dcom.sun.xml.ws.transport.http.client.HttpTransportPipe.dump=true. This option sends HTTP transport information to the GlassFish Output window. Click OK and the dialog closes.
    Project Properties for the simple MTOM test client, showing the JVM run property
  4. Right-click the project node and choose New > Web Service Client. The Web Service Client wizard opens.
  5. Click Browse next to the Project field. A browsing dialog opens. Select the FlowerService web service and click OK. You return to the New Web Service Client wizard with the WSDL location filled in. Click Finish.
    New Web Service Client wizard for simple MTOM test client, showing the WSDL location for the Flower Service
  6. A Web Service Reference node appears in the project. Expand it and you see the getFlower and getThumbnails methods.
    Web Service References node for simple MTOM test client
  7. Open the Main.java class in FlowerMtomClient. Drag and drop the getFlower method into the main method body. A try block appears in the code.
    public static void main(String[] args) {
    
        try { // Call Web Service Operation
            flower.album.FlowerService_Service service = new flower.album.FlowerService_Service();
            flower.album.FlowerService port = service.getFlowerServicePort();
            // TODO initialize WS operation arguments here
            java.lang.String arg0 = "";
            // TODO process result here
            java.awt.Image result = port.getFlower(arg0);
            System.out.println("Result = " + result);
        } catch (Exception ex) {
                // TODO handle custom exceptions here
        }
    }
            
  8. Change the line java.lang.String arg0 = ""; to java.lang.String arg0 = "rose";. This passes test input data so that the server returns the rose.jpeg image.
  9. In the catch block, add ex.printStackTrace(); to help see what is going on if there is a problem.
  10. Run the FlowerMtomClient project. The GlassFish output window shows the HTTP request and response:
    ---[HTTP request]---
    Host: localhost:8080
    Content-type: text/xml; charset=utf-8
    Content-length: 197
    Connection: keep-alive
    User-agent: Java/1.6.0_07
    Accept: text/xml, multipart/related, text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
    Soapaction: ""
    <?xml version="1.0" ?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
    <S:Body><ns2:getFlower xmlns:ns2="http://album.flower/">
    <arg0>rose</arg0></ns2:getFlower></S:Body>
    </S:Envelope>
    --------------------
    ---[HTTP response 200]---
    <?xml version="1.0" ?>
    <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
    <S:Body><ns2:getFlowerResponse xmlns:ns2="http://album.flower/">
    <return>iVBORw0KGgoAAAANSUhEUgAAAoAAAAKACAIAAACDr150AACAAElEQVR42ly8B3MbWZqu2Xe6p0siCW/TAJkAEt577wlDEI4ACEOAMPTekyIpUqIoV/ISZUulUnm5qnbTXTN3J2Y37p3YiI2J/VP7Jdjdt+8q3jiRgIAEkAmc53vOOclfYeKLODJAigchMhFDLmaqEbYW
    ...LOTS OF ASCII...
    8bdLd7LMz0gIDckNC80ZGJYaEFESHl8TEVG8tKinKrKKftWzN/zw9yf1y3Zv3rh7qWzti0gk5CrZhdvqijYNCO3qiJn/bQMxD8k+Rnw+rIstMoBPN6EUnYqLAMwOalw5aQ0ZFXnZsbMSouemxWPDPG0pLA5mfHTkyNlQ4JjqqYV7F04+e
    </return>
            

    Here you see that the binary is included directly in the <return> element, and it is very, very long.

  11. Reopen FlowerService.java and add the @javax.xml.ws.soap.MTOM annotation above the @WebService annotation, as shown:
    @javax.xml.ws.soap.MTOM
        @WebService(serviceName = "FlowerService", wsdlLocation="WEB-INF/wsdl/FlowerService.wsdl")
  12. Undeploy the FlowerApplication from the GlassFish node in the Services window. Clean and build the FlowerService project and then the FlowerApplication project. Then deploy FlowerApplication again.
  13. Open the Admin Console, from the server's node in the Services window, as shown here:
    Java application with consumed ws

    The Admin Console opens in the browser. Enter your username and password and press Enter.

  14. In the Admin Console, select Application Server from the tree menu. The Application Server page opens. Select the JVM Settings > JVM Options tab, shown below, and add the following JVM option on the server.
    -Dcom.sun.xml.ws.transport.http.HttpAdapter.dump=true

    Java application with consumed ws
  15. In the Services window, restart the server.
  16. While the server starts up, look in the Output window and make sure you see the property amongst the other server output, as shown below.
    Java application with consumed ws
  17. Redeploy the enterprise application to the Tester application, invoke an operation via the Tester application, and notice the Output window again, showing HTTP requests and responses, because of the logging mechanism that you enabled in the previous steps.
    Java application with consumed ws

    Now that we have HTTP logging working correctly, we will optimize the message transfer of our images.

  18. Open the web service in the Web Service Visual Designer and select "Optimize Transfer of Binary Data (MTOM)".
    Java application with consumed ws
  19. In the Services window, restart the server.
  20. While the server starts up, notice the following in the Output window.
    Java application with consumed ws

    As indicated in the highlighted lines in the Output window, by using MTOM we ensure that binary data is not contained within the SOAP body. Instead, it is sent as a SOAP attachment, while the attachment is included in the SOAP message.



See Also

For more information about using NetBeans IDE 6.1 to develop Java EE applications, see the following resources:

To send comments and suggestions, get support, and keep informed on the latest developments on the NetBeans IDE Java EE development features, join the mailing list.