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.
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:
Create a new Java Application project. Name it FlowerMtomClient.
Right-click the project node and choose Properties from the context menu. The Project Properties dialog opens.
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.
Right-click the project node and choose New > Web Service Client. The Web Service Client wizard opens.
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.
A Web Service Reference node appears in the project. Expand it and you see the getFlower and getThumbnails methods.
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
}
}
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.
In the catch block, add ex.printStackTrace(); to help see what is going on if there is a problem.
Run the FlowerMtomClient project. The GlassFish output window shows the HTTP request and response:
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.
Open the Admin Console, from the server's node
in the Services window, as shown here:
The Admin Console opens in the browser. Enter your
username and password and press Enter.
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.
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.
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.
Now that we have HTTP logging working correctly,
we will optimize the message transfer of our images.
Open the web service in the Web Service Visual Designer
and select "Optimize Transfer of Binary Data (MTOM)".
In the Services window, restart the server.
While the server starts up, notice the following in the Output window.
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.
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.