/[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 2844 - (show annotations) (download)
Mon Jan 25 21:43:59 2016 UTC (8 years, 3 months ago) by torben
File size: 2098 byte(s)
Implemented AdressManager.visitRange()

Added AddressSource.validate() to let manager do a pre-validation before loading data from DB

Added support for DAO

Make Task and webpage show error messages
1 package dk.daoas.adressevedligehold.tasks;
2
3 import dk.daoas.adressevedligehold.util.TimingHelper;
4
5 public abstract class Task implements Runnable {
6
7 public enum TaskState {
8 STATE_QUEUED, STATE_RUNNING, STATE_DONE, STATE_ABORTED;
9 }
10
11 public static class TaskBean {
12 public String description;
13 public String detail;
14 public String errorMessage;
15 public double percentCompleted;
16 public String state;
17 }
18
19 protected boolean abort = false;
20 protected TaskState state = TaskState.STATE_QUEUED;
21 protected TaskManager manager;
22 private String errorMsg;
23
24 public void setManager(TaskManager manager) {
25 this.manager = manager;
26 }
27
28 @Override
29 public final void run() {
30 TimingHelper timing = new TimingHelper();
31 System.out.println("Starting " + this.getDescription() );
32
33 this.state = TaskState.STATE_RUNNING;
34 manager.setCurrentTask(this);
35
36 try {
37 taskRun();
38 this.state = TaskState.STATE_DONE;
39 } catch (Exception e) {
40 this.errorMsg = e.getMessage();
41 e.printStackTrace();
42 this.state = TaskState.STATE_ABORTED;
43 }
44
45 manager.setCurrentTask(null);
46
47 System.out.println("Done " + this.getDescription() + " " + timing.getElapsed() + "ms");
48 }
49
50
51 public TaskState getState() {
52 return this.state;
53 }
54
55 public void doAbort() {
56 this.abort = true;
57 }
58
59 public TaskBean getTaskBean() {
60 TaskBean bean = new TaskBean();
61
62 bean.description = this.getDescription();
63 bean.detail = this.getDetail();
64 bean.percentCompleted = this.getPercentCompleted();
65 bean.state = this.state.toString();
66 bean.errorMessage = this.getErrorMessage();
67
68 return bean;
69 }
70
71 public String getErrorMessage() {
72 return this.errorMsg;
73 }
74
75 /**
76 * @throws Exception
77 *
78 * Implementing classes should not catch terminating exceptions but let it propagate to Task the Task::run()
79 * method.
80 */
81 protected abstract void taskRun() throws Exception;
82
83 public abstract String getDescription();
84 public abstract String getDetail();
85 public abstract short getPercentCompleted();
86
87 }

  ViewVC Help
Powered by ViewVC 1.1.20