Portability and Performance in JAX-WS Clients built in NB 6.7

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 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.
  3. 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.
  4. In the WSDL and Client Location screen, select Local File and browse to the location of the downloaded CurrencyConvertor.asmx.wsdl file.
  5. Click Finish. The wizard closes, and the IDE creates the following contents in the project tree.
  6. 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
    }
  7. 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;
  8. 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!)
    wsimport-client-generate:
    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
    compile:
    run:
    Result = 0.7544
    BUILD SUCCESSFUL (total time: 18 seconds)
  9. 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" 
    	uri="wsdl/CurrencyConvertor.asmx.wsdl"/>
    </catalog>
  10. 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.
  11. 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.
  12. 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.
Comments:

Great tutorial!

Posted by guest on April 02, 2009 at 03:27 PM CEST #

Thanks a lot. I really needed this.

Posted by Chet on July 12, 2009 at 11:01 AM CEST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

I'm a technical writer for NetBeans, covering web service support.

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today