001: /*
002: * hgcommons 7
003: * Hammurapi Group Common Library
004: * Copyright (C) 2003 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.util;
024:
025: import java.util.ArrayList;
026: import java.util.Collections;
027: import java.util.Iterator;
028: import java.util.List;
029:
030: /**
031: * Load balancer distributes work to other workers
032: * according to weights. The less the weight the more
033: * job worker is getting.
034: * @author Pavel Vlasov
035: * @revision $Revision$
036: */
037: public class LoadBalancer implements Worker {
038:
039: private List workers = new ArrayList();
040:
041: private void descore() {
042: synchronized (workers) {
043: Iterator it = workers.iterator();
044: while (it.hasNext()) {
045: ((WorkerEntry) it.next()).score -= Integer.MAX_VALUE;
046: }
047: }
048: }
049:
050: private class WorkerEntry implements Comparable {
051: Worker worker;
052: int weight;
053: int score;
054:
055: /**
056: * @param worker
057: * @param weight Must be >0
058: */
059: WorkerEntry(Worker worker, int weight) {
060: super ();
061: if (weight < 1) {
062: throw new IllegalArgumentException(
063: "Worker weight must be >0");
064: }
065: this .worker = worker;
066: this .weight = weight;
067: }
068:
069: public int compareTo(Object o) {
070: if (o instanceof WorkerEntry) {
071: return score - ((WorkerEntry) o).score;
072: }
073:
074: return hashCode() - o.hashCode();
075: }
076:
077: boolean post(Runnable job) {
078: if (worker.post(job)) {
079: // reduce all scores to avoid overflow.
080: if (score > Integer.MAX_VALUE - weight) {
081: descore();
082: }
083:
084: score += weight;
085: return true;
086: }
087: return false;
088: }
089: }
090:
091: public boolean post(Runnable job) {
092: synchronized (workers) {
093: Collections.sort(workers);
094: Iterator it = workers.iterator();
095: while (it.hasNext()) {
096: WorkerEntry we = (WorkerEntry) it.next();
097: if (we.post(job)) {
098: return true;
099: }
100: }
101: }
102:
103: return false;
104: }
105:
106: public void addWorker(Worker worker, int weight) {
107: synchronized (workers) {
108: workers.add(new WorkerEntry(worker, weight));
109: }
110: }
111: }
|