* Extract session cleanup and cron job invoker code from run()
into separate cleanupSessions() and executeCronJobs() methods * Add some missing JavaDoc tags
This commit is contained in:
parent
42a088ef58
commit
b316462c69
1 changed files with 159 additions and 133 deletions
|
@ -478,7 +478,7 @@ public final class Application implements IPathElement, Runnable {
|
||||||
/**
|
/**
|
||||||
* Returns true if this app is currently running
|
* Returns true if this app is currently running
|
||||||
*
|
*
|
||||||
* @return
|
* @return true if the app is running
|
||||||
*/
|
*/
|
||||||
public synchronized boolean isRunning() {
|
public synchronized boolean isRunning() {
|
||||||
return running;
|
return running;
|
||||||
|
@ -487,7 +487,7 @@ public final class Application implements IPathElement, Runnable {
|
||||||
/**
|
/**
|
||||||
* Get the application directory.
|
* Get the application directory.
|
||||||
*
|
*
|
||||||
* @return
|
* @return the application directory, or first file based repository
|
||||||
*/
|
*/
|
||||||
public File getAppDir() {
|
public File getAppDir() {
|
||||||
return appDir;
|
return appDir;
|
||||||
|
@ -497,7 +497,7 @@ public final class Application implements IPathElement, Runnable {
|
||||||
* Get a comparator for comparing Resources according to the order of
|
* Get a comparator for comparing Resources according to the order of
|
||||||
* repositories they're contained in.
|
* repositories they're contained in.
|
||||||
*
|
*
|
||||||
* @return
|
* @return a comparator that sorts resources according to their repositories
|
||||||
*/
|
*/
|
||||||
public ResourceComparator getResourceComparator() {
|
public ResourceComparator getResourceComparator() {
|
||||||
return resourceComparator;
|
return resourceComparator;
|
||||||
|
@ -1406,8 +1406,9 @@ public final class Application implements IPathElement, Runnable {
|
||||||
public void run() {
|
public void run() {
|
||||||
|
|
||||||
// as first thing, invoke global onStart() function
|
// as first thing, invoke global onStart() function
|
||||||
RequestEvaluator eval = getEvaluator();
|
RequestEvaluator eval = null;
|
||||||
try {
|
try {
|
||||||
|
eval = getEvaluator();
|
||||||
eval.invokeInternal(null, "onStart", new Object[0]);
|
eval.invokeInternal(null, "onStart", new Object[0]);
|
||||||
} catch (Exception xcept) {
|
} catch (Exception xcept) {
|
||||||
logEvent("Error in " + name + "/onStart(): " + xcept);
|
logEvent("Error in " + name + "/onStart(): " + xcept);
|
||||||
|
@ -1416,24 +1417,73 @@ public final class Application implements IPathElement, Runnable {
|
||||||
}
|
}
|
||||||
|
|
||||||
// interval between session cleanups
|
// interval between session cleanups
|
||||||
long sessionCleanupInterval = 60000;
|
|
||||||
long lastSessionCleanup = System.currentTimeMillis();
|
long lastSessionCleanup = System.currentTimeMillis();
|
||||||
|
|
||||||
// logEvent ("Starting scheduler for "+name);
|
// logEvent ("Starting scheduler for "+name);
|
||||||
|
|
||||||
// loop-local cron job data
|
|
||||||
List cronJobs = null;
|
|
||||||
long lastCronParse = 0;
|
|
||||||
|
|
||||||
while (Thread.currentThread() == worker) {
|
while (Thread.currentThread() == worker) {
|
||||||
|
|
||||||
|
// purge sessions
|
||||||
|
try {
|
||||||
|
lastSessionCleanup = cleanupSessions(lastSessionCleanup);
|
||||||
|
} catch (Exception x) {
|
||||||
|
logError("Error in session cleanup", x);
|
||||||
|
}
|
||||||
|
|
||||||
|
// execute cron jobs
|
||||||
|
try {
|
||||||
|
executeCronJobs();
|
||||||
|
} catch (Exception x) {
|
||||||
|
logError("Error in cron job execution", x);
|
||||||
|
}
|
||||||
|
|
||||||
|
long sleepInterval = 60000;
|
||||||
|
try {
|
||||||
|
String sleepProp = props.getProperty("schedulerInterval");
|
||||||
|
if (sleepProp != null) {
|
||||||
|
sleepInterval = Math.max(1000, Integer.parseInt(sleepProp) * 1000);
|
||||||
|
} else {
|
||||||
|
sleepInterval = CronJob.millisToNextFullMinute();
|
||||||
|
}
|
||||||
|
} catch (Exception ignore) {
|
||||||
|
// we'll use the default interval
|
||||||
|
}
|
||||||
|
|
||||||
|
// sleep until the next full minute
|
||||||
|
try {
|
||||||
|
Thread.sleep(sleepInterval);
|
||||||
|
} catch (InterruptedException x) {
|
||||||
|
worker = null;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// when interrupted, shutdown running cron jobs
|
||||||
|
synchronized (activeCronJobs) {
|
||||||
|
for (Iterator i = activeCronJobs.values().iterator(); i.hasNext();) {
|
||||||
|
((CronRunner) i.next()).interrupt();
|
||||||
|
i.remove();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
logEvent("Scheduler for " + name + " exiting");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Purge sessions that have not been used for a certain amount of time.
|
||||||
|
* This is called by run().
|
||||||
|
*
|
||||||
|
* @param lastSessionCleanup the last time sessions were purged
|
||||||
|
* @return the updated lastSessionCleanup value
|
||||||
|
*/
|
||||||
|
private long cleanupSessions(long lastSessionCleanup) {
|
||||||
|
|
||||||
long now = System.currentTimeMillis();
|
long now = System.currentTimeMillis();
|
||||||
|
long sessionCleanupInterval = 60000;
|
||||||
|
|
||||||
// check if we should clean up user sessions
|
// check if we should clean up user sessions
|
||||||
if ((now - lastSessionCleanup) > sessionCleanupInterval) {
|
if ((now - lastSessionCleanup) > sessionCleanupInterval) {
|
||||||
|
|
||||||
lastSessionCleanup = now;
|
|
||||||
|
|
||||||
// get session timeout
|
// get session timeout
|
||||||
int sessionTimeout = 30;
|
int sessionTimeout = 30;
|
||||||
|
|
||||||
|
@ -1441,7 +1491,8 @@ public final class Application implements IPathElement, Runnable {
|
||||||
sessionTimeout = Math.max(0,
|
sessionTimeout = Math.max(0,
|
||||||
Integer.parseInt(props.getProperty("sessionTimeout",
|
Integer.parseInt(props.getProperty("sessionTimeout",
|
||||||
"30")));
|
"30")));
|
||||||
} catch (Exception ignore) {}
|
} catch (Exception ignore) {
|
||||||
|
}
|
||||||
|
|
||||||
RequestEvaluator thisEvaluator = null;
|
RequestEvaluator thisEvaluator = null;
|
||||||
|
|
||||||
|
@ -1478,20 +1529,28 @@ public final class Application implements IPathElement, Runnable {
|
||||||
releaseEvaluator(thisEvaluator);
|
releaseEvaluator(thisEvaluator);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return now;
|
||||||
|
} else {
|
||||||
|
return lastSessionCleanup;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
/**
|
||||||
|
* Executes cron jobs for the application, which are either
|
||||||
|
* defined in app.properties or via app.addCronJob().
|
||||||
|
* This method is called by run().
|
||||||
|
*/
|
||||||
|
private void executeCronJobs() {
|
||||||
|
// loop-local cron job data
|
||||||
|
List cronJobs = null;
|
||||||
|
long lastCronParse = 0;
|
||||||
|
|
||||||
if ((cronJobs == null) || (props.lastModified() > lastCronParse)) {
|
if ((cronJobs == null) || (props.lastModified() > lastCronParse)) {
|
||||||
updateProperties();
|
updateProperties();
|
||||||
cronJobs = CronJob.parse(props);
|
cronJobs = CronJob.parse(props);
|
||||||
lastCronParse = props.lastModified();
|
lastCronParse = props.lastModified();
|
||||||
}
|
}
|
||||||
} catch (Exception ex) {
|
|
||||||
logEvent ("error parsing CronJobs: " + ex);
|
|
||||||
ex.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
Date date = new Date();
|
Date date = new Date();
|
||||||
List jobs = new ArrayList(cronJobs);
|
List jobs = new ArrayList(cronJobs);
|
||||||
|
|
||||||
|
@ -1549,39 +1608,6 @@ public final class Application implements IPathElement, Runnable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception ex) {
|
|
||||||
logEvent("error handling CronJobs: " + ex);
|
|
||||||
ex.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
long sleepInterval = CronJob.millisToNextFullMinute();
|
|
||||||
try {
|
|
||||||
String sleepProp = props.getProperty("schedulerInterval");
|
|
||||||
if (sleepProp != null) {
|
|
||||||
sleepInterval = Math.max(1000, Integer.parseInt(sleepProp)*1000);
|
|
||||||
}
|
|
||||||
} catch (Exception ignore) {
|
|
||||||
// we'll use the default interval
|
|
||||||
}
|
|
||||||
|
|
||||||
// sleep until the next full minute
|
|
||||||
try {
|
|
||||||
Thread.sleep(sleepInterval);
|
|
||||||
} catch (InterruptedException x) {
|
|
||||||
worker = null;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// when interrupted, shutdown running cron jobs
|
|
||||||
synchronized (activeCronJobs) {
|
|
||||||
for (Iterator i = activeCronJobs.values().iterator(); i.hasNext();) {
|
|
||||||
((CronRunner) i.next()).interrupt();
|
|
||||||
i.remove();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
logEvent("Scheduler for " + name + " exiting");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Reference in a new issue