Design Patterns    |     Security    |     Testing    |     Distributed Computing    |     Contact

Distributed Computing

 

Web Services

W3C Definition of Web Services

A Web service is a software system identified by a URI whose public interfaces and bindings are defined and described using XML.

Its definition can be discovered by other software systems. These systems may then interact with the Web service in a manner prescribed by its definition, using XML-based messages conveyed by Internet protocols.

Advantages of Using Web Services

  • Based on open standards (XML, SOAP, WSDL, UDDI)
  • Interoperability: capable to operate on different operating systems using different programming languages
  • Operate over the Internet
  • Easy integration with existing systems (it is relatively easy to implement a web services interface to an existing system)
  • Support various client types
  • Increased programming productivity

Challenges

  • Security
  • Reliability
  • Scalability
  • Availability

Web Services Standards

  • Common markup language for communication (XML)
  • Common message format for exchanging information (SOAP)
  • Common service specification formats (WSDL)
  • Common means for service lookup (UDDI)

Creating a Web Services

There are two major approaches:

Java – to – WSDL (Bottom-Up) : Start with the Java class that implements the service (methods) and from it create the WSDL

WSDL – to – Java (Top-Down) : Start with the WSDL and based on it design the Java interface and implementation

Example: Creating a Calculator Web Services using Java-to- WSDL Approach

We will use the same Calculator example used for RMI. Similar with the RMI example, we will define a Java interface which describes the Calculator methods (add, subtract, multiply, divide) and a Java implementation class that implements those methods.

Steps in Creating a Web Service

  1. Create Java Interface
  2. Create Java Implementation
  3. Develop, Assemble, Deploy
  4. Testing the Installation
  5. Generate The Web Service Stub
  6. Write Java Client

1. Create Java Interface

public interface Calculator extends Remote {
	public long subtract(long a, long b) throws java.rmi.RemoteException;
	public long add(long a, long b) throws java.rmi.RemoteException;
	public long multiply(long a, long b) throws java.rmi.RemoteException;
	public long divide(long a, long b) throws java.rmi.RemoteException;
}
// similar with RMI, the interface extends Remote

2. Create Java Implementation

public class CalculatorImpl implements Calculator {
	public CalculatorImpl() { 
        super(); 
	} 
public long add(long a, long b) throws java.rmi.RemoteException {
        return a + b; 
} 
public long subtract(long a, long b) throws java.rmi.RemoteException { 
        return a - b; 
}
// similar for multiply and divide
// unlike in RMI, CalculatorImpl does not extend UnicastRemoteObject

3. Develop, Assemble, Deploy

Eclipse automates all phases involved in making a web service accessible to users:

  • Define the WSDL
  • Assembling
  • Deployment on target application server

4.Testing the Installation

After deployment the web service runs as a web application under the application server (Tomcat)

Similar to a web application, it can be accessed trough a URL. In order to find out the correct URL, open WSDL and look for the correct value for port binding:

<wsdl:port binding="impl:CalculatorImplSoapBinding“ name="CalculatorImpl">
  <wsdlsoap:address location="http://localhost:8080/WSServer/services/CalculatorImpl"/>
</wsdl:port>

The WSDL can be accessed and displayed remotely appending ?WSDL to the above URL:

http://localhost:8080/WSServer/services/CalculatorImpl?WSDL

4. Generate the Web Service Stub

A Java client accesses the web service trough a web service stub. The stub is generated from WSDL

The easiest way to create the stub is using Eclipse

Bring a copy of WSDL file under the client project

Right click on WSDL, select Web Services then Generate Client

As a result, several Java classes will be automatically generated

5. Write the Java Client

public class CalculatorClient {
	public static void main(String[] args) {
		try {
		   CalculatorImplService service = new 			         	   
		   CalculatorImplServiceLocator();
		    CalculatorImpl port = service.getCalculatorImpl();
		    long result = port.add(3, 4);
		    System.out.println("invoked add(3, 4) = " + result);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}