package flashcache; import java.io.*; import java.util.*; //import java.net.*; import com.gemstone.gemfire.cache.*; import com.gemstone.gemfire.distributed.*; /** * This class is an interactive command-line application that looks up * NASDAQ stock symbols and displays current quote information. A * cache loader, QuoteLoader, is used to read quotes from * quotes.nasdaq.com and cache them in the distributed GemFire cache * for quicker access. * *
* * Quotes are cached for 20 seconds. At that time the cached data is * invalidated by GemFire and, if the symbol is queried again, the * QuoteLoader will be invoked to read an updated quote from NASDAQ. * *
* * The program is run like this, assuming that you have followed the instructions for setting up your * environment. * *
* $ java flashcache.Quote * Enter symbol: ORCL * (QuoteLoader netSearching for ORCL) * (QuoteLoader querying nasdaq for ORCL) * ORCL: last sale=9.85 net change=-0.34 volume=26,526,419 * Enter next symbol: AMZN * (QuoteLoader netSearching for AMZN) * (QuoteLoader querying nasdaq for AMZN) * AMZN: last sale=36.37 net change=-0.19 volume=5,668,938 * Enter next symbol: * ** * The main prompts for a NASDAQ stock symbol. Enter a symbol, such * as SUNW, to request a quote. Enter no symbol to exit. The cached * quotes are distributed to other GemFire caches connected to the same * distributed system. Optimistic distribution is used, so there is no * locking or acknowledgment of updates, and quotes are distributed * asynchronously. * * @author GemStone Systems, Inc. * * @since 2.0 */ public class Quote { /** The GemFire region that caches quotes obtained from NASDAQ */ private Region quotes; public static void main(String args[]) throws Exception { for (int i = 0; i < args.length; i++) { System.out.println("Ignoring command line argument: " + args[i]); } Quote instance = new Quote(); instance.initializeDistSystem(System.getProperty("systemDirectory")); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); try { System.out.print("Enter symbol: "); String symbol = reader.readLine(); while (symbol != null && symbol.length() != 0) { System.out.println(instance.getQuote(symbol)); System.out.print("Enter next symbol: "); symbol = reader.readLine(); } } catch (IOException e) { System.out.println(e.getMessage()); } System.exit(0); } /** this method returns a cached quote */ public String getQuote(String symbol) throws Exception { String anObj = (String)quotes.get(symbol); return anObj; } /** this method initializes the distributed system */ public void initializeDistSystem(String sysDir) throws Exception { Properties dsProps = new java.util.Properties(); dsProps.put("log-file", "quote.log"); // use default properties to connect to the distributed system // and create the cache and region DistributedSystem distSystem = DistributedSystem.connect(dsProps); Cache cache = CacheFactory.create(distSystem); initializeCache(cache, null); } /** this method initializes the cache */ public void initializeCache(Cache cache, CacheListener aListener) throws Exception { // create attributes for the quotes region AttributesFactory af = new AttributesFactory(); af.setCacheLoader(new QuoteLoader()); af.setScope(Scope.DISTRIBUTED_NO_ACK); // its default setting af.setEntryTimeToLive(new ExpirationAttributes(20)); af.setStatisticsEnabled(true); af.setCacheListener(aListener); RegionAttributes regionAttr = af.create(); //create the region quotes = cache.createRegion("NASDAQ Quotes", regionAttr); } }