Alan Kaminsky Department of Computer Science Rochester Institute of Technology 4486 + 2220 = 6706
Home Page
Distributed Systems 4005-730-01 Spring Quarter 2013
Course Page

4005-730 Distributed Systems
Web Services Example

Prof. Alan Kaminsky
Rochester Institute of Technology -- Department of Computer Science

Acknowledgment
Overview
Source Code
Demonstration
HTTP Messages


Acknowledgment

This example is taken from Martin Kalin, Java Web Services: Up and Running (O'Reilly, 2009).


Overview

Write a simple time server in Java and publish it as a SOAP-based web service.

The example uses the Java API for XML Web Services (JAX-WS) that is part of the standard Java platform starting with JDK 1.6. All code must be compiled and executed using JDK 1.6. The API is in package javax.jws, package javax.xml, and their subpackages.

For further information, see the Metro Web Services Stack web site (http://metro.java.net/).


Source Code


Demonstration

  1. Run the server; listen to port 56789.
    $ java ch01.ts.TimeServerPublisher localhost 56789
    

  2. To view the service's WSDL specification, browse this URL: http://localhost:56789/ts?wsdl

  3. Run the client; connect to port 56789.
    $ java ch01.ts.TimeClient localhost 56789
    Sun Jan 31 12:17:15 EST 2010
    1264958235807
    

    To watch the HTTP traffic between the client and the server, use the tcpmon utility. Download tcpmon.jar from https://tcpmon.dev.java.net/.

  4. Run tcpmon; configure it to connect to port 56789 and listen to port 8080.
    $ java -jar tcpmon.jar &
    

  5. Run the client; connect to port 8080.
    $ java ch01.ts.TimeClient localhost 8080
    Sun Jan 31 14:14:44 EST 2010
    1264965285026
    

  6. tcpmon shows the HTTP messages that go back and forth between the client and the server.


HTTP Messages

  1. The client sends an HTTP GET request message to the server, asking for the service's WSDL document.

  2. The server sends an HTTP response message to the client, with the service's WSDL document.
    <?xml version="1.0" encoding="UTF-8"?>
    <!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.1 in JDK 6. -->
    <!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.1 in JDK 6. -->
    <definitions
            xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
            xmlns:tns="http://ts.ch01/"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns="http://schemas.xmlsoap.org/wsdl/"
            targetNamespace="http://ts.ch01/"
            name="TimeServerImplService">
        <types></types>
        <message name="getTimeAsString"></message>
        <message name="getTimeAsStringResponse">
            <part name="return" type="xsd:string"></part>
        </message>
        <message name="getTimeAsElapsed"></message>
        <message name="getTimeAsElapsedResponse">
            <part name="return" type="xsd:long"></part>
        </message>
        <portType name="TimeServer">
            <operation name="getTimeAsString" parameterOrder="">
                <input message="tns:getTimeAsString"></input>
                <output message="tns:getTimeAsStringResponse"></output>
            </operation>
            <operation name="getTimeAsElapsed" parameterOrder="">
                <input message="tns:getTimeAsElapsed"></input>
                <output message="tns:getTimeAsElapsedResponse"></output>
            </operation>
        </portType>
        <binding name="TimeServerImplPortBinding" type="tns:TimeServer">
            <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="rpc"></soap:binding>
            <operation name="getTimeAsString">
                <soap:operation soapAction=""></soap:operation>
                <input>
                    <soap:body use="literal" namespace="http://ts.ch01/"></soap:body>
                </input>
                <output>
                    <soap:body use="literal" namespace="http://ts.ch01/"></soap:body>
                </output>
            </operation>
            <operation name="getTimeAsElapsed">
                <soap:operation soapAction=""></soap:operation>
                <input>
                    <soap:body use="literal" namespace="http://ts.ch01/"></soap:body>
                </input>
                <output>
                    <soap:body use="literal" namespace="http://ts.ch01/"></soap:body>
                </output>
            </operation>
        </binding>
        <service name="TimeServerImplService">
            <port name="TimeServerImplPort" binding="tns:TimeServerImplPortBinding">
                <soap:address location="http://localhost:8080/ts"></soap:address>
            </port>
        </service>
    </definitions>
    

  3. The client sends an HTTP POST request message to the server, requesting a remote method call be performed on the getTimeAsString() method.
    <?xml version="1.0" ?>
    <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
        <S:Body>
            <ns2:getTimeAsString xmlns:ns2="http://ts.ch01/"/>
        </S:Body>
    </S:Envelope>
    

  4. The server sends an HTTP reply message to the client, with the return value of the getTimeAsString() method call.
    <?xml version="1.0" ?>
    <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
        <S:Body>
            <ns2:getTimeAsStringResponse xmlns:ns2="http://ts.ch01/">
                <return>Sun Jan 31 14:14:44 EST 2010</return>
            </ns2:getTimeAsStringResponse>
        </S:Body>
    </S:Envelope>
    

  5. The client sends an HTTP POST request message to the server, requesting a remote method call be performed on the getTimeAsElapsed() method.
    <?xml version="1.0" ?>
    <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
        <S:Body>
            <ns2:getTimeAsElapsed xmlns:ns2="http://ts.ch01/"/>
        </S:Body>
    </S:Envelope>
    

  6. The server sends an HTTP reply message to the client, with the return value of the getTimeAsElapsed() method call.
    <?xml version="1.0" ?>
    <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
        <S:Body>
            <ns2:getTimeAsElapsedResponse xmlns:ns2="http://ts.ch01/">
                <return>1264965285026</return>
            </ns2:getTimeAsElapsedResponse>
        </S:Body>
    </S:Envelope>
    

Distributed Systems 4005-730-01 Spring Quarter 2013
Course Page
Alan Kaminsky Department of Computer Science Rochester Institute of Technology 4486 + 2220 = 6706
Home Page
Copyright © 2011 Alan Kaminsky. All rights reserved. Last updated 23-Sep-2011. Please send comments to ark­@­cs.rit.edu.