|
Identifying and Eliminating Web Services Transaction, by Helen Thomas
WSJ Vol 02 Issue 09 - pg.14
Listing 1
public class getQuote {
public static void main (String[] args) throws Exception {
// Process the arguments.
int offset = 3 - args.length;
String encodingStyleURI = args.length == 3
? args[0].substring(1)
: Constants.NS_URI_SOAP_ENC;
URL url = new URL (args[1 - offset]);
String symbol = args[2 - offset];
// Build the call.
Call call = new Call ();
call.setTargetObjectURI ("urn:xmltoday-delayed-quotes");
call.setMethodName ("getQuote");
call.setEncodingStyleURI(encodingStyleURI);
Vector params = new Vector ();
params.addElement (new Parameter("symbol",
String.class, symbol, null));
call.setParams (params);
// Make the call.
Response resp = call.invoke (/* router URL */ url, /*
actionURI */ "" );
// Check the response.
if (resp.generatedFault ()) {
Fault fault = resp.getFault ();
System.out.println ("Ouch, the call failed: ");
} else {
Parameter result = resp.getReturnValue ();
System.out.println (result.getValue ());
}
}
}
Listing 2
public synchronized void addObject(Object key, Object value)
{
if (key != null && value != null)
{
cache.put(key, value);
}
}
public Object fetchObject(Object key)
{
if (key == null)
{
return null;
}
return cache.get(key);
}
Listing 3
public StockQuote getQuote(String symbol)
{
StockQuote stockQuote = null;
if(symbol != null)
{
stockQuote = (StockQuote) cache.fetchObject(symbol);
if(stockQuote == null)
{
synchronized(cache)
{
stockQuote = (StockQuote) cache.fetchObject(symbol);
if(stockQuote != null)
{
return(stockQuote);
}
Vector params = new Vector();
params.addElement(new Parameter("symbol",
String.class, symbol, null));
stockQuote = (StockQuote) soap.invoke("getQuote",
params);
if(stockQuote != null)
{
cache.addObject(stockQuote.getSymbol(), stockQuote);
}
}
}
}
return stockQuote;
}
|