KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collection JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.LinkedList JavaDoc;
26 import java.util.Queue JavaDoc;
27 import java.util.Set JavaDoc;
28
29 import net.sf.hajdbc.Database;
30
31 /**
32  * @author Paul Ferraro
33  * @since 1.0
34  */

35 public class RoundRobinBalancer extends AbstractBalancer
36 {
37     private Set JavaDoc<Database> databaseSet = new HashSet JavaDoc<Database>();
38     private Queue JavaDoc<Database> databaseQueue = new LinkedList JavaDoc<Database>();
39
40     /**
41      * @see net.sf.hajdbc.balancer.AbstractBalancer#getDatabases()
42      */

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

52     @Override JavaDoc
53     public synchronized boolean add(Database database)
54     {
55         boolean added = super.add(database);
56         
57         if (added)
58         {
59             int weight = database.getWeight();
60             
61             for (int i = 0; i < weight; ++i)
62             {
63                 this.databaseQueue.add(database);
64             }
65         }
66         
67         return added;
68     }
69
70     /**
71      * @see net.sf.hajdbc.Balancer#remove(net.sf.hajdbc.Database)
72      */

73     @Override JavaDoc
74     public synchronized boolean remove(Database database)
75     {
76         boolean removed = super.remove(database);
77
78         if (removed)
79         {
80             int weight = database.getWeight();
81
82             for (int i = 0; i < weight; ++i)
83             {
84                 this.databaseQueue.remove(database);
85             }
86         }
87         
88         return removed;
89     }
90     
91     /**
92      * @see net.sf.hajdbc.Balancer#first()
93      */

94     @Override JavaDoc
95     public synchronized Database first()
96     {
97         return (this.databaseQueue.isEmpty()) ? super.first() : this.databaseQueue.element();
98     }
99     
100     /**
101      * @see net.sf.hajdbc.Balancer#next()
102      */

103     public synchronized Database next()
104     {
105         if (this.databaseQueue.isEmpty())
106         {
107             return super.first();
108         }
109         
110         Database database = this.databaseQueue.remove();
111         
112         this.databaseQueue.add(database);
113         
114         return database;
115     }
116 }
117
Popular Tags