[jboss-svn-commits] JBL Code SVN: r8233 - labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/gateway

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Mon Dec 11 13:45:38 EST 2006


Author: estebanschifman
Date: 2006-12-11 13:45:35 -0500 (Mon, 11 Dec 2006)
New Revision: 8233

Modified:
   labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/gateway/GatewayListenerController.java
Log:
Fix reload parameter problem when file timestamp did not change

Modified: labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/gateway/GatewayListenerController.java
===================================================================
--- labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/gateway/GatewayListenerController.java	2006-12-11 18:02:27 UTC (rev 8232)
+++ labs/jbossesb/trunk/product/core/listeners/src/org/jboss/soa/esb/listeners/gateway/GatewayListenerController.java	2006-12-11 18:45:35 UTC (rev 8233)
@@ -55,7 +55,7 @@
 
 public class GatewayListenerController implements Runnable
 {
-	private static Map<String,String> _paramFileTimeStamps=new ConcurrentHashMap<String,String>();
+	private static Map<String,Long> _paramFileTimeStamps=new ConcurrentHashMap<String,Long>();
 	private static String _sRldSecs=null;
 	
 	public static void main(String[] args) throws Exception 
@@ -193,32 +193,25 @@
 	 *             If attributes are wrong or not enough for a proper runtime
 	 *             configuration
 	 */
-	public void checkParms(ConfigTree p_oP) throws Exception {
+	public void checkParms(ConfigTree p_oP) throws Exception 
+	{
 		// We've just loaded - set to false until next reload requested
 		_reloadRequested = false;
+		if (null!=_sParametersName)
+		{
+			File file = new File(_sParametersName);
+			if (file.exists())
+				_paramFileTimeStamps.put(_sParametersName, file.lastModified());
+		}
+		
+		
 		_commandQueue = createCommandQueue(p_oP);
 
 		// Open the command queue...
 		if (null!=_commandQueue)
 			_commandQueue.open(p_oP);
 
-		// if RELOAD_SECONDS_TAG not set, and no command queue
-		// then reload every 10 minutes
-		// If there is a command queue, run until command is received
-		_sRldSecs = p_oP.getAttribute(RELOAD_SECONDS_TAG);
-		_nextReload = (null != _sRldSecs) 
-				? System.currentTimeMillis() + 1000 * Long.parseLong(_sRldSecs)
-				: (null == _commandQueue) 
-						? Long.MAX_VALUE 
-						: System.currentTimeMillis() + _defaultReloadMillis;
-	    if (null==_sRldSecs)
-	    {
-	    	String sMsg = (null==_commandQueue)
-	    		? " -  Using default of "+_sRldSecs
-	    		: " - Listener will run until stopped by command sent to queue";
-	    	_logger.warn("No value specified for: "+RELOAD_SECONDS_TAG + sMsg);
-	    }
-
+		setNextReloadTime(p_oP);
 		// if END_TIME_TAG not set try to run forever
 		// not a good practice if command queue is not set
 		// Expected date format is "yyyyMMdd hh:mm:ss"
@@ -233,7 +226,33 @@
 	    	_logger.warn("No value specified for: "+END_TIME_TAG + sMsg);
 	    }
 	} // ________________________________
+	
+	private void setNextReloadTime(ConfigTree tree)
+	{
+		// if RELOAD_SECONDS_TAG not set, and no command queue
+		// then reload every 10 minutes
+		// If there is a command queue, run until command is received
+		_sRldSecs = tree.getAttribute(RELOAD_SECONDS_TAG);
+		
+		synchronized (_synchReload) 
+		{
+			_nextReload = (null != _sRldSecs) 
+			? System.currentTimeMillis() + 1000 * Long.parseLong(_sRldSecs)
+			: (null == _commandQueue) 
+					? Long.MAX_VALUE 
+					: System.currentTimeMillis() + _defaultReloadMillis;
+		}
+		
+	    if (null==_sRldSecs)
+	    {
+	    	String sMsg = (null==_commandQueue)
+	    		? " -  Using default of "+_sRldSecs
+	    		: " - Listener will run until stopped by command sent to queue";
+	    	_logger.warn("No value specified for: "+RELOAD_SECONDS_TAG + sMsg);
+	    }
 
+	} //________________________________
+
     /**
      * Factory method for creating the command queue.
      * @param config GatewayListener config.
@@ -285,39 +304,21 @@
 			if (endRequested()) {
 				break;
 			}
-			if (_sParametersName != null && timeToReload()) 
-			{
-//				check the TS on the file.
-				boolean reloadNeeded=true;
-				File paramFile = new File(_sParametersName);
-				if (paramFile.exists()) {
-					String currentTimeStamp = String.valueOf(paramFile.lastModified());
-					String previousTimeStamp = _paramFileTimeStamps.get(_sParametersName);
-					_paramFileTimeStamps.put(_sParametersName, currentTimeStamp);
-					if (previousTimeStamp!=null && previousTimeStamp.equals(currentTimeStamp)) {
-						reloadNeeded=false;
-						_nextReload = (null != _sRldSecs) 
-						? System.currentTimeMillis() + 1000 * Long.parseLong(_sRldSecs)
-						: (null == _commandQueue) 
-								? Long.MAX_VALUE 
-								: System.currentTimeMillis() + _defaultReloadMillis;
-					}
+			if (_sParametersName != null && isReloadNeeded()) 
+				try 
+				{
+					_status = State.Loading_parameters;
+					_logger
+							.info("Reloading parameters _____________________________________________________");
+					ConfigTree oNew = GatewayListenerController.getListenerConfig(_sParametersName);
+					checkParms(oNew);
+					_config = oNew;
+				} 
+				catch (Exception e) 
+				{
+					_logger.error("Failed to reload parameters"
+							+ " - Continuing with cached version", e);
 				}
-				if (reloadNeeded) {
-					try 
-					{
-						_status = State.Loading_parameters;
-						_logger
-								.info("Reloading parameters _____________________________________________________");
-						ConfigTree oNew = GatewayListenerController.getListenerConfig(_sParametersName);
-						checkParms(oNew);
-						_config = oNew;
-					} catch (Exception e) {
-						_logger.error("Failed to reload parameters"
-								+ " - Continuing with cached version", e);
-					}
-				}
-			}
 		}
 		// _status = State.Shutting_down;
 
@@ -354,8 +355,12 @@
 		}
 	} // ________________________________
 
-	long millisToWait() {
-		return Math.min(_nextReload, _endTime) - System.currentTimeMillis();
+	long millisToWait()
+	{
+		synchronized (_synchReload)
+		{
+			return Math.min(_nextReload, _endTime) - System.currentTimeMillis();
+		}
 	} // ________________________________
 
 	private void waitForCmdOrSleep() {
@@ -386,7 +391,7 @@
 					return;
 				}
 				processCommand(oM);
-				if (endRequested() || timeToReload()) {
+				if (endRequested() || isReloadNeeded()) {
 					break;
 				}
 			} catch (CommandQueueException eJ) {
@@ -492,10 +497,42 @@
 	 * 
 	 * @return boolean - true if it's time to reload parameters
 	 */
-	public boolean timeToReload() {
-		return _reloadRequested
-				|| System.currentTimeMillis() >= _nextReload;
+	private boolean isReloadNeeded()
+	{
+		// command to reload was received - force reload
+		if (_reloadRequested)
+			return refreshNextReload();
+
+		// Still not time to reload
+		if (System.currentTimeMillis() < _nextReload)
+			return false;
+		
+		if (null==_sParametersName)
+			return refreshNextReload();
+		
+		File paramFile = new File(_sParametersName);
+		if (! paramFile.exists())
+			return refreshNextReload();
+		
+//		check the TS on the file.
+		Long previousTimeStamp	= _paramFileTimeStamps.get(_sParametersName);
+		if (null==previousTimeStamp)
+			return refreshNextReload();
+
+
+		Long currentTimeStamp	= paramFile.lastModified();
+		if (! previousTimeStamp.equals(currentTimeStamp))
+			return refreshNextReload();
+
+		setNextReloadTime(_config);
+		return false;
 	}
+	
+	private boolean refreshNextReload()
+	{
+		setNextReloadTime(_config);
+		return true;
+	}
 
 	/**
 	 * Helper accessor for child processes that provides info to determine if
@@ -505,7 +542,7 @@
 	 *         reload parameters
 	 */
 	public boolean continueLooping() {
-		return (endNotRequested() && !timeToReload());
+		return (endNotRequested() && !isReloadNeeded());
 	} // ________________________________
 
 	/**
@@ -637,6 +674,7 @@
 		= new SimpleDateFormat("yyyyMMdd hh:mm:ss");
 
 	private State _status = null;
+	private Object _synchReload = new Short((short)0);
 
 
 	private HashMap<String, Object> _attributes;




More information about the jboss-svn-commits mailing list