From 3ec9ac7605408a7c6e16af8940e104405a826beb Mon Sep 17 00:00:00 2001 From: nash Date: Sun, 26 Sep 2010 21:35:52 +0000 Subject: 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 --- .../src/main/java/bigbank/AccountServiceImpl.java | 55 ++++++++++++++++++++-- .../src/main/java/bigbank/ExchangeRateImpl.java | 14 +++++- 2 files changed, 64 insertions(+), 5 deletions(-) (limited to 'sca-java-1.x/branches/sca-java-1.6.1/demos/xml-bigbank/src/main/java') 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( + ""+ + ""+ + "IBM"+ + "134.11"+ + "9/24/2010"+ + ""+ + "+2.44"+ + "132.42"+ + "134.15"+ + "132.34"+ + "7122325"+ + "169.1B"+ + "131.67"+ + "+1.85%"+ + "116.00 - 134.25"+ + "10.582"+ + "12.44"+ + "International Bus"+ + ""+ + "")); + } - 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(); -- cgit v1.2.3