1 package org.sapia.taskman; 2 3 import java.io.Serializable ; 4 import java.util.Map ; 5 6 24 public abstract class TaskDescriptor implements Serializable { 25 26 static final long serialVersionUID = 1L; 27 28 public static final int PERIODIC = 0; 29 public static final int TRANSIENT = 1; 30 private int _occurrence; 31 private long _intervalMillis; 32 private long _nextExecTime; 33 private boolean _executed; 34 private String _name; 35 private Task _task; 36 private transient TaskOutput _out; 37 private Map _contextVals; 38 private boolean _root; 39 40 53 public TaskDescriptor(String name, int occurrence, long intervalMillis, 54 Task task) { 55 _occurrence = occurrence; 56 _intervalMillis = intervalMillis; 57 _task = task; 58 _name = name; 59 } 60 61 String getName() { 62 return _name; 63 } 64 65 TaskDescriptor setRoot(boolean root) { 66 _root = root; 67 68 return this; 69 } 70 71 void setTaskOutput(TaskOutput out) { 72 _out = out; 73 } 74 75 TaskOutput getTaskOutput() { 76 return _out; 77 } 78 79 void setContextVals(Map vals) { 80 _contextVals = vals; 81 } 82 83 Map getContextVals() { 84 return _contextVals; 85 } 86 87 boolean isFinished() { 88 return (_executed && (_occurrence == TRANSIENT)) 89 || (_task instanceof Abortable && ((Abortable) _task).isAborted()); 90 } 91 92 long nextExecTime() { 93 return _nextExecTime; 94 } 95 96 boolean isPeriodic() { 97 return _occurrence == PERIODIC; 98 } 99 100 void calcNextExecTime() { 101 _nextExecTime = System.currentTimeMillis() + _intervalMillis; 102 } 103 104 final void exec(TaskContext ctx) { 105 if(_out != null) { 106 _out.setTaskName(_name); 107 } 108 109 if(_contextVals != null) { 110 ctx.copyVals(_contextVals); 111 } 112 113 _task.exec(ctx); 114 _executed = true; 115 calcNextExecTime(); 116 117 if(_root) { 118 ctx.getTaskOutput().close(); 119 } 120 } 121 } 122 | Popular Tags |