geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hogst...@apache.org
Subject svn commit: r583341 - in /geronimo/daytrader/trunk/modules/ejb/src/main/java/org/apache/geronimo/samples/daytrader: TradeAction.java direct/TradeDirect.java
Date Wed, 10 Oct 2007 01:50:07 GMT
Author: hogstrom
Date: Tue Oct  9 18:50:06 2007
New Revision: 583341

URL: http://svn.apache.org/viewvc?rev=583341&view=rev
Log:
Corrected MarketSummary Caching.  I had originally put the cache in TradeDirect for testing.
 It needed to be moved to TradeAction so all runtime modes would benefit.

Modified:
    geronimo/daytrader/trunk/modules/ejb/src/main/java/org/apache/geronimo/samples/daytrader/TradeAction.java
    geronimo/daytrader/trunk/modules/ejb/src/main/java/org/apache/geronimo/samples/daytrader/direct/TradeDirect.java

Modified: geronimo/daytrader/trunk/modules/ejb/src/main/java/org/apache/geronimo/samples/daytrader/TradeAction.java
URL: http://svn.apache.org/viewvc/geronimo/daytrader/trunk/modules/ejb/src/main/java/org/apache/geronimo/samples/daytrader/TradeAction.java?rev=583341&r1=583340&r2=583341&view=diff
==============================================================================
--- geronimo/daytrader/trunk/modules/ejb/src/main/java/org/apache/geronimo/samples/daytrader/TradeAction.java
(original)
+++ geronimo/daytrader/trunk/modules/ejb/src/main/java/org/apache/geronimo/samples/daytrader/TradeAction.java
Tue Oct  9 18:50:06 2007
@@ -36,6 +36,11 @@
  * making calls to TradeAction methods to actually performance each operation.
  */
 public class TradeAction implements TradeServices {
+    // 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();
+    
     // make this static so the trade impl can be cached
     // - ejb3 mode is the only thing that really uses this
     // - can go back and update other modes to take advantage (ie. TradeDirect)
@@ -102,6 +107,67 @@
         }
     }
 
+
+    /**
+     * 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
@@ -110,9 +176,9 @@
      *
      * @return A snapshot of the current market summary
      */
-    public MarketSummaryDataBean getMarketSummary() throws Exception {
+    public MarketSummaryDataBean getMarketSummaryInternal() throws Exception {
         if (Log.doActionTrace()) {
-            Log.trace("TradeAction:getMarketSummary()");
+            Log.trace("TradeAction:getMarketSummaryInternal()");
         }
         MarketSummaryDataBean marketSummaryData = null;
         marketSummaryData = trade.getMarketSummary();

Modified: geronimo/daytrader/trunk/modules/ejb/src/main/java/org/apache/geronimo/samples/daytrader/direct/TradeDirect.java
URL: http://svn.apache.org/viewvc/geronimo/daytrader/trunk/modules/ejb/src/main/java/org/apache/geronimo/samples/daytrader/direct/TradeDirect.java?rev=583341&r1=583340&r2=583341&view=diff
==============================================================================
--- geronimo/daytrader/trunk/modules/ejb/src/main/java/org/apache/geronimo/samples/daytrader/direct/TradeDirect.java
(original)
+++ geronimo/daytrader/trunk/modules/ejb/src/main/java/org/apache/geronimo/samples/daytrader/direct/TradeDirect.java
Tue Oct  9 18:50:06 2007
@@ -59,11 +59,6 @@
 public class TradeDirect implements TradeServices
 
 {
-	// This lock is used to serialize market summary operations.
-	private static Integer marketSummaryLock = new Integer(0);
-	private static long nextMarketSummary = 0;
-	private static MarketSummaryDataBean cachedMSDB = MarketSummaryDataBean.getRandomInstance();
-
 	private static String dsName = TradeConfig.DATASOURCE;
 
 	private static DataSource datasource = null;
@@ -89,42 +84,10 @@
 		this.inSession = inSession;
 	}
 
-	/**
+    /**
      * @see TradeServices#getMarketSummary()
      */
 	public MarketSummaryDataBean getMarketSummary() throws Exception {
-		boolean fetch = false;
-		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.
-		 */
-		if (System.currentTimeMillis() > nextMarketSummary) {
-			synchronized (marketSummaryLock) {
-				if (System.currentTimeMillis() > nextMarketSummary)  fetch = true;
-				if (nextMarketSummary == 0) nextMarketSummary = System.currentTimeMillis();
-				nextMarketSummary += TradeConfig.getMarketSummaryInterval()*1000;
-			}
-
-			/**
-			 * If we're the lucky one reset the trap and get a new MarketSummaryObject.
-			 */
-			if (fetch) {
-				fetch = false;
-				cachedMSDB = getMarketSummaryInternal();
-			}
-		}
-		return cachedMSDB;
-	}
-
-
-	/**
-     * @see TradeServices#getMarketSummary()
-     */
-	public MarketSummaryDataBean getMarketSummaryInternal() throws Exception {
 
 		MarketSummaryDataBean marketSummaryData = null;
 		Connection conn = null;



Mime
View raw message