/[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 2928 - (show annotations) (download)
Fri Feb 5 11:09:02 2016 UTC (8 years, 3 months ago) by torben
File size: 3623 byte(s)
Make it possible to remove queued tasks
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 public final String getLog() {
78 if (this.state == TaskState.STATE_RUNNING) {
79 return TaskLogger.getInstance().getBuffer(); //if live return current log buffer content
80 } else {
81 return logMessages;
82 }
83 }
84
85 public final void setId(int newId) {
86 this.id = newId;
87 }
88
89 public final int getId() {
90 return this.id;
91 }
92
93 public final TaskState getState() {
94 return this.state;
95 }
96
97 public boolean isAborted() {
98 return this.abort;
99 }
100
101 public void doAbort() {
102 this.abort = true;
103 }
104
105 public void doAbort(Exception e) {
106 this.abort = true;
107 this.errorMsg = e.getMessage();
108 }
109
110 public TaskBean getTaskBean() {
111 TaskBean bean = new TaskBean();
112 bean.id = this.getId();
113 bean.description = this.getDescription();
114 bean.detail = this.getDetail();
115 bean.percentCompleted = this.getPercentCompleted();
116 bean.state = this.state.toString();
117 bean.errorMessage = this.getErrorMessage();
118
119 return bean;
120 }
121
122 public String getErrorMessage() {
123 return this.errorMsg;
124 }
125
126 @Override
127 public int hashCode() {
128 return this.id;
129 }
130
131 @Override
132 public boolean equals(Object o) {
133 if (! (o instanceof Task))
134 return false;
135
136 Task otherTask = (Task) o;
137
138 return this.getId() == otherTask.getId();
139 }
140
141 /**
142 * @throws Exception
143 *
144 * Implementing classes should not catch terminating exceptions but let it propagate to Task the Task::run()
145 * method.
146 */
147 protected abstract void taskRun() throws Exception;
148
149 public abstract String getDescription();
150 public abstract String getDetail();
151 public abstract double getPercentCompleted();
152
153 }

  ViewVC Help
Powered by ViewVC 1.1.20