O'Reilly logo

Java SOA Cookbook by Eben Hewitt

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Monitoring SOAP Traffic with TCPMon


You want to monitor SOAP traffic without putting anything on your classpath.


Use TCPMon, an open source monitor for TCP traffic, available from https://tcpmon.dev.java.net. You’ll need to change the values in your local WSDL to use it.


You can run the tool a few ways—directly from the Web using Java Web Start, or by downloading the JAR. It will listen on an available port, and you must route requests through it for it to be able to dump the traffic data. TCPMon will then forward the requests on to the actual destination.

Running TCPMon

The easiest way to get started with TCPMon is to visit the website and find the link that says “Click here to run directly from the web.” If you don’t want to do that, you can download it and execute it on Windows by double-clicking it or on Linux by typing this command:

$ java -jar tcpmon.jar

This will start up the Java Web Start program. Here are the steps to get it up and running. You’ll use a NetBeans sample project because it gets in the way the least. If you have another web service and client you want to use, the general instructions are still relevant:

  1. Start the application. In the GUI, change the default values to the following:

    • Local Port: 8090

    • Server Port: 8080

  2. Click “Add Monitor,” which will run the listener.

  3. You want to send a SOAP request to port 8080 because that’s the port your service is listening on. So you need to change your WSDL to point to 8090 so that TCPMon can dump the message. Then the value you supplied in the Server Port field is the target to which TCPMon will forward each request. Because all you care about here is illustrating the traffic dump, and not the service itself, you’ll just create a new web service sample project in NetBeans. To do so, click File→New Project→Samples→Web Service→Calculator. Of course, you can use any project you like. Doing so will also create a Calculator Client project, containing a web service reference.

    When the client project was created, it imported a copy of the WSDL from the service. This is the copy you need to change to point to TCPMon. To access the WSDL, navigate to Configuration Files→xml-resources→web-service-references, and find the WSDL under it. If you’re using a different web service client project, just locate the local copy of the WSDL that is being invoked. Find this section at the end of the WSDL file:

    <soap:address location="http://localhost:8080/CalculatorApp/CalculatorWSService">

    Change the location to http://localhost:8090/CalculatorApp/CalculatorWSService.

  4. Now you need to change your application to use the modified WSDL. If you’re using the NetBeans sample, find the ClientServlet Java source file in the org.me.calculator.client package. Find this line:

    @WebServiceRef(wsdlLocation = 

    Modify it to use the TCPMon port number of 8090. Because you’re in a servlet, the container will inject the service instance at runtime, and use the modified WSDL location.

    If you weren’t using a servlet, but had generated portable artifacts from wsimport, you could use code similar to this:

    URL wsdlLocation = new URL("file:///C:/projects/etc/CalculatorWSService.wsdl");        
    QName serviceName = new QName("http://calculator.me.org/",            
    CalculatorWSService service = 
      new CalculatorWSService(wsdlLocation, serviceName);          
    CalculatorWS port = service.getCalculatorWSPort();      
    int result = port.add(2, 3);

    The thing that you’re doing here is overriding the WSDL that the client will use when invoking the service; you’re pointing to the file that you’ve modified manually.

  5. Now you’ll make a request, routed through the monitor. If necessary, clean and build the web service Calculator project, and make sure it is deployed using the Undeploy and Deploy target.

    Now right-click on the web service Calculator Client project and choose Run.

    Then go to the monitor application and click Submit to Server. The monitor will display a timestamp of your request and show the request and response contents, including HTTP headers, in the windows.

TCPMon is shown in action in Figure 4-10.

TCPMon showing a SOAP request and response

Figure 4-10. TCPMon showing a SOAP request and response

Using TCPMon is pretty easy to use given all it’s doing for you, and it’s a terrific aid when you’re debugging. Just remember to change your WSDLs back.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required