KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > directwebremoting > util > HitMonitor


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.util;
17
18 /**
19  * We need a way to record how heavily used the server is, and adjust our
20  * behavior to reduce the load on the server.
21  * @author Joe Walker [joe at getahead dot ltd dot uk]
22  */

23 public class HitMonitor
24 {
25     /**
26      * Create a HitMonitor that records the number of hits in the last n
27      * milliseconds.
28      * @param seconds The number of seconds to record hits for
29      */

30     public HitMonitor(int seconds)
31     {
32         hitLog = new long[seconds];
33     }
34
35     /**
36      * A hit has happened, record some load on the server
37      */

38     public void recordHit()
39     {
40         synchronized (hitLog)
41         {
42             trimHitLog();
43             hitLog[0]++;
44         }
45     }
46
47     /**
48      * How to detect the number of hits in the time period specified in the
49      * constructor.
50      * @return The hit count
51      */

52     public int getHitsInLastPeriod()
53     {
54         synchronized (hitLog)
55         {
56             trimHitLog();
57
58             int count = 0;
59             for (int i = 0; i < hitLog.length; i++)
60             {
61                 count += hitLog[i];
62             }
63
64             return count;
65         }
66     }
67
68     /**
69      * Remove all the hits that are no longer relevant.
70      * PERFORMANCE: There is probably a faster way to do this
71      */

72     private void trimHitLog()
73     {
74         long now = getCurrentTimestamp();
75         int secondsPassedSinceLastHit = (int) (now - zeroTimestamp);
76         zeroTimestamp = now;
77
78         if (secondsPassedSinceLastHit > 0)
79         {
80             // Move the counts down
81
for (int i = hitLog.length - 1; i >= 0; i--)
82             {
83                 if (i >= secondsPassedSinceLastHit)
84                 {
85                     hitLog[i] = hitLog[i - secondsPassedSinceLastHit];
86                 }
87                 else
88                 {
89                     hitLog[i] = 0;
90                 }
91             }
92         }
93     }
94
95     /**
96      * A timestamp is {@link System#currentTimeMillis()} divided by 1000
97      * @return The current timestamp
98      */

99     private long getCurrentTimestamp()
100     {
101         return System.currentTimeMillis() / 1000;
102     }
103
104     /**
105      * What is the timestamp of the first element of the hitLog?
106      */

107     private long zeroTimestamp = getCurrentTimestamp();
108
109     /**
110      * Our log of hits
111      */

112     private final long[] hitLog;
113 }
114
Popular Tags