diff options
author | nash <nash@13f79535-47bb-0310-9956-ffa450edef68> | 2010-09-26 21:35:52 +0000 |
---|---|---|
committer | nash <nash@13f79535-47bb-0310-9956-ffa450edef68> | 2010-09-26 21:35:52 +0000 |
commit | 3ec9ac7605408a7c6e16af8940e104405a826beb (patch) | |
tree | 7a903c751c66eab5d11a02b7eedcc15be6fd0d1f /sca-java-1.x/branches/sca-java-1.6.1/demos/xml-bigbank/src/main/java | |
parent | f394e0b2cf9ff798ca295a98314b3b39c7e09267 (diff) |
TUSCANY-3690: Handle unavailability of live RSS feed or web service by using historical data
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1001526 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sca-java-1.x/branches/sca-java-1.6.1/demos/xml-bigbank/src/main/java')
2 files changed, 64 insertions, 5 deletions
diff --git a/sca-java-1.x/branches/sca-java-1.6.1/demos/xml-bigbank/src/main/java/bigbank/AccountServiceImpl.java b/sca-java-1.x/branches/sca-java-1.6.1/demos/xml-bigbank/src/main/java/bigbank/AccountServiceImpl.java index 00fce39462..46995de393 100644 --- a/sca-java-1.x/branches/sca-java-1.6.1/demos/xml-bigbank/src/main/java/bigbank/AccountServiceImpl.java +++ b/sca-java-1.x/branches/sca-java-1.6.1/demos/xml-bigbank/src/main/java/bigbank/AccountServiceImpl.java @@ -25,6 +25,9 @@ import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamReader; import org.apache.axiom.om.OMElement; +import org.apache.log4j.Level; +import org.apache.log4j.LogManager; +import org.apache.log4j.spi.LoggerRepository; import org.osoa.sca.ServiceRuntimeException; import org.osoa.sca.annotations.Property; import org.osoa.sca.annotations.Reference; @@ -65,11 +68,55 @@ public class AccountServiceImpl implements AccountService { System.out.println("Getting stock quote..."); XMLStreamReader request = factory.createXMLStreamReader(new StringReader(STOCK_QUOTE_REQUEST)); - OMElement quotes = stockQuote.GetQuote(request); + // temporarily disable INFO logging before calling the web service + LoggerRepository repository = LogManager.getLoggerRepository(); + Level threshold = repository.getThreshold(); + repository.setThreshold(Level.WARN); + + // first try to get a live stock quote from the web service + String xml = null; + try { + OMElement quotes = stockQuote.GetQuote(request); + xml = quotes.getText(); + } catch (Exception e) { + + // restore the previous logging setting + } finally { + repository.setThreshold(threshold); + } + + // if the web service invocation was successful, process the response + XMLStreamReader qts = null; + if (xml != null && xml.startsWith("<")) { + System.out.println(xml); + qts = factory.createXMLStreamReader(new StringReader(xml)); + + // if the web service isn't responding, continue with the demo using historical data + } else { + System.out.println("Stock price live quote not available, using historical data"); + qts = factory.createXMLStreamReader(new StringReader( + "<StockQuotes>"+ + "<Stock>"+ + "<Symbol>IBM</Symbol>"+ + "<Last>134.11</Last>"+ + "<Date>9/24/2010</Date>"+ + "<Time>4:00pm</Time>"+ + "<Change>+2.44</Change>"+ + "<Open>132.42</Open>"+ + "<High>134.15</High>"+ + "<Low>132.34</Low>"+ + "<Volume>7122325</Volume>"+ + "<MktCap>169.1B</MktCap>"+ + "<PreviousClose>131.67</PreviousClose>"+ + "<PercentageChange>+1.85%</PercentageChange>"+ + "<AnnRange>116.00 - 134.25</AnnRange>"+ + "<Earns>10.582</Earns>"+ + "<P-E>12.44</P-E>"+ + "<Name>International Bus</Name>"+ + "</Stock>"+ + "</StockQuotes>")); + } - String xml = quotes.getText(); - System.out.println(xml); - XMLStreamReader qts = factory.createXMLStreamReader(new StringReader(xml)); System.out.println("Calculating total value..."); double value = stockValue.calculate(qts, accounts); diff --git a/sca-java-1.x/branches/sca-java-1.6.1/demos/xml-bigbank/src/main/java/bigbank/ExchangeRateImpl.java b/sca-java-1.x/branches/sca-java-1.6.1/demos/xml-bigbank/src/main/java/bigbank/ExchangeRateImpl.java index fcb3707cfe..11c300cd3c 100644 --- a/sca-java-1.x/branches/sca-java-1.6.1/demos/xml-bigbank/src/main/java/bigbank/ExchangeRateImpl.java +++ b/sca-java-1.x/branches/sca-java-1.6.1/demos/xml-bigbank/src/main/java/bigbank/ExchangeRateImpl.java @@ -65,7 +65,19 @@ public class ExchangeRateImpl { public double getExchangeRate(String currency) { try { System.out.println("Retrieving exchange rate..."); - SyndFeed feed = exchangeRate.getRates(); + + // first try to get a live exchange rate quote from the RSS feed + SyndFeed feed = null; + try { + feed = exchangeRate.getRates(); + + // if the RSS feed isn't responding, continue with the demo using historical data + } catch (Exception e) { + System.out.println("Exchange rate live quote not available, using historical data"); + return 0.74107; + } + + // extract the exchange rate from the feed data SyndEntry entry = (SyndEntry)feed.getEntries().get(0); String rateTable = entry.getDescription().getValue(); |