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

  ViewVC Help
Powered by ViewVC 1.1.20