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