KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > web > loadbalancer > scheduler > WeightedLeastConnectionSchedulerService


1 /*
2  * JBoss, the OpenSource WebOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.web.loadbalancer.scheduler;
8
9 import java.net.URL JavaDoc;
10 import java.util.Comparator JavaDoc;
11 import java.util.Collections JavaDoc;
12 import java.util.NoSuchElementException JavaDoc;
13
14 /**
15  * A scheduler that chooses the node with the lowest current connection count. It
16  * takes the loadbalance factor (lbFactor) into account. The node with the higher lbFactor
17  * gets more requests.
18  *
19  * @jmx:mbean name="jboss.web.loadbalancer: service=WeightedLeastConnectionScheduler"
20  * extends="org.jboss.web.loadbalancer.scheduler.AbstractSchedulerMBean"
21  *
22  * @author Thomas Peuss <jboss@peuss.de>
23  * @version $Revision: 1.1 $
24  */

25 public class WeightedLeastConnectionSchedulerService
26     extends AbstractScheduler implements WeightedLeastConnectionSchedulerServiceMBean {
27
28   private int index = 0;
29   private WeightedLeastConnectionComparator comparator=new WeightedLeastConnectionComparator();
30
31   public WeightedLeastConnectionSchedulerService() {
32   }
33
34   protected Host getNextHost() {
35     Host host = null;
36     try
37     {
38       synchronized (this)
39       {
40          host=(Host)Collections.min(hostsUp, comparator);
41       }
42     }
43     catch (NoSuchElementException JavaDoc nsee)
44     {
45        return null;
46     }
47     return host;
48   }
49 }
50
51 class WeightedLeastConnectionComparator implements Comparator JavaDoc
52 {
53    public int compare(Object JavaDoc o1, Object JavaDoc o2)
54    {
55       Host h1=(Host)o1;
56       Host h2=(Host)o2;
57
58       return ((h1.getCurrentConnections()/h1.getLbFactor())-(h2.getCurrentConnections()/h2.getLbFactor()));
59    }
60 }
61
Popular Tags