KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > web > stats > PWCConnectionQueueStatsImpl


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.web.stats;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.logging.Logger JavaDoc;
28 import java.util.logging.Level JavaDoc;
29 import java.text.MessageFormat JavaDoc;
30 import javax.management.ObjectName JavaDoc;
31 import javax.management.MBeanServerFactory JavaDoc;
32 import javax.management.MBeanServer JavaDoc;
33 import javax.management.j2ee.statistics.CountStatistic JavaDoc;
34 import javax.management.j2ee.statistics.Statistic JavaDoc;
35 import com.sun.logging.LogDomains;
36 import com.sun.enterprise.admin.monitor.stats.PWCConnectionQueueStats;
37 import com.sun.enterprise.admin.monitor.stats.MutableCountStatistic;
38 import com.sun.enterprise.admin.monitor.stats.MutableCountStatisticImpl;
39 import com.sun.enterprise.admin.monitor.stats.GenericStatsImpl;
40 import com.sun.enterprise.admin.monitor.stats.CountStatisticImpl;
41 import com.sun.enterprise.admin.monitor.stats.StringStatistic;
42 import com.sun.enterprise.admin.monitor.stats.StringStatisticImpl;
43
44 /**
45  * Implementation of PWCConnectionQueueStats interface.
46  */

47 public class PWCConnectionQueueStatsImpl implements PWCConnectionQueueStats {
48
49     private static Logger JavaDoc _logger
50         = LogDomains.getLogger(LogDomains.WEB_LOGGER);
51
52     private GenericStatsImpl baseStatsImpl;
53
54     private MBeanServer JavaDoc server;
55     private ObjectName JavaDoc connectionQueueName;
56
57     private StringStatistic id;
58     private MutableCountStatistic countTotalConnections;
59     private MutableCountStatistic countQueued;
60     private MutableCountStatistic peakQueued;
61     private MutableCountStatistic maxQueued;
62     private MutableCountStatistic countOverflows;
63     private MutableCountStatistic countTotalQueued;
64     private MutableCountStatistic ticksTotalQueued;
65     private MutableCountStatistic countQueued1MinuteAverage;
66     private MutableCountStatistic countQueued5MinuteAverage;
67     private MutableCountStatistic countQueued15MinuteAverage;
68
69
70     /**
71      * Constructor.
72      *
73      * @param domain Domain name
74      */

75     public PWCConnectionQueueStatsImpl(String JavaDoc domain) {
76        
77         baseStatsImpl = new GenericStatsImpl(
78             com.sun.enterprise.admin.monitor.stats.PWCConnectionQueueStats.class,
79             this);
80         
81         // get an instance of the MBeanServer
82
ArrayList JavaDoc servers = MBeanServerFactory.findMBeanServer(null);
83         if(!servers.isEmpty())
84             server = (MBeanServer JavaDoc)servers.get(0);
85         else
86             server = MBeanServerFactory.createMBeanServer();
87         
88         String JavaDoc objNameStr = domain + ":type=PWCConnectionQueue,*";
89         try {
90             connectionQueueName = new ObjectName JavaDoc(objNameStr);
91         } catch (Throwable JavaDoc t) {
92             String JavaDoc msg = _logger.getResourceBundle().getString(
93                                     "webcontainer.objectNameCreationError");
94             msg = MessageFormat.format(msg, new Object JavaDoc[] { objNameStr });
95             _logger.log(Level.SEVERE, msg, t);
96         }
97
98         // initialize all the MutableStatistic Classes
99
initializeStatistics();
100     }
101
102
103     /**
104      * Gets the ID of the connection queue
105      *
106      * @return The ID of the connection queue
107      */

108     public StringStatistic getId() {
109         return id;
110     }
111
112
113     /**
114      * Gets the total number of connections that have been accepted.
115      *
116      * @return Total number of connections that have been accepted.
117      */

118     public CountStatistic JavaDoc getCountTotalConnections() {
119         countTotalConnections.setCount(
120             StatsUtil.getAggregateStatistic(server, connectionQueueName,
121                                             "countTotalConnections"));
122         return (CountStatistic JavaDoc)countTotalConnections.unmodifiableView();
123     }
124     
125
126     /**
127      * Gets the number of connections currently in the queue
128      *
129      * @return Number of connections currently in the queue
130      */

131     public CountStatistic JavaDoc getCountQueued() {
132         countQueued.setCount(
133             StatsUtil.getAggregateStatistic(server, connectionQueueName,
134                                             "countQueued"));
135         return (CountStatistic JavaDoc)countQueued.unmodifiableView();
136     }
137
138     
139     /**
140      * Gets the largest number of connections that were in the queue
141      * simultaneously.
142      *
143      * @return Largest number of connections that were in the queue
144      * simultaneously
145      */

146     public CountStatistic JavaDoc getPeakQueued() {
147         peakQueued.setCount(
148             StatsUtil.getAggregateStatistic(server, connectionQueueName,
149                                             "peakQueued"));
150         return (CountStatistic JavaDoc)peakQueued.unmodifiableView();
151     }
152
153     
154     /**
155      * Gets the maximum size of the connection queue
156      *
157      * @return Maximum size of the connection queue
158      */

159     public CountStatistic JavaDoc getMaxQueued() {
160         maxQueued.setCount(
161             StatsUtil.getConstant(server, connectionQueueName, "maxQueued"));
162         return (CountStatistic JavaDoc)maxQueued.unmodifiableView();
163     }
164
165     
166     /**
167      * Gets the number of times the queue has been too full to accommodate
168      * a connection
169      *
170      * @return Number of times the queue has been too full to accommodate
171      * a connection
172      */

173     public CountStatistic JavaDoc getCountOverflows() {
174         countOverflows.setCount(
175             StatsUtil.getAggregateStatistic(server, connectionQueueName,
176                                             "countOverflows"));
177         return (CountStatistic JavaDoc)countOverflows.unmodifiableView();
178     }
179
180
181     /**
182      * Gets the total number of connections that have been queued.
183      *
184      * A given connection may be queued multiple times, so
185      * <code>counttotalqueued</code> may be greater than or equal to
186      * <code>counttotalconnections</code>.
187      *
188      * @return Total number of connections that have been queued
189      */

190     public CountStatistic JavaDoc getCountTotalQueued() {
191         countTotalQueued.setCount(
192             StatsUtil.getAggregateStatistic(server, connectionQueueName,
193                                             "countTotalQueued"));
194         return (CountStatistic JavaDoc)countTotalQueued.unmodifiableView();
195     }
196
197
198     /**
199      * Gets the total number of ticks that connections have spent in the
200      * queue.
201      *
202      * A tick is a system-dependent unit of time.
203      *
204      * @return Total number of ticks that connections have spent in the
205      * queue
206      */

207     public CountStatistic JavaDoc getTicksTotalQueued() {
208         ticksTotalQueued.setCount(
209             StatsUtil.getAverageStatistic(server, connectionQueueName,
210                                             "ticksTotalQueued"));
211         return (CountStatistic JavaDoc)ticksTotalQueued.unmodifiableView();
212     }
213     
214
215     /**
216      * Gets the average number of connections queued in the last 1 minute
217      *
218      * @return Average number of connections queued in the last 1 minute
219      */

220     public CountStatistic JavaDoc getCountQueued1MinuteAverage() {
221         countQueued1MinuteAverage.setCount(
222             StatsUtil.getAverageStatistic(server, connectionQueueName,
223                                           "countQueued1MinuteAverage"));
224         return (CountStatistic JavaDoc)countQueued1MinuteAverage.unmodifiableView();
225     }
226
227
228     /**
229      * Gets the average number of connections queued in the last 5 minutes
230      *
231      * @return Average number of connections queued in the last 5 minutes
232      */

233     public CountStatistic JavaDoc getCountQueued5MinuteAverage() {
234         countQueued5MinuteAverage.setCount(
235             StatsUtil.getAverageStatistic(server, connectionQueueName,
236                                           "countQueued5MinuteAverage"));
237         return (CountStatistic JavaDoc)countQueued5MinuteAverage.unmodifiableView();
238     }
239
240
241     /**
242      * Gets the average number of connections queued in the last 15 minutes
243      *
244      * @return Average number of connections queued in the last 15 minutes
245      */

246     public CountStatistic JavaDoc getCountQueued15MinuteAverage() {
247         countQueued15MinuteAverage.setCount(
248             StatsUtil.getAverageStatistic(server, connectionQueueName,
249                                           "countQueued15MinuteAverage"));
250         return (CountStatistic JavaDoc)countQueued15MinuteAverage.unmodifiableView();
251     }
252
253     
254     /**
255      * This method can be used to retrieve all the Statistics, exposed
256      * by this implementation of Stats
257      * @return Statistic[]
258      */

259     public Statistic JavaDoc[] getStatistics() {
260         return baseStatsImpl.getStatistics();
261     }
262     
263
264     /**
265      * Queries for a statistic with the given name.
266      *
267      * @name Name of the statistic to query for
268      *
269      * @return Statistic for the given name
270      */

271     public Statistic JavaDoc getStatistic(String JavaDoc name) {
272         return baseStatsImpl.getStatistic(name);
273     }
274
275     
276     /**
277      * Gets array of all statistic names exposed by this implementation of
278      * <codeStats</code>
279      *
280      * @return Array of statistic names
281      */

282     public String JavaDoc[] getStatisticNames() {
283         return baseStatsImpl.getStatisticNames();
284     }
285
286     
287     private void initializeStatistics() {
288
289         long startTime = System.currentTimeMillis();
290         id = new StringStatisticImpl("",
291                                      "Id",
292                                      "String",
293                                      "ID of the connection queue",
294                                      startTime,
295                                      startTime);
296
297         CountStatistic JavaDoc c = new CountStatisticImpl("CountTotalConnections");
298         countTotalConnections = new MutableCountStatisticImpl(c);
299
300         c = new CountStatisticImpl("CountQueued");
301         countQueued = new MutableCountStatisticImpl(c);
302
303         c = new CountStatisticImpl("PeakQueued");
304         peakQueued = new MutableCountStatisticImpl(c);
305
306         c = new CountStatisticImpl("MaxQueued");
307         maxQueued = new MutableCountStatisticImpl(c);
308
309         c = new CountStatisticImpl("CountOverflows");
310         countOverflows = new MutableCountStatisticImpl(c);
311
312         c = new CountStatisticImpl("CountTotalQueued");
313         countTotalQueued = new MutableCountStatisticImpl(c);
314
315         c = new CountStatisticImpl("TicksTotalQueued");
316         ticksTotalQueued = new MutableCountStatisticImpl(c);
317
318         c = new CountStatisticImpl("CountQueued1MinuteAverage");
319         countQueued1MinuteAverage = new MutableCountStatisticImpl(c);
320
321         c = new CountStatisticImpl("CountQueued5MinuteAverage");
322         countQueued5MinuteAverage = new MutableCountStatisticImpl(c);
323
324         c = new CountStatisticImpl("CountQueued15MinuteAverage");
325         countQueued15MinuteAverage = new MutableCountStatisticImpl(c);
326     }
327
328 }
329
Popular Tags