KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > directwebremoting > impl > ThreadDroppingServerLoadMonitor


1 /*
2  * Copyright 2005 Joe Walker
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.directwebremoting.impl;
17
18 import java.util.Random JavaDoc;
19
20 import org.directwebremoting.extend.ServerLoadMonitor;
21 import org.directwebremoting.extend.WaitController;
22 import org.directwebremoting.util.HitMonitor;
23
24 /**
25  * A smart implementation of ServerLoadMonitor, customized for Jetty.
26  * The ThreadDroppingServerLoadMonitor attempts to keep the hit rate down by increasing
27  * the disconnected time as usage increases.
28  * @author Joe Walker [joe at getahead dot org]
29  */

30 public class ThreadDroppingServerLoadMonitor extends AbstractServerLoadMonitor implements ServerLoadMonitor
31 {
32     /* (non-Javadoc)
33      * @see org.directwebremoting.extend.ServerLoadMonitor#getConnectedTime()
34      */

35     public long getConnectedTime()
36     {
37         return connectedTime;
38     }
39
40     /* (non-Javadoc)
41      * @see org.directwebremoting.ServerLoadMonitor#timeToNextPoll()
42      */

43     public int getDisconnectedTime()
44     {
45         return disconnectedTime;
46     }
47
48     /* (non-Javadoc)
49      * @see org.directwebremoting.impl.AbstractServerLoadMonitor#threadWaitStarting(org.directwebremoting.extend.WaitController)
50      */

51     public void threadWaitStarting(WaitController controller)
52     {
53         hitMonitor.recordHit();
54         super.threadWaitStarting(controller);
55
56         checkLoading();
57     }
58
59     /* (non-Javadoc)
60      * @see org.directwebremoting.impl.AbstractServerLoadMonitor#threadWaitEnding(org.directwebremoting.extend.WaitController)
61      */

62     public void threadWaitEnding(WaitController controller)
63     {
64         super.threadWaitEnding(controller);
65     }
66
67     /**
68      * Check that we are setting the time to next poll correctly.
69      */

70     private void checkLoading()
71     {
72         float hitsPerSecond = (float) hitMonitor.getHitsInLastPeriod() / SECONDS_MONITORED;
73
74         // If we're getting close to the upper bound then slow down
75
float load = hitsPerSecond / maxHitsPerSecond;
76
77         disconnectedTime = (int) (disconnectedTime * load);
78         if (disconnectedTime == 0)
79         {
80             disconnectedTime = 1;
81         }
82     }
83
84     /**
85      * @param maxHitsPerSecond the maxHitsPerSecond to set
86      */

87     public void setMaxHitsPerSecond(int maxHitsPerSecond)
88     {
89         this.maxHitsPerSecond = maxHitsPerSecond;
90     }
91
92     /**
93      * Static configuration data: The max number of hits per second.
94      * We increase the poll time to compensate and reduce the load. If this
95      * number is not at least half maxWaitingThreads then the USAGE_HIGH mode
96      * will not exist and the system will sublime from USAGE_LOW to USAGE_DIGG
97      */

98     protected int maxHitsPerSecond = 100;
99
100     /**
101      * We ask clients to wait a random number of millis before they come
102      * back to avoid killing the server
103      */

104     protected Random JavaDoc random = new Random JavaDoc();
105
106     /**
107      * The time we are currently waiting before sending a browser away and
108      * asking it to reconnect.
109      */

110     protected int connectedTime = 60000;
111
112     /**
113      * How long are we telling users to wait before they come back next
114      */

115     protected int disconnectedTime = 1;
116
117     /**
118      * We are recording the number of hits in the last 5 seconds.
119      * Maybe we should think about making this configurable.
120      */

121     protected static final int SECONDS_MONITORED = 10;
122
123     /**
124      * Our record of the server loading
125      */

126     protected HitMonitor hitMonitor = new HitMonitor(SECONDS_MONITORED);
127 }
128
Popular Tags