KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > hajdbc > balancer > LoadBalancer


1 /*
2  * HA-JDBC: High-Availability JDBC
3  * Copyright (c) 2004-2006 Paul Ferraro
4  *
5  * This library is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU Lesser General Public License as published by the
7  * Free Software Foundation; either version 2.1 of the License, or (at your
8  * option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13  * for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; if not, write to the Free Software Foundation,
17  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: ferraro@users.sourceforge.net
20  */

21 package net.sf.hajdbc.balancer;
22
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.Comparator JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import net.sf.hajdbc.Database;
32
33 /**
34  * @author Paul Ferraro
35  * @since 1.0
36  */

37 public class LoadBalancer extends AbstractBalancer
38 {
39     private Map JavaDoc<Database, Integer JavaDoc> databaseMap = new HashMap JavaDoc<Database, Integer JavaDoc>();
40     
41     /**
42      * @see net.sf.hajdbc.balancer.AbstractBalancer#getDatabases()
43      */

44     @Override JavaDoc
45     protected Collection JavaDoc<Database> getDatabases()
46     {
47         return this.databaseMap.keySet();
48     }
49
50     /**
51      * @see net.sf.hajdbc.Balancer#add(net.sf.hajdbc.Database)
52      */

53     @Override JavaDoc
54     public synchronized boolean add(Database database)
55     {
56         boolean exists = this.databaseMap.containsKey(database);
57         
58         if (!exists)
59         {
60             this.databaseMap.put(database, 1);
61         }
62         
63         return !exists;
64     }
65     
66     /**
67      * @see net.sf.hajdbc.Balancer#next()
68      */

69     public synchronized Database next()
70     {
71         if (this.databaseMap.size() <= 1)
72         {
73             return this.first();
74         }
75         
76         List JavaDoc<Map.Entry JavaDoc<Database, Integer JavaDoc>> databaseMapEntryList = new ArrayList JavaDoc<Map.Entry JavaDoc<Database, Integer JavaDoc>>(this.databaseMap.entrySet());
77         
78         Collections.sort(databaseMapEntryList, comparator);
79         
80         Map.Entry JavaDoc<Database, Integer JavaDoc> mapEntry = databaseMapEntryList.get(0);
81         
82         return mapEntry.getKey();
83     }
84     
85     /**
86      * @see net.sf.hajdbc.Balancer#beforeOperation(net.sf.hajdbc.Database)
87      */

88     @Override JavaDoc
89     public void beforeOperation(Database database)
90     {
91         this.incrementLoad(database, 1);
92     }
93     
94     /**
95      * @see net.sf.hajdbc.Balancer#afterOperation(net.sf.hajdbc.Database)
96      */

97     @Override JavaDoc
98     public void afterOperation(Database database)
99     {
100         this.incrementLoad(database, -1);
101     }
102     
103     private synchronized void incrementLoad(Database database, int increment)
104     {
105         Integer JavaDoc load = this.databaseMap.remove(database);
106
107         if (load != null)
108         {
109             this.databaseMap.put(database, load + increment);
110         }
111     }
112     
113     private static Comparator JavaDoc<Map.Entry JavaDoc<Database, Integer JavaDoc>> comparator = new Comparator JavaDoc<Map.Entry JavaDoc<Database, Integer JavaDoc>>()
114     {
115         public int compare(Map.Entry JavaDoc<Database, Integer JavaDoc> mapEntry1, Map.Entry JavaDoc<Database, Integer JavaDoc> mapEntry2)
116         {
117             Database database1 = mapEntry1.getKey();
118             Database database2 = mapEntry2.getKey();
119
120             Integer JavaDoc load1 = mapEntry1.getValue();
121             Integer JavaDoc load2 = mapEntry2.getValue();
122             
123             int weight1 = database1.getWeight();
124             int weight2 = database2.getWeight();
125             
126             if (weight1 == weight2)
127             {
128                 return load1.compareTo(load2);
129             }
130             
131             float weightedLoad1 = (weight1 != 0) ? (load1.floatValue() / weight1) : Float.POSITIVE_INFINITY;
132             float weightedLoad2 = (weight2 != 0) ? (load2.floatValue() / weight2) : Float.POSITIVE_INFINITY;
133             
134             return Float.compare(weightedLoad1, weightedLoad2);
135         }
136     };
137 }
138
Popular Tags