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

  ViewVC Help
Powered by ViewVC 1.1.20