/[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 2891 - (show annotations) (download)
Sun Jan 31 22:45:03 2016 UTC (8 years, 3 months ago) by torben
File size: 2334 byte(s)
Show progress when doing distance calculations
1 package dk.daoas.adressevedligehold.tasks;
2
3 import dk.daoas.adressevedligehold.util.TimingHelper;
4
5 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
6
7 public abstract class Task implements Runnable {
8
9 public enum TaskState {
10 STATE_QUEUED, STATE_RUNNING, STATE_DONE, STATE_ABORTED;
11 }
12
13 @SuppressFBWarnings("URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")//bliver læst via gson - og det forvirrer findbugs
14 public static class TaskBean {
15 public String description;
16 public String detail;
17 public String errorMessage;
18 public double percentCompleted;
19 public String state;
20 }
21
22 protected boolean abort = false;
23 protected TaskState state = TaskState.STATE_QUEUED;
24 protected TaskManager manager;
25 private String errorMsg;
26
27 public void setManager(TaskManager manager) {
28 this.manager = manager;
29 }
30
31 @Override
32 public final void run() {
33 TimingHelper timing = new TimingHelper();
34 System.out.println("Starting " + this.getDescription() );
35
36 this.state = TaskState.STATE_RUNNING;
37 manager.setCurrentTask(this);
38
39 try {
40 taskRun();
41 this.state = TaskState.STATE_DONE;
42 } catch (Exception e) {
43 this.errorMsg = e.getMessage();
44 e.printStackTrace();
45 this.state = TaskState.STATE_ABORTED;
46 }
47
48 manager.setCurrentTask(null);
49
50 System.out.println("Done " + this.getDescription() + " " + timing.getElapsed() + "ms");
51 }
52
53
54 public TaskState getState() {
55 return this.state;
56 }
57
58 public boolean isAborted() {
59 return this.abort;
60 }
61
62 public void doAbort() {
63 this.abort = true;
64 }
65
66 public TaskBean getTaskBean() {
67 TaskBean bean = new TaskBean();
68
69 bean.description = this.getDescription();
70 bean.detail = this.getDetail();
71 bean.percentCompleted = this.getPercentCompleted();
72 bean.state = this.state.toString();
73 bean.errorMessage = this.getErrorMessage();
74
75 return bean;
76 }
77
78 public String getErrorMessage() {
79 return this.errorMsg;
80 }
81
82 /**
83 * @throws Exception
84 *
85 * Implementing classes should not catch terminating exceptions but let it propagate to Task the Task::run()
86 * method.
87 */
88 protected abstract void taskRun() throws Exception;
89
90 public abstract String getDescription();
91 public abstract String getDetail();
92 public abstract double getPercentCompleted();
93
94 }

  ViewVC Help
Powered by ViewVC 1.1.20