perfsonar-dev - perfsonar: r5404 - in branches/new-structure-with-base2/ps-mdm-base2/src/main/java/org/perfsonar/base2/service/scheduler: . singleThread
Subject: perfsonar development work
List archive
perfsonar: r5404 - in branches/new-structure-with-base2/ps-mdm-base2/src/main/java/org/perfsonar/base2/service/scheduler: . singleThread
Chronological Thread
- From:
- To:
- Subject: perfsonar: r5404 - in branches/new-structure-with-base2/ps-mdm-base2/src/main/java/org/perfsonar/base2/service/scheduler: . singleThread
- Date: Fri, 11 Dec 2009 06:32:43 -0500
Author: trzaszcz
Date: 2009-12-11 06:32:43 -0500 (Fri, 11 Dec 2009)
New Revision: 5404
Added:
branches/new-structure-with-base2/ps-mdm-base2/src/main/java/org/perfsonar/base2/service/scheduler/singleThread/
branches/new-structure-with-base2/ps-mdm-base2/src/main/java/org/perfsonar/base2/service/scheduler/singleThread/SchedulerTaskComparator.java
branches/new-structure-with-base2/ps-mdm-base2/src/main/java/org/perfsonar/base2/service/scheduler/singleThread/SingleThreadScheduler.java
Modified:
branches/new-structure-with-base2/ps-mdm-base2/src/main/java/org/perfsonar/base2/service/scheduler/SchedulerTask.java
Log:
SingleThreadScheduler added
Modified:
branches/new-structure-with-base2/ps-mdm-base2/src/main/java/org/perfsonar/base2/service/scheduler/SchedulerTask.java
===================================================================
---
branches/new-structure-with-base2/ps-mdm-base2/src/main/java/org/perfsonar/base2/service/scheduler/SchedulerTask.java
2009-12-10 11:43:47 UTC (rev 5403)
+++
branches/new-structure-with-base2/ps-mdm-base2/src/main/java/org/perfsonar/base2/service/scheduler/SchedulerTask.java
2009-12-11 11:32:43 UTC (rev 5404)
@@ -1,216 +1,226 @@
package org.perfsonar.base2.service.scheduler;
import org.apache.log4j.Logger;
+
/**
- * Scheduler Task is a wrapper for Scheduler Action. Scheduler runs tasks
- * with specific interval. If the interval is not specified, the defaul one
- * will be taken.
+ * Scheduler Task is a wrapper for Scheduler Action. Scheduler runs tasks
with
+ * specific interval. If the interval is not specified, the defaul one will
be
+ * taken.
*
* @author Maciej Glowiak
* @author Roman Lapacz
*/
public class SchedulerTask {
- protected static final Logger logger =
Logger.getLogger(SchedulerTask.class.getName());
-
- // --------------------------------------------- Variables
+ protected static final Logger logger =
Logger.getLogger(SchedulerTask.class
+ .getName());
+ // --------------------------------------------- Variables
- private long MIN_INTERVAL = 1; //seconds
+ private long MIN_INTERVAL = 1; // seconds
- private long interval = MIN_INTERVAL; //minimal interval (in seconds)
between actions
+ private long interval = MIN_INTERVAL; // minimal interval (in seconds)
+
// between actions
- private long timestamp = 0; // last timestamp when action was
performed; 0 - never
+ private long timestamp = 0; // last timestamp when action was
performed; 0 -
+ // never
- private SchedulerAction action;
-
- private Thread thread;
+ private SchedulerAction action;
+ private Thread thread;
- // --------------------------------------------------- Constructor
+ public SchedulerTask(long interval, final SchedulerAction action) {
+ // check whether interval isn't lower than minimal
+ if (interval < MIN_INTERVAL) {
+ logger.debug("Requested interval for action [" +
action.getClass()
+ + "] too low. Taking [" +
MIN_INTERVAL + "] seconds");
+ this.interval = MIN_INTERVAL;
+ } else {
+ logger.debug("Interval for action [" +
action.getClass()
+ + "] set to [" + interval + "]
seconds");
+ this.interval = interval;
+ }
+ this.action = action;
+ this.timestamp = 0;
- public SchedulerTask(long interval, final SchedulerAction action) {
+ thread = createThread();
+ }
- //check whether interval isn't lower than minimal
- if (interval<MIN_INTERVAL) {
- logger.debug("Requested interval for action
["+action.getClass()+"] too low. Taking ["+MIN_INTERVAL+"] seconds");
- this.interval = MIN_INTERVAL;
- } else {
- logger.debug("Interval for action ["+action.getClass()+"] set to
["+interval+"] seconds");
- this.interval = interval;
- }
- this.action = action;
- this.timestamp = 0;
-
- thread=getThread();
- }
-
- private Thread getThread(){
- return new Thread() {
- @Override
- public void run() {
-// super.run();
- action.runAction();
- }
- };
- }
+ private Thread createThread() {
+ return new Thread() {
+ @Override
+ public void run() {
+ action.runAction();
+ }
+ };
+ }
- /**
- * This constructor is used, when some action should be run in the future
- *
- * @param interval interval for running action in seconds
- * @param action action to run
- * @param startTimestamp when should we start running action
- */
- public SchedulerTask(
- long interval, final SchedulerAction action, long startTimestamp) {
+ /**
+ * This constructor is used, when some action should be run in the
future
+ *
+ * @param interval
+ * interval for running action in seconds
+ * @param action
+ * action to run
+ * @param startTimestamp
+ * when should we start running action
+ */
+ public SchedulerTask(long interval, final SchedulerAction action,
+ long startTimestamp) {
+ this(interval, action); // interval in seconds
+ timestamp = startTimestamp - (interval * 1000);
+ }
+
+ /**
+ * checks whether action can start
+ *
+ * @return
+ */
+ public boolean canRun() {
+ long diff= (timestamp + (interval * 1000) -
System.currentTimeMillis());
+ System.out.println("DIFF "+diff);
+ return diff<= 0;
+ }
- this(interval, action); //interval in seconds
+ /**
+ * Runs proper action as new Thread (only if time is out)
+ */
+ public boolean runAction() {
- timestamp = startTimestamp - (interval*1000);
-
- }
+ logger.debug("Scheduler Task [" + this.getClass() + "] -
interval ["
+ + interval + "] seconds");
+ // if timestamp older than current - interval
- // ------------------------------------------------ Methods
+ logger.debug("Scheduler run action [" + action.getClass() +
"]");
+ timestamp = System.currentTimeMillis();
- /**
- * Runs proper action as new Thread (only if time is out)
- */
- public boolean runAction() {
+ // TODO: to be changed - should be thread pool?
+ // run action in thread
- long currentTimestamp = System.currentTimeMillis();
-
- logger.debug("Scheduler Task ["+this.getClass()+"] - interval
["+interval+"] seconds" );
+ if (thread.isAlive()) {
+ // do nothing
+ logger.debug("last action is already running");
+ } else {
+ thread = createThread();
+ thread.start();
+ }
- //if timestamp older than current - interval
- if ((timestamp + (interval*1000) - currentTimestamp) <= 0) {
+ // if action is being performed return true
+ return true;
- logger.debug("Scheduler run action ["+action.getClass()+"]" );
+ }
- timestamp = currentTimestamp;
+ /**
+ * get encapsulated SchedulerAction
+ *
+ * @return
+ */
+ public SchedulerAction getAction() {
- //TODO: to be changed - should be thread pool?
- //run action in thread
-
- if(thread.isAlive()){
- // do nothing
- logger.debug("last action is already running");
- }else{
- thread=getThread();
- thread.start();
- }
+ return action;
- //if action is being performed return true
- return true;
- }
+ }
- //otherwise false
- return false;
- }
+ /**
+ * sets Action that will be run as a task
+ *
+ * @param action
+ */
+ public void setAction(SchedulerAction action) {
+ this.action = action;
- /**
- * get encapsulated SchedulerAction
- * @return
- */
- public SchedulerAction getAction() {
+ }
- return action;
+ /**
+ * Get interval (in seconds) between two subsequence runs of action
+ *
+ * @param interval
+ */
+ public long getInterval() {
- }
+ return interval;
+ }
- /**
- * sets Action that will be run as a task
- * @param action
- */
- public void setAction(SchedulerAction action) {
+ /**
+ * Set interval between two subsequence runs of action,
+ *
+ * @param interval
+ * in seconds
+ */
+ public void setInterval(long interval) {
- this.action = action;
+ this.interval = interval;
- }
+ }
+ /**
+ * Returns timestamp of recent run of action
+ *
+ * @return
+ */
+ public long getTimestamp() {
- /**
- * Get interval (in seconds) between two subsequence runs of action
- * @param interval
- */
- public long getInterval() {
+ return timestamp;
- return interval;
+ }
- }
+ /**
+ * Set timestamp of recent run of action on the beginning of epoch
(1970),
+ * so action should be run immediately (in the next cycle of
Scheduler)
+ *
+ */
+ public void resetTimestamp() {
+ this.timestamp = 0;
- /**
- * Set interval between two subsequence runs of action,
- * @param interval in seconds
- */
- public void setInterval(long interval) {
+ }
- this.interval = interval;
+ /**
+ * Return hash code for this SchedulerTask, which in turn returns
action's
+ * hash code or 0 if action is null.
+ *
+ * @return hash code
+ */
+ public int hashCode() {
- }
+ return action == null ? 0 : action.hashCode();
+ }
- /**
- * Returns timestamp of recent run of action
- * @return
- */
- public long getTimestamp() {
+ /**
+ * Return equality of this object based only on equality of action
objects.
+ *
+ * @param o
+ * other objects (should be instanceof SchedulerTask
+ * @return true if both action objects are equal
+ */
+ public boolean equals(Object o) {
- return timestamp;
+ if (o == null)
+ return false;
- }
+ return o instanceof SchedulerTask ? (((SchedulerTask)
o).action == null ? action == null
+ : ((SchedulerTask) o).action.equals(action))
+ : false;
+ }
- /**
- * Set timestamp of recent run of action on the beginning of epoch
- * (1970), so action should be run immediately (in the next cycle of
- * Scheduler)
- *
- */
- public void resetTimestamp() {
+ /**
+ * checks whether action thread is alive
+ *
+ * @return
+ */
+ public boolean isThreadAlive() {
+ if (thread != null) {
+ return thread.isAlive();
+ } else {
+ return false;
+ }
+ }
- this.timestamp = 0;
-
- }
-
-
- /**
- * Return hash code for this SchedulerTask, which in turn returns
- * action's hash code or 0 if action is null.
- *
- * @return hash code
- */
- public int hashCode() {
-
- return action == null ? 0 : action.hashCode();
-
- }
-
-
- /**
- * Return equality of this object based only on equality of action
- * objects.
- *
- * @param o other objects (should be instanceof SchedulerTask
- * @return true if both action objects are equal
- */
- public boolean equals(Object o) {
-
- if (o == null) return false;
-
- return o instanceof SchedulerTask ?
- (((SchedulerTask)o).action == null ?
- action == null :
- ((SchedulerTask)o).action.equals(action)) :
- false;
-
- }
-
-
-} //SchedulerTask
+} // SchedulerTask
Added:
branches/new-structure-with-base2/ps-mdm-base2/src/main/java/org/perfsonar/base2/service/scheduler/singleThread/SchedulerTaskComparator.java
Added:
branches/new-structure-with-base2/ps-mdm-base2/src/main/java/org/perfsonar/base2/service/scheduler/singleThread/SingleThreadScheduler.java
- perfsonar: r5404 - in branches/new-structure-with-base2/ps-mdm-base2/src/main/java/org/perfsonar/base2/service/scheduler: . singleThread, svnlog, 12/11/2009
Archive powered by MHonArc 2.6.16.