geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jb...@apache.org
Subject svn commit: r823221 - in /geronimo/sandbox/jbohn/daytrader-bp: assemblies/web/daytrader-web-jdbc/src/main/webapp/ modules/core/src/main/java/org/apache/geronimo/samples/daytrader/core/
Date Thu, 08 Oct 2009 16:22:49 GMT
Author: jbohn
Date: Thu Oct  8 16:22:49 2009
New Revision: 823221

URL: http://svn.apache.org/viewvc?rev=823221&view=rev
Log:
include the MarketSummary cache function that was previously in TradeAction and now lives
in TradeServicesManagerImpl

Modified:
    geronimo/sandbox/jbohn/daytrader-bp/assemblies/web/daytrader-web-jdbc/src/main/webapp/marketSummary.jsp
    geronimo/sandbox/jbohn/daytrader-bp/modules/core/src/main/java/org/apache/geronimo/samples/daytrader/core/TradeServicesManager.java
    geronimo/sandbox/jbohn/daytrader-bp/modules/core/src/main/java/org/apache/geronimo/samples/daytrader/core/TradeServicesManagerImpl.java

Modified: geronimo/sandbox/jbohn/daytrader-bp/assemblies/web/daytrader-web-jdbc/src/main/webapp/marketSummary.jsp
URL: http://svn.apache.org/viewvc/geronimo/sandbox/jbohn/daytrader-bp/assemblies/web/daytrader-web-jdbc/src/main/webapp/marketSummary.jsp?rev=823221&r1=823220&r2=823221&view=diff
==============================================================================
--- geronimo/sandbox/jbohn/daytrader-bp/assemblies/web/daytrader-web-jdbc/src/main/webapp/marketSummary.jsp
(original)
+++ geronimo/sandbox/jbohn/daytrader-bp/assemblies/web/daytrader-web-jdbc/src/main/webapp/marketSummary.jsp
Thu Oct  8 16:22:49 2009
@@ -24,9 +24,8 @@
 if (tradeServicesManager == null) {
     tradeServicesManager = TradeServiceUtilities.getTradeServicesManager();
 }
-TradeServices tradeServices = tradeServicesManager.getTradeServices();
 
-MarketSummaryDataBean marketSummaryData = tradeServices.getMarketSummary();
+MarketSummaryDataBean marketSummaryData = tradeServicesManager.getMarketSummary();
 %>
 <TABLE border="1" bgcolor="#ffffff" width="100%"
 	style="font-size: smaller">

Modified: geronimo/sandbox/jbohn/daytrader-bp/modules/core/src/main/java/org/apache/geronimo/samples/daytrader/core/TradeServicesManager.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/jbohn/daytrader-bp/modules/core/src/main/java/org/apache/geronimo/samples/daytrader/core/TradeServicesManager.java?rev=823221&r1=823220&r2=823221&view=diff
==============================================================================
--- geronimo/sandbox/jbohn/daytrader-bp/modules/core/src/main/java/org/apache/geronimo/samples/daytrader/core/TradeServicesManager.java
(original)
+++ geronimo/sandbox/jbohn/daytrader-bp/modules/core/src/main/java/org/apache/geronimo/samples/daytrader/core/TradeServicesManager.java
Thu Oct  8 16:22:49 2009
@@ -48,9 +48,22 @@
     /**
       * Get the currently selected TradeDBServices
       */
-
     public TradeDBServices getTradeDBServices();
 
+    /**
+      * Compute and return a snapshot of the current market
+      * conditions.  This includes the TSIA - and index of the prive
+      * of the top 100 Trade stock quotes.  Ths openTSIA(the index
+      * at the open), The volune of shares traded, Top Stocks gain
+      * and loss.  Get the currently selected TradeDBServices
+      * 
+      * This is a special version of this function which will cache
+      * the results provided by TradeServices.
+      * 
+      * @return A snapshot of the current market summary
+      */
+    public MarketSummaryDataBean getMarketSummary() throws Exception;
+
 
 }   
 

Modified: geronimo/sandbox/jbohn/daytrader-bp/modules/core/src/main/java/org/apache/geronimo/samples/daytrader/core/TradeServicesManagerImpl.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/jbohn/daytrader-bp/modules/core/src/main/java/org/apache/geronimo/samples/daytrader/core/TradeServicesManagerImpl.java?rev=823221&r1=823220&r2=823221&view=diff
==============================================================================
--- geronimo/sandbox/jbohn/daytrader-bp/modules/core/src/main/java/org/apache/geronimo/samples/daytrader/core/TradeServicesManagerImpl.java
(original)
+++ geronimo/sandbox/jbohn/daytrader-bp/modules/core/src/main/java/org/apache/geronimo/samples/daytrader/core/TradeServicesManagerImpl.java
Thu Oct  8 16:22:49 2009
@@ -20,6 +20,7 @@
 import java.util.Iterator;
 import java.util.Map;
 
