LoadBalancer.java

biz/hammurapi/util/LoadBalancer.java

Violations

Inspector Message Severity Location
Java Inspector 048 Copyrights information should be present in each file. 1
Java Inspector 089 Undocumented method 2 68:17
Java Inspector 089 Undocumented method 2 90:9
Java Inspector 089 Undocumented method 2 105:9
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 62:68

Source code

1/*
2 * hgcommons 9
3 * Hammurapi Group Common Library
4 * Copyright (C) 2003 Hammurapi Group
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
21 * e-Mail: support@hammurapi.biz
22 */
23package biz.hammurapi.util;
24
25import java.util.ArrayList;
26import java.util.Collections;
27import java.util.Iterator;
28import java.util.List;
29
30/**
31 * Load balancer distributes work to other workers
32 * according to weights. The less the weight the more
33 * job worker is getting.
34 * @author Pavel Vlasov
35 * @revision $Revision$
36 */
37public class LoadBalancer implements Worker {
38
39 private List workers=new ArrayList();
40
41 private void descore() {
42 synchronized (workers) {
43 Iterator it=workers.iterator();
44 while (it.hasNext()) {
45 ((WorkerEntry) it.next()).score-=Integer.MAX_VALUE;
46 }
47 }
48 }
49
50 private class WorkerEntry implements Comparable {
51 Worker worker;
52 int weight;
53 int score;
54
55 /**
56 * @param worker
57 * @param weight Must be >0
58 */
59 WorkerEntry(Worker worker, int weight) {
60 super();
61 if (weight<1) {
62 throw new IllegalArgumentException("Worker weight must be >0");
63 }
64 this.worker = worker;
65 this.weight = weight;
66 }
67
68 public int compareTo(Object o) {
69 if (o instanceof WorkerEntry) {
70 return score-((WorkerEntry) o).score;
71 }
72
73 return hashCode()-o.hashCode();
74 }
75
76 boolean post(Runnable job) {
77 if (worker.post(job)) {
78 // reduce all scores to avoid overflow.
79 if (score>Integer.MAX_VALUE-weight) {
80 descore();
81 }
82
83 score+=weight;
84 return true;
85 }
86 return false;
87 }
88 }
89
90 public boolean post(Runnable job) {
91 synchronized (workers) {
92 Collections.sort(workers);
93 Iterator it=workers.iterator();
94 while (it.hasNext()) {
95 WorkerEntry we=(WorkerEntry) it.next();
96 if (we.post(job)) {
97 return true;
98 }
99 }
100 }
101
102 return false;
103 }
104
105 public void addWorker(Worker worker, int weight) {
106 synchronized (workers) {
107 workers.add(new WorkerEntry(worker, weight));
108 }
109 }
110}
111