KickJava   Java API By Example, From Geeks To Geeks.

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


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 package com.sun.enterprise.web.stats;
24
25 import java.util.ArrayList JavaDoc;
26 import java.util.logging.Logger JavaDoc;
27 import java.util.logging.Level JavaDoc;
28 import java.text.MessageFormat JavaDoc;
29 import javax.management.ObjectName JavaDoc;
30 import javax.management.MBeanServerFactory JavaDoc;
31 import javax.management.MBeanServer JavaDoc;
32 import javax.management.j2ee.statistics.CountStatistic JavaDoc;
33 import javax.management.j2ee.statistics.Statistic JavaDoc;
34 import com.sun.logging.LogDomains;
35 import com.sun.enterprise.admin.monitor.stats.StringStatistic;
36 import com.sun.enterprise.admin.monitor.stats.StringStatisticImpl;
37 import com.sun.enterprise.admin.monitor.stats.PWCThreadPoolStats;
38 import com.sun.enterprise.admin.monitor.stats.MutableCountStatistic;
39 import com.sun.enterprise.admin.monitor.stats.MutableCountStatisticImpl;
40 import com.sun.enterprise.admin.monitor.stats.GenericStatsImpl;
41 import com.sun.enterprise.admin.monitor.stats.CountStatisticImpl;
42
43 /**
44  * @author Jan Luehe
45  */

46 public class PWCThreadPoolStatsImpl implements PWCThreadPoolStats {
47
48     private static Logger JavaDoc _logger
49         = LogDomains.getLogger(LogDomains.WEB_LOGGER);
50
51     private GenericStatsImpl baseStatsImpl;
52
53     private MBeanServer JavaDoc server;
54     private ObjectName JavaDoc threadPoolName;
55
56     private StringStatistic id;
57     private MutableCountStatistic countThreadsIdle;
58     private MutableCountStatistic countThreads;
59     private MutableCountStatistic maxThreads;
60     private MutableCountStatistic countQueued;
61     private MutableCountStatistic peakQueued;
62     private MutableCountStatistic maxQueued;
63     
64
65     public PWCThreadPoolStatsImpl(String JavaDoc domain) {
66        
67         baseStatsImpl = new GenericStatsImpl(
68             com.sun.enterprise.admin.monitor.stats.PWCThreadPoolStats.class,
69             this);
70         
71         // get an instance of the MBeanServer
72
ArrayList JavaDoc servers = MBeanServerFactory.findMBeanServer(null);
73         if(!servers.isEmpty())
74             server = (MBeanServer JavaDoc)servers.get(0);
75         else
76             server = MBeanServerFactory.createMBeanServer();
77         
78         String JavaDoc objNameStr = domain + ":type=Selector,*";
79         try {
80             threadPoolName = new ObjectName JavaDoc(objNameStr);
81         } catch (Throwable JavaDoc t) {
82             String JavaDoc msg = _logger.getResourceBundle().getString(
83                                     "webcontainer.objectNameCreationError");
84             msg = MessageFormat.format(msg, new Object JavaDoc[] { objNameStr });
85             _logger.log(Level.SEVERE, msg, t);
86         }
87
88         // initialize all the MutableStatistic Classes
89
initializeStatistics();
90     }
91
92
93     /**
94      * Gets the ID of the thread pool.
95      *
96      * @return ID of the thread pool
97      */

98     public StringStatistic getId() {
99         return id;
100     }
101     
102
103     /**
104      * Gets the number of threads that are currently idle.
105      *
106      * @return Number of currently idle threads
107      */

108     public CountStatistic JavaDoc getCountThreadsIdle() {
109         countThreadsIdle.setCount(
110             StatsUtil.getAggregateStatistic(server, threadPoolName,
111                                             "countThreadsIdleStats"));
112         return (CountStatistic JavaDoc)countThreadsIdle.unmodifiableView();
113     }
114
115     
116     /**
117      * Gets the current number of threads.
118      *
119      * @return Current number of threads
120      */

121     public CountStatistic JavaDoc getCountThreads() {
122         countThreads.setCount(
123             StatsUtil.getAggregateStatistic(server, threadPoolName,
124                                             "countThreadsStats"));
125         return (CountStatistic JavaDoc)countThreads.unmodifiableView();
126     }
127     
128
129     /**
130      * Gets the maximum number of threads allowed in the thread pool.
131      *
132      * @return Maximum number of threads allowed in the thread pool
133      */

134     public CountStatistic JavaDoc getMaxThreads() {
135         maxThreads.setCount(StatsUtil.getAggregateStatistic(server,
136                                                             threadPoolName,
137                                                             "maxThreadsStats"));
138         return (CountStatistic JavaDoc)maxThreads.unmodifiableView();
139     }
140
141     
142     /**
143      * Gets the current number of requests waiting for a thread.
144      *
145      * @return Number of requests waiting for a thread
146      */

147     public CountStatistic JavaDoc getCountQueued() {
148         return null;
149     }
150
151     
152     /**
153      * Gets the largest number of requests that were ever queued up
154      * simultaneously for the use of a thread since the server was started.
155      *
156      * @return Largest number of requests that were ever queued up waiting
157      * for a thread
158      */

159     public CountStatistic JavaDoc getPeakQueued() {
160         return null;
161     }
162
163     
164     /**
165      * Gets the maximum number of requests that may be queued up
166      *
167      * @return Maximum number of requests that may be queued up
168      */

169     public CountStatistic JavaDoc getMaxQueued() {
170         return null;
171     }
172     
173     
174     /**
175      * This method can be used to retrieve all the Statistics, exposed
176      * by this implementation of Stats
177      * @return Statistic[]
178      */

179     public Statistic JavaDoc[] getStatistics() {
180         return baseStatsImpl.getStatistics();
181     }
182     
183
184     /**
185      * Queries for a statistic with the given name.
186      *
187      * @name Name of the statistic to query for
188      *
189      * @return Statistic for the given name
190      */

191     public Statistic JavaDoc getStatistic(String JavaDoc name) {
192         return baseStatsImpl.getStatistic(name);
193     }
194
195     
196     /**
197      * Gets array of all statistic names exposed by this implementation of
198      * <codeStats</code>
199      *
200      * @return Array of statistic names
201      */

202     public String JavaDoc[] getStatisticNames() {
203         return baseStatsImpl.getStatisticNames();
204     }
205
206
207     private void initializeStatistics() {
208         
209         long startTime = System.currentTimeMillis();
210         id = new StringStatisticImpl("",
211                                      "id",
212                                      "String",
213                                      "ID of the thread pool",
214                                      startTime,
215                                      startTime);
216
217         CountStatistic JavaDoc c = new CountStatisticImpl("CountThreadsIdle");
218         countThreadsIdle = new MutableCountStatisticImpl(c);
219
220         c = new CountStatisticImpl("CountThreads");
221         countThreads = new MutableCountStatisticImpl(c);
222
223         c = new CountStatisticImpl("MaxThreads");
224         maxThreads = new MutableCountStatisticImpl(c);
225
226         c = new CountStatisticImpl("CountQueued");
227         countQueued = new MutableCountStatisticImpl(c);
228
229         c = new CountStatisticImpl("PeakQueued");
230         peakQueued = new MutableCountStatisticImpl(c);
231
232         c = new CountStatisticImpl("MaxQueued");
233         maxQueued = new MutableCountStatisticImpl(c);
234     }
235     
236 }
237
Popular Tags