+import org.apache.geronimo.samples.daytrader.util.Log;
 import org.apache.geronimo.samples.daytrader.util.TradeConfig;
 import org.apache.geronimo.samples.daytrader.core.TradeServices;
 import org.apache.geronimo.samples.daytrader.core.TradeDBServices;
@@ -44,17 +45,16 @@
 
 public class TradeServicesManagerImpl implements TradeServicesManager {
 
-//    private Set tradeServicesSet = new HashSet();
     private Map<Integer, TradeServices> tradeServicesMap = new HashMap<Integer,
TradeServices>();
-//    private Set tradeDBServicesSet = new HashSet();
     private Map<Integer, TradeDBServices> tradeDBServicesMap = new HashMap<Integer,
TradeDBServices>();
-    private TradeServices tradeServices = null;
-    private TradeDBServices tradeDBServices = null;
-    private TradeServices tradeServicesJPA = null;
-    private TradeDBServices tradeDBServicesJPA = null;
     private int currentMode = TradeConfig.JDBC;
     private String test;
 
+    // This lock is used to serialize market summary operations.
+    private static final Integer marketSummaryLock = new Integer(0);
+    private static long nextMarketSummary = System.currentTimeMillis();
+    private static MarketSummaryDataBean cachedMSDB = MarketSummaryDataBean.getRandomInstance();
+    
     /**
       * TradeServicesManagerImpl null constructor
       */
@@ -85,6 +85,7 @@
       */
     public void setTradeServices(TradeServices tradeServices)
     {
+//        System.out.println("@JAB TradeServicesManagerImpl.setTradeServices current size
" + tradeServicesMap.size() + " adding ");
 //        System.out.println("@JAB TradeServicesManagerImpl.setTradeServices current size
" + tradeServicesMap.size() + " adding " + tradeServices);
         tradeServicesMap.put(tradeServices.getMode(), tradeServices);
 //        System.out.println("@JAB TradeServicesManagerImpl.setTradeServices new size " +
tradeServicesMap.size());
@@ -185,4 +186,83 @@
         }
 //        System.out.println("@JAB TradeServicesManagerImpl.unbindDBService new size " +
tradeDBServicesMap.size());
     }
+
+    /**
+     * Market Summary is inherently a heavy database operation.  For servers that have a
caching
+     * story this is a great place to cache data that is good for a period of time.  In order
to
+     * provide a flexible framework for this we allow the market summary operation to be
+     * invoked on every transaction, time delayed or never.  This is configurable in the

+     * configuration panel.  
+     *
+     * @return An instance of the market summary
+     */
+    public MarketSummaryDataBean getMarketSummary() throws Exception {
+    
+        if (Log.doActionTrace()) {
+            Log.trace("TradeAction:getMarketSummary()");
+        }
+    
+        if (TradeConfig.getMarketSummaryInterval() == 0) return getMarketSummaryInternal();
+        if (TradeConfig.getMarketSummaryInterval() < 0) return cachedMSDB;
+    
+        /**
+         * This is a little funky.  If its time to fetch a new Market summary then we'll
synchronize
+         * access to make sure only one requester does it.  Others will merely return the
old copy until
+         * the new MarketSummary has been executed.
+         */
+         long currentTime = System.currentTimeMillis();
+         
+         if (currentTime > nextMarketSummary) {
+             long oldNextMarketSummary = nextMarketSummary;
+             boolean fetch = false;
+
+             synchronized (marketSummaryLock) {
+                 /**
+                  * Is it still ahead or did we miss lose the race?  If we lost then let's
get out
+                  * of here as the work has already been done.
+                  */
+                 if (oldNextMarketSummary == nextMarketSummary) {
+                     fetch = true;
+                     nextMarketSummary += TradeConfig.getMarketSummaryInterval()*1000;
+                     
+                     /** 
+                      * If the server has been idle for a while then its possible that nextMarketSummary
+                      * could be way off.  Rather than try and play catch up we'll simply
get in sync with the 
+                      * current time + the interval.
+                      */ 
+                     if (nextMarketSummary < currentTime) {
+                         nextMarketSummary = currentTime + TradeConfig.getMarketSummaryInterval()*1000;
+                     }
+                 }
+             }
+
+            /**
+             * If we're the lucky one then let's update the MarketSummary
+             */
+            if (fetch) {
+                cachedMSDB = getMarketSummaryInternal();
+            }
+        }
+         
+        return cachedMSDB;
+    }
+
+    /**
+     * Compute and return a snapshot of the current market conditions This
+     * includes the TSIA - an index of the price of the top 100 Trade stock
+     * quotes The openTSIA ( the index at the open) The volume of shares traded,
+     * Top Stocks gain and loss
+     *
+     * @return A snapshot of the current market summary
+     */
+    private MarketSummaryDataBean getMarketSummaryInternal() throws Exception {
+        if (Log.doActionTrace()) {
+            Log.trace("TradeAction:getMarketSummaryInternal()");
+        }
+        MarketSummaryDataBean marketSummaryData = null;
+        marketSummaryData = tradeServicesMap.get(currentMode).getMarketSummary();
+        return marketSummaryData;
+    }
+
+
 }



Mime
View raw message