live.cache
Class CacheThread

java.lang.Object
  extended byjava.lang.Thread
      extended bylive.cache.CacheThread
All Implemented Interfaces:
EventListener, Runnable, ServletContextListener

public class CacheThread
extends Thread
implements ServletContextListener

This class starts a background thread on the server that keeps the cache updated. The cache is accessable from the live.constants.Attributes.servletContext.cache attribute of the ServletContext, and this thread is accessable from the Attributes.servletContext.cacheThread attribute. This object needs to be accessable so that the user can trigger cache updates from a refresh button.

Author:
Evan Driscoll

Field Summary
private  ServletContext context
           
private  boolean continueRunning
          Tells whether the thread should be running
(package private)  Exception exception
           
private static long HOURS
          The number of milliseconds in an hour
private static long MINUTES
          The number of milliseconds in a minute
private  Date nextUpdate
          The time the nextUpdate is scheduled for
private static long SECONDS
          The number of milliseconds in a second
static long SLEEP_TIME_MS
          This is how long the thread sleeps before waking up and checking if it is supposed to do an update.
static long UPDATE_DELTA_MS
          This is the number of milliseconds before the cache will automatically update.
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
CacheThread()
           
 
Method Summary
 void contextDestroyed(ServletContextEvent event)
          Called automatically when the server ends.
 void contextInitialized(ServletContextEvent event)
          Called automatically when the server starts.
private  void doUpdateCache()
          This function actually carries out the cache update.
 Exception getError()
          Returns the exception thrown
 void run()
          This function enters an infinite loop that checks for new updates for the server ever so ofter.
 void scheduleUpdate()
          This function updates the cache.
 void scheduleUpdate(Date nextUpdate)
          This function schedules the next cache update for the specified parameter.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getContextClassLoader, getName, getPriority, getThreadGroup, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setName, setPriority, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

SECONDS

private static final long SECONDS
The number of milliseconds in a second

See Also:
Constant Field Values

MINUTES

private static final long MINUTES
The number of milliseconds in a minute

See Also:
Constant Field Values

HOURS

private static final long HOURS
The number of milliseconds in an hour

See Also:
Constant Field Values

UPDATE_DELTA_MS

public static final long UPDATE_DELTA_MS
This is the number of milliseconds before the cache will automatically update. TODO: Get this fnom a config file, and provide separate frequencies for the different updates.

See Also:
Constant Field Values

SLEEP_TIME_MS

public static final long SLEEP_TIME_MS
This is how long the thread sleeps before waking up and checking if it is supposed to do an update. Note that this is distinct from UPDATE_DELTA_MS, which is how often an update is supposed to happen. This should be low so that if the user presses refresh he or she sees an update shortly after, because it could potentially be SLEEP_TIME_MS milliseconds from when they press the button to when the update even starts.

See Also:
Constant Field Values

context

private ServletContext context

nextUpdate

private Date nextUpdate
The time the nextUpdate is scheduled for


continueRunning

private boolean continueRunning
Tells whether the thread should be running


exception

Exception exception
Constructor Detail

CacheThread

public CacheThread()
Method Detail

run

public void run()
This function enters an infinite loop that checks for new updates for the server ever so ofter. TODO: A possible idea for future expansion (not likely during our time here) is to provide a more flexible method of scheduling updates so you could do, for example, "update every 5 minutes from 9am to 5pm, 10 minutes from 7am to 9am and 5pm to 8pm, and 20 minutes from 8pm to 7am", or even more complex functions, through the use of policy classes. This would be a fairly large chunk of coding for relatively little benefit, so it's low on the priority ladder.

Specified by:
run in interface Runnable

contextInitialized

public void contextInitialized(ServletContextEvent event)
Called automatically when the server starts. Simply stores the context and starts the update thread

Specified by:
contextInitialized in interface ServletContextListener
Parameters:
event -

contextDestroyed

public void contextDestroyed(ServletContextEvent event)
Called automatically when the server ends. We tell the thread to die. At least soon.

Specified by:
contextDestroyed in interface ServletContextListener
Parameters:
event -

scheduleUpdate

public void scheduleUpdate()
This function updates the cache. More precisely (and accurately), it schedules an update for now, so that when the server wakes up again in something less than SLEEP_TIME_MS, it will update.


scheduleUpdate

public void scheduleUpdate(Date nextUpdate)
This function schedules the next cache update for the specified parameter. This is true even if nextUpdate is after the currently scheduled update time.

Parameters:
nextUpdate - The time to start the next cache update, within SLEEP_TIME_MS

getError

public Exception getError()
Returns the exception thrown

Returns:
The exception

doUpdateCache

private void doUpdateCache()
This function actually carries out the cache update. It makes a new cache object, updates the cache, then sets the cache object in application scope. The reasoning behind creating a new cache object instead of getting the current one is that it likely helps to reduce concurrency issues. Rather than have the cache worry about what happens if someone querys it while it's updating, we simply use a different object. (I'm not saying the cache is free from concurrency issues because of this, just that they are a bit less of a problem.) TODO: Fix the getting of the server parameters



Copyright © 2005