Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

The SOAP RPC Conventions

Once you have the SOAP data model, it's quite natural to map remote procedure call (RPC) interactions to SOAP by using a struct to represent a call. The best way to describe this is with an example.

Let's say we have this method in Java:

public int addFive(int arg);

A request message representing a call to this method in SOAP would look something like this:

<env:Envelope>
 <env:Body>
  <myNS:addFive xmlns:myNS="http://my-domain.com/"
     enc:encodingStyle="http://">
   <arg xsi:type="xsd:int">33</arg>
  </myNS:addFive>
 </env:Body>
</env:Envelope>

Notice that the method name has been translated into XML (the rules by which a name in a language like Java gets turned into an XML name, and vice versa, can be found in part 2 of the SOAP 1.2 spec), and we've put it in a namespace that is specific to our service (this is common practice, but not strictly necessary). The method invocation, as we mentioned, is an encoded struct with one accessor for each argument, so the arg element is inside the method element, and the argument contains the value we're passing: 33.

If we pass this message to a service, the response looks something like this:

<env:Envelope>
 <env:Body>
  <myNS:addFiveResponse xmlns:myNS="http://my-domain.com/"
        xmlns:rpc="http://www.w3.org/2003/05/soap-rpc"
     enc:encodingStyle="http://">
   <rpc:result>ret</rpc:result>
   <ret xsi:type="xsd:int">38</ret>
  </myNS:addFive>
 </env:Body>
</env:Envelope>

The RPC response is also modeled as a struct, and by convention the name of the response struct is the name of the method with Response appended to the end. The struct contains accessors for all inout and out parameters in the method call (see the next section) as well as the return value.

The first accessor in the struct is interesting, and it brings to light another difference between SOAP 1.1 and 1.2: In SOAP 1.1's RPC style, there was no way to tell which accessor in the struct was the return value of the method and which were the out parameters. This was a problem unless you had good meta-data, and even then the situation could be confusing. SOAP 1.2 resolves this issue by specifying that an RPC response structure containing a return value must contain an accessor named result in the SOAP RPC namespace. The value of this field is a QName that names the accessor containing the return value for the invocation.

out and inout Parameters

In some environments, programmers use out parameters g to enable returning multiple values from a given RPC call. For instance, if we wanted to return not only a Boolean yes/no value from our inventory check service but also the actual number of units in stock, we might change our signature to something like this (using pseudocode):

  boolean doCheck(String SKU,
          int quantity,
          out int numInStock)

The idea is that the numInStock value is filled in by the service response as well as returning a Boolean true/false. Inout parameters g are similar, except that they also get passed in—so we could use an inout like this:

  boolean doCheck(String SKU,
          inout int quantity)

In this situation, we'd pass a quantity value in, and then we expect the value of the quantity variable to have been updated to the actual quantity available by the service.

Java developers aren't used to the concept of inout or out parameters because, typically, in Java all objects are automatically passed by reference. When you're using RMI, simple objects may be passed by value, but other objects are still passed by reference. In this sense, any mutable objects (whose state can be modified) are automatically treated as inout parameters. If a method changes them, the changes are seen automatically by anyone else.

In Web services, the situation is different: All parameters are passed by value. SOAP has no notion of passing parameters by reference. This design decision was made in order to keep SOAP and its data encoding simple. Passing values by reference in a distributed system requires distributed garbage collection and (potentially) a lot of network round-trips. This not only complicates the design of the system but also imposes restrictions on some possible system architectures and interaction patterns. For example, how can you do distributed garbage collection when the requestor and the provider of a service can both be offline at the same time?

Therefore, for Web services, the notion of inout and out parameters doesn't involve passing objects by reference and letting the target backend modify their state; instead, copies of the data are exchanged. It's then up to the service client code to create the perception that the state of the object that has been passed in to the client method has been modified. We'll show you what we mean. Let's take the modified doCheck() you saw earlier:

  boolean doCheck(String SKU,
          inout int quantity)

When this method is called, the request message looks like this on the wire:

<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <soapenv:Body>
 <doCheck soapenv:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
  <SKU>318-BP</SKU>
  <quantity xsi:type="xsd:int">3</quantity>
 </doCheck>
 </soapenv:Body>
</soapenv:Envelope>

And here's the response message:

<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"
         xmlns:xsd="http://www.w3.org/2001/XMLSchema"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <soapenv:Body>
 <doCheckResponse soapenv:encodingStyle=
 "http://www.w3.org/2003/05/soap-encoding">
  <rpc:result xmlns:rpc="http://www.w3.org/2003/05/soap-rpc">return</rpc:result>
  <return>true</return>
  <quantity xsi:type="xsd:int">72</quantity>
 </doCheckResponse>
 </soapenv:Body>
</soapenv:Envelope>

This is a request to see if 3 items are available, and the response indicates that not only are there 3 (the true response), but there are in fact 72 (the new quantity value). The endpoint receiving this response should then update the appropriate programming-language construct for the quantity parameter with the new value.

Finally, here's the example that adds an extra out parameter containing the number of items in stock to our doCheck() method:

boolean doCheck(in sku, in quantity, out numInStock)

If we called this new method, the request would look identical to the one you saw earlier in this chapter, but the response would now look like this:

<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"
         xmlns:xsd="http://www.w3.org/2001/XMLSchema"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <soapenv:Body>
 <doCheckResponse soapenv:encodingStyle=
 "http://www.w3.org/2003/05/soap-encoding">
  <rpc:result xmlns:rpc="http://www.w3.org/2003/05/soap-rpc">return</rpc:result>
  <return>true</return>
  <numInStock>72</numInStock>
 </doCheckResponse>
 </soapenv:Body>
</soapenv:Envelope>

Despite the fact that Java doesn't have a native concept of inout and out parameters, we can still use them with toolkits like Axis—we'll explore how to do this in Chapter 5.

  • + Share This
  • 🔖 Save To Your Account