/[projects]/dao/DaoAdresseVedligehold/src/main/java/dk/daoas/adressevedligehold/tasks/Task.java
ViewVC logotype

Contents of /dao/DaoAdresseVedligehold/src/main/java/dk/daoas/adressevedligehold/tasks/Task.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2992 - (show annotations) (download)
Tue Mar 29 19:19:11 2016 UTC (8 years, 1 month ago) by torben
File size: 3859 byte(s)
Add inter-task delay
1 package dk.daoas.adressevedligehold.tasks;
2
3 import dk.daoas.adressevedligehold.util.TimingHelper;
4 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
5
6 public abstract class Task implements Runnable {
7
8 private TaskLogger logger = TaskLogger.getInstance();
9
10 public enum TaskState {
11 STATE_QUEUED, STATE_RUNNING, STATE_DONE, STATE_ABORTED;
12 }
13
14 @SuppressFBWarnings("URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")//bliver læst via gson - og det forvirrer findbugs
15 public static class TaskBean {
16 public int id;
17 public String description;
18 public String detail;
19 public String errorMessage;
20 public double percentCompleted;
21 public String state;
22 }
23
24 protected volatile boolean abort = false;//mark volatile to make sure value isn't cached by threads
25 protected volatile TaskState state = TaskState.STATE_QUEUED;
26 protected TaskManager manager;
27 private String errorMsg;
28
29 private String logMessages;
30
31 private int id;
32
33 public final void setManager(TaskManager manager) {
34 this.manager = manager;
35 }
36
37 @Override
38 public final void run() {
39
40 if (this.state == TaskState.STATE_ABORTED) {//if this task as cancelled while still in queue
41 return;
42 }
43
44
45
46 TaskLogger.getInstance().reset();
47
48 TimingHelper timing = new TimingHelper();
49
50
51 logger.info("Starting " + this.getDescription() );
52
53 this.state = TaskState.STATE_RUNNING;
54 manager.setCurrentTask(this);
55
56 try {
57 taskRun();
58
59 if (this.state != TaskState.STATE_ABORTED) { //just to make sure we wasn't aborted
60 this.state = TaskState.STATE_DONE;
61 }
62
63
64 } catch (Exception e) {
65 logger.warning("Error during taskrun", e);
66 this.errorMsg = e.getMessage();
67 this.state = TaskState.STATE_ABORTED;
68 }
69
70 manager.setCurrentTask(null);
71
72 logger.info("Done " + this.getDescription() + " " + timing.getElapsed() + "ms");
73
74 logMessages = TaskLogger.getInstance().getBuffer();
75
76
77 // Make some delay between tasks to make sure that the VM settles and other contexts can perform their tasks as well
78 try {
79 Thread.sleep(60*1000);
80 } catch (InterruptedException e) {
81 e.printStackTrace();
82 }
83
84 }
85
86 public final String getLog() {
87 if (this.state == TaskState.STATE_RUNNING) {
88 return TaskLogger.getInstance().getBuffer(); //if live return current log buffer content
89 } else {
90 return logMessages;
91 }
92 }
93
94 public final void setId(int newId) {
95 this.id = newId;
96 }
97
98 public final int getId() {
99 return this.id;
100 }
101
102 public final TaskState getState() {
103 return this.state;
104 }
105
106 public boolean isAborted() {
107 return this.abort;
108 }
109
110 public void doAbort() {
111 this.abort = true;
112 }
113
114 public void doAbort(Exception e) {
115 this.abort = true;
116 this.errorMsg = e.getMessage();
117 }
118
119 public TaskBean getTaskBean() {
120 TaskBean bean = new TaskBean();
121 bean.id = this.getId();
122 bean.description = this.getDescription();
123 bean.detail = this.getDetail();
124 bean.percentCompleted = this.getPercentCompleted();
125 bean.state = this.state.toString();
126 bean.errorMessage = this.getErrorMessage();
127
128 return bean;
129 }
130
131 public String getErrorMessage() {
132 return this.errorMsg;
133 }
134
135 @Override
136 public int hashCode() {
137 return this.id;
138 }
139
140 @Override
141 public boolean equals(Object o) {
142 if (! (o instanceof Task))
143 return false;
144
145 Task otherTask = (Task) o;
146
147 return this.getId() == otherTask.getId();
148 }
149
150 /**
151 * @throws Exception
152 *
153 * Implementing classes should not catch terminating exceptions but let it propagate to Task the Task::run()
154 * method.
155 */
156 protected abstract void taskRun() throws Exception;
157
158 public abstract String getDescription();
159 public abstract String getDetail();
160 public abstract double getPercentCompleted();
161
162 }

  ViewVC Help
Powered by ViewVC 1.1.20