KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > demo > sharedqueue > Queue


1 /*
2 @COPYRIGHT@
3 */

4 package demo.sharedqueue;
5
6 import java.net.InetAddress JavaDoc;
7 import java.net.UnknownHostException JavaDoc;
8 import java.util.Collections JavaDoc;
9 import java.util.HashMap JavaDoc;
10 import java.util.LinkedList JavaDoc;
11 import java.util.List JavaDoc;
12 import java.util.ListIterator JavaDoc;
13 import java.util.Random JavaDoc;
14
15 public class Queue {
16    private List JavaDoc queue = Collections.synchronizedList(new LinkedList JavaDoc());
17    private List JavaDoc workers = Collections.synchronizedList(new LinkedList JavaDoc());
18    private List JavaDoc completedJobs = Collections.synchronizedList(new LinkedList JavaDoc());
19    private int nextJobId;
20    private int port;
21    private static final int MAX_HISTORY_LENGTH = 15;
22    private static final int MAX_QUEUE_LENGTH = 150;
23
24    public Queue(int port) {
25       this.port = port;
26       this.nextJobId = 1;
27    }
28
29    public Job getJob() {
30       synchronized (queue) {
31          while (queue.size() == 0) {
32             try {
33                queue.wait();
34             }
35             catch (InterruptedException JavaDoc e) {
36                throw new RuntimeException JavaDoc(e);
37             }
38          }
39          return (Job) queue.remove(0);
40       }
41    }
42
43    public String JavaDoc getXmlData() {
44       // the list of jobs in the queue
45
String JavaDoc data = "<workqueue>";
46       synchronized (queue) {
47          ListIterator JavaDoc i = queue.listIterator();
48          while (i.hasNext()) {
49             Job job = (Job) i.next();
50             data += job.toXml();
51          }
52       }
53       data += "</workqueue>";
54
55       // the list of completed jobs
56
data += "<completed>";
57       synchronized (completedJobs) {
58          ListIterator JavaDoc i = completedJobs.listIterator();
59          while (i.hasNext()) {
60             Job job = (Job) i.next();
61             data += job.toXml();
62          }
63       }
64       data += "</completed>";
65
66       // the list of registered job consumers
67
data += "<consumers>";
68       synchronized (workers) {
69          ListIterator JavaDoc i = workers.listIterator();
70          while (i.hasNext()) {
71             Worker worker = (Worker) i.next();
72             data += worker.toXml();
73          }
74       }
75       data += "</consumers>";
76       return data;
77    }
78
79    public Worker createWorker(String JavaDoc nodeId) {
80       synchronized (workers) {
81          Worker worker = new Worker(this, port, nodeId);
82          workers.add(worker);
83          Thread JavaDoc t = new Thread JavaDoc(worker);
84          t.setDaemon(true);
85          t.start();
86          return worker;
87       }
88    }
89    
90    public Worker getWorker(String JavaDoc nodeId) {
91       synchronized (workers) {
92          ListIterator JavaDoc i = workers.listIterator();
93          while (i.hasNext()) {
94             Worker worker = (Worker) i.next();
95             if (worker.getNodeId().equals(nodeId)) {
96                return worker;
97             }
98          }
99       }
100       return null;
101    }
102
103    public void log(Job job) {
104       synchronized (completedJobs) {
105          completedJobs.add(0, job);
106          if (completedJobs.size() > MAX_HISTORY_LENGTH) {
107             completedJobs.remove(completedJobs.size() - 1);
108          }
109       }
110    }
111
112    public void reap() {
113       synchronized (workers) {
114          ListIterator JavaDoc i = workers.listIterator();
115          while (i.hasNext()) {
116             Worker worker = (Worker) i.next();
117             if (worker.expire()) {
118                i.remove();
119             }
120          }
121       }
122    }
123    
124    public void addJob() {
125       synchronized (queue) {
126          if (queue.size() >= MAX_QUEUE_LENGTH) {
127             return;
128          }
129
130          Job job = new Job(Queue.getHostName() + " " + this.port, this.nextJobId);
131          this.nextJobId = this.nextJobId < 999 ? this.nextJobId + 1 : 1;
132          queue.add(job);
133          queue.notifyAll();
134       }
135    }
136
137    public void addJob(Job job) {
138       synchronized (queue) {
139          queue.add(job);
140          queue.notifyAll();
141       }
142    }
143
144    public static String JavaDoc getHostName() {
145       try {
146          InetAddress JavaDoc addr = InetAddress.getLocalHost();
147          byte[] ipAddr = addr.getAddress();
148          return addr.getHostName();
149       }
150       catch (UnknownHostException JavaDoc e) {
151          return "Unknown";
152       }
153    }
154 }
155
Popular Tags