Portability and Performance Improvements in JAX-WS Clients Created in NetBeans IDE 6.7

DZone 's Guide to

Portability and Performance Improvements in JAX-WS Clients Created in NetBeans IDE 6.7

· Web Dev Zone ·
Free Resource

In NetBeans IDE 6.7, JAX-WS web service clients are both faster and portable out-of-the-box, because the client no longer has to find and parse a remote WSDL file. When you create a client from a WSDL, the IDE generates a copy of this WSDL and saves it in your project. What's more, if the WSDL references any schema files or recursively references other WSDL files, the IDE generates copies of these files as well. The IDE also generates a jax-ws-catalog.xml file that maps the remote system references to local paths.

Before the client can call a web service operation, it has to parse the service WSDL to get information such as the endpoint URL. Thanks to local WSDL copies and catalog mapping, the remote WSDL does not have to be available for the client to run. (Even if the remote WSDL is available, the client runs more quickly without having to look up the WSDL on the Internet.) This means that you can distribute the .war or .jar file built from your client project. As long as the service is running, the client will work on other machines.

To see how portable are JAX-WS clients created in NetBeans 6.7, create a simple client from a WSDL file you have on your machine. The real-world scenario would use the URL of a WSDL on the Internet. For convenience, download the WSDL onto your machine instead of using its URL.

To generate and test the client:

  1. In a browser, go to webservicex.net and download the WSDL file for their Currency Convertor [sic] service. The WSDL is located at http://www.webservicex.net/CurrencyConvertor.asmx?wsdl.

  2. Open NetBeans IDE 6.7 and create a new Java Application project. Name the project CurrencyConvertorClient.
  4. In the Projects window, right-click the project node of CurrencyConvertorClient. Select New > Other > Web Services > Web Service Client. The New Web Service Client wizard opens.
  6. In the WSDL and Client Location screen, select Local File and browse to the location of the downloaded CurrencyConvertor.asmx.wsdl file:

  8. Click Finish. The wizard closes, and the IDE creates the following contents in the project tree:

  10. Open the Main.java file in your CurrencyConvertorClient project. Drag the CurrencyConvertorSoap ConversionRate operation from the Projects window into the method body of the main method, where the comment says //TODO code application logic here. The IDE inserts the following try block into the main method:
    try { // Call Web Service Operation
    net.webservicex.CurrencyConvertor service = new net.webservicex.CurrencyConvertor();
    net.webservicex.CurrencyConvertorSoap port = service.getCurrencyConvertorSoap();
    // TODO initialize WS operation arguments here
    net.webservicex.Currency fromCurrency = null;
    net.webservicex.Currency toCurrency = null;
    // TODO process result here
    double result = port.conversionRate(fromCurrency, toCurrency);
    System.out.println("Result = "+result);
    } catch (Exception ex) {
    // TODO handle custom exceptions here
  12. In order to get output, replace the following two lines:
    net.webservicex.Currency fromCurrency = null;
    net.webservicex.Currency toCurrency = null;

    ...with the following lines, which hard code the client to return the exchange rate from US dollars to Euros.

    net.webservicex.Currency fromCurrency = net.webservicex.Currency.USD;
    net.webservicex.Currency toCurrency = net.webservicex.Currency.EUR;
  14. Run the project. The output window in the IDE shows the following. (Your paths will probably be different, and the exchange rate may be different, too!)
    Created dir: G:\NetBeansProjects\67M2\CurrencyConvertorClient\build\classes
    Compiling 10 source files to G:\NetBeansProjects\67M2\CurrencyConvertorClient\build\classes
    Copying 2 files to G:\NetBeansProjects\67M2\CurrencyConvertorClient\build\classes
    Result = 0.7544
    BUILD SUCCESSFUL (total time: 18 seconds)
  16. Go to the Projects window and find the project's META-INF folder, under Source Packages. In this folder, open jax-ws-catalog.xml. It maps the original WSDL file references to the copy of the WSDL in your project, in META-INF.wsdl. Note that the client looks for the resource in the uri attribute first, and then looks for the original WSDL file.
    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <catalog xmlns="urn:oasis:names:tc:entity:xmlns:xml:catalog" prefer="system">
    <system systemId="file:/G:/installation_files/CurrencyConvertor.asmx.wsdl"
  18. Change the path in the uri attribute to something invalid. Clean and build your project and run it again. The project builds and runs successfully. Although the client could not find the local resource, it could find the resource in systemId.
  20. Move or rename the WSDL you downloaded, which is named in the systemId attribute. Run the project again. The project compiles, because you have a copy of all necessary Java files under Generated Sources. However, the IDE does not display any result, because no WSDL, and therefore no service endpoint, could be found.
  22. Change the value of the uri attribute back to the correct path, wsdl/CurrencyConvertor.asmx.wsdl. Run the project again. Now the result displays correctly, because the compiler can find the local copy of the WSDL in the project files.

You now have a JAX-WS client that can run on any machine connected to the Internet. Clean and build the project. The CurrencyConverterClient.jar file is in the project's dist directory. You can find it in the Files window. You can copy the jar file and port it to any machine with Java, connected to the Internet.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}