/[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 2915 - (show annotations) (download)
Thu Feb 4 09:22:05 2016 UTC (8 years, 3 months ago) by torben
File size: 3123 byte(s)
Task: only set state=done if the task wasn't aborted
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 TaskLogger.getInstance().reset();
41
42 TimingHelper timing = new TimingHelper();
43
44
45 logger.info("Starting " + this.getDescription() );
46
47 this.state = TaskState.STATE_RUNNING;
48 manager.setCurrentTask(this);
49
50 try {
51 taskRun();
52
53 if (this.state != TaskState.STATE_ABORTED) { //just to make sure we wasn't aborted
54 this.state = TaskState.STATE_DONE;
55 }
56
57
58 } catch (Exception e) {
59 this.errorMsg = e.getMessage();
60 e.printStackTrace();
61 this.state = TaskState.STATE_ABORTED;
62 }
63
64 manager.setCurrentTask(null);
65
66 logger.info("Done " + this.getDescription() + " " + timing.getElapsed() + "ms");
67
68 logMessages = TaskLogger.getInstance().getBuffer();
69 }
70
71 public final String getLog() {
72 if (this.state == TaskState.STATE_RUNNING) {
73 return TaskLogger.getInstance().getBuffer(); //if live return current log buffer content
74 } else {
75 return logMessages;
76 }
77 }
78
79 public final void setId(int newId) {
80 this.id = newId;
81 }
82
83 public final int getId() {
84 return this.id;
85 }
86
87 public final TaskState getState() {
88 return this.state;
89 }
90
91 public boolean isAborted() {
92 return this.abort;
93 }
94
95 public void doAbort() {
96 this.abort = true;
97 }
98
99 public TaskBean getTaskBean() {
100 TaskBean bean = new TaskBean();
101 bean.id = this.getId();
102 bean.description = this.getDescription();
103 bean.detail = this.getDetail();
104 bean.percentCompleted = this.getPercentCompleted();
105 bean.state = this.state.toString();
106 bean.errorMessage = this.getErrorMessage();
107
108 return bean;
109 }
110
111 public String getErrorMessage() {
112 return this.errorMsg;
113 }
114
115 /**
116 * @throws Exception
117 *
118 * Implementing classes should not catch terminating exceptions but let it propagate to Task the Task::run()
119 * method.
120 */
121 protected abstract void taskRun() throws Exception;
122
123 public abstract String getDescription();
124 public abstract String getDetail();
125 public abstract double getPercentCompleted();
126
127 }

  ViewVC Help
Powered by ViewVC 1.1.20