01: /*
02: @COPYRIGHT@
03: */
04: package demo.sharedqueue;
05:
06: import java.util.Random;
07:
08: public class Job {
09:
10: private final int duration;
11:
12: private final String producer;
13:
14: private Worker consumer;
15:
16: private final int type;
17:
18: private int state;
19:
20: private String id;
21:
22: private final static int STATE_READY = 0;
23:
24: private final static int STATE_PROCESSING = 1;
25:
26: private final static int STATE_COMPLETE = 2;
27:
28: private final static int STATE_ABORTED = 3;
29:
30: public Job(String producer, int id) {
31: Random random = new Random();
32: this .state = STATE_READY;
33: this .consumer = null;
34: this .producer = producer;
35: this .duration = random.nextInt(3) + 3;
36: this .type = random.nextInt(3) + 1;
37: this .id = Integer.toString(id);
38: while (this .id.length() < 3) {
39: this .id = "0" + this .id;
40: }
41: }
42:
43: public final void run(Worker consumer) {
44: synchronized (this ) {
45: this .state = STATE_PROCESSING;
46: this .consumer = consumer;
47: try {
48: Thread.sleep(duration * 1000L);
49: this .state = STATE_COMPLETE;
50: } catch (InterruptedException ie) {
51: this .state = STATE_ABORTED;
52: }
53: }
54: }
55:
56: public final String toXml() {
57: return "<job>" + "<id>" + id + "</id>" + "<type>" + type
58: + "</type>" + "<state>" + state + "</state>"
59: + "<producer>" + producer + "</producer>"
60: + "<consumer>" + getConsumer() + "</consumer>"
61: + "<duration>" + duration + "</duration>" + "</job>";
62: }
63:
64: private final String getConsumer() {
65: return consumer == null ? "" : consumer.getName();
66: }
67: }
|