Home > Articles > Web Services > SOA

  • Print
  • + Share This
Like this article? We recommend

Writing a Web Service

Let's start with the Euro-Ship system. To schedule a package pickup, you send a message to the ground crew. Since it's not the aim of this article to discuss message sending, let's simply use System.out.println() to emulate this function. In the real world, we would send email, JMS, page someone, etc.

The logic for processing a request was shown earlier. First, open the EuroShipWS.jws document. We'll create a data-transfer object named EuroShipReturn, used to return the request results. For brevity, this class is already written for you in the sample code, as an inner class of EuroShipReturn. You just have to uncomment it.

Now, right-click in Design view and select Add Method. Name it pickUp; then go to Code view to change its signature as shown in Listing 1. The implementation for this method follows closely the pseudo-code provided earlier.

Listing 1 Euro-Ship Pickup Web Service

/**
 * This is not a realistic implementation, I know. The implementation details 
 * aren't important for us to learn about Workshop and SOA.
 * @common:operation
 */
public EuroShipReturn pickUp(
  long senderId, 
  String pickUpAddress, 
  Date pickUpTime, 
  String destinationAddress, 
  int taxCode, 
  String taxNumber, 
  String packageType, 
  String usaPackageNumber) 
{
  System.out.println("Euro-ship processing package pick-up request for " + 
      pickUpTime.toString() + " destined to " + destinationAddress);
  EuroShipReturn result = new EuroShipReturn();
  result.setShipmentNumber(nextShipmentNumber());
  GregorianCalendar cal = new GregorianCalendar();
  cal.setTime(pickUpTime);
  cal.add(Calendar.HOUR, 2); //We'll pick up the package 2 hours later than requested.
  
  result.setPickUpTime(cal.getTime());
  
  //The cost is based on the type of package, the pick-up address, and destination address.
  float cost = packageType.hashCode() - 
     destinationAddress.hashCode() + pickUpAddress.hashCode();
  cost = Math.abs(cost);
  result.setSubtotal(cost); 
  
  //The tax rate depends on the tax code.
  float tax = ((float)taxCode) / 100f;
  result.setTax(cost * tax);
  
  result.setTransactionNumber(incrementKey);
  
  if( (usaPackageNumber!=null) && !usaPackageNumber.trim().equals("") ) { 
    //If the destination address is in the USA, forward to CWE here...
    //***
    
  }
  return result;
}

private static long randomKey = System.currentTimeMillis();
private static long incrementKey = 0;

public String nextShipmentNumber() {
  return "ES-" + randomKey + "-" + incrementKey++;    
}

public long nextUSAShipmentNumber() {
  return randomKey - 100000 + incrementKey++;
}

Workshop will now generate a lot of things for you:

  • XML data format for the SOAP request and response. You can take a peek at the kind of XML that will be used by the application. In Design view, right-click the pickup() method and select Edit XML Schema to display samples of data that will be sent in by clients and then returned to them.

  • WSDL definition file. This is needed for third-party customers to connect to the service. You can generate it by right-clicking in the EuroShip.jws and selecting Generate WSDL File. (Ignore any errors in the web application—we'll get to that later.) Then open the WSDL file in Code view. The XML schema is in there.

You can have Workshop take care of the build-and-deployment step by selecting Build, Build Application. By default, the application is deployed to the local development environment.

NOTE

You deploy a service to the real production environment simply by uploading the resulting archive to it. But I digress.

Finally, we need a client to test our application. We could spend some time writing one, or we could use Workshop's debug environment. Let's do that. Select Debug, Start. If it's not already running, start WebLogic Server when prompted. The web browser window in Figure 3 shows up. This allows you to initiate requests and view the response.

Figure 3Figure 3 Debugging the web service in Workshop.

  • + Share This
  • 🔖 Save To Your Account