KickJava   Java API By Example, From Geeks To Geeks.

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


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 avg. request duration.
16  * @jmx:mbean name="jboss.web.loadbalancer: service=MinAvgScheduler"
17  * extends="org.jboss.web.loadbalancer.scheduler.AbstractSchedulerMBean"
18  *
19  * @author Thomas Peuss <jboss@peuss.de>
20  * @version $Revision: 1.2 $
21  */

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