001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)MessageServiceStatistics.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.messaging;
030:
031: import java.util.Date;
032:
033: import javax.management.AttributeChangeNotification;
034: import javax.management.Notification;
035: import javax.management.NotificationListener;
036: import javax.management.openmbean.CompositeData;
037:
038: import javax.xml.namespace.QName;
039:
040: import com.sun.jbi.monitoring.StatisticsBase;
041: import com.sun.jbi.management.config.SystemConfigurationFactory;
042:
043: /**
044: * This class implements the MBean for collection of statistics for the
045: * messaging service. All statistics are since the last message service startup;
046: * they are all reset when the message service is restarted.
047: *
048: * @author Sun Microsystems, Inc.
049: */
050: public class MessageServiceStatistics implements
051: MessageServiceStatisticsMBean, NotificationListener {
052: private MessageService mMsgSvc;
053:
054: /**
055: * Instance of StatisticsBase for manipulating the object tree.
056: */
057: private StatisticsBase mStatisticsBase;
058:
059: /**
060: * Time the message service was last successfully started.
061: */
062: private Date mLastRestartTime;
063:
064: /**
065: * Count of current number of services registered with the message service.
066: */
067: private int mRegisteredServices;
068:
069: /**
070: * Count of current number of endpoints registered with the message service.
071: */
072: private int mRegisteredEndpoints;
073:
074: /**
075: * Instance of MessagingStatistics for holding messaging statistics.
076: */
077: private MessagingStatistics mMessagingStatistics;
078:
079: /**
080: * Constant used to compute percentages.
081: */
082: private static final int ONE_HUNDRED = 100;
083:
084: /**
085: * Constant used to compute rates.
086: */
087: private static final int MILLISECONDS_PER_HOUR = 3600000;
088:
089: /**
090: * Constructor to create the StatisticsBase and MessagingStatistics
091: * instances.
092: * @param key the string value to use as the key for this statistics
093: * instance.
094: */
095: MessageServiceStatistics(MessageService msgSvc, String key) {
096: mMsgSvc = msgSvc;
097: mStatisticsBase = new com.sun.jbi.util.monitoring.StatisticsBaseImpl(
098: key);
099: mMessagingStatistics = new MessagingStatistics();
100: }
101:
102: //
103: // Methods defined in StatisticsMBean must delegate to StatisticsBase.
104: //
105:
106: /**
107: * Disable statistics collection. This method causes collection for this
108: * object and all its child objects to be disabled.
109: */
110: public void setDisabled() {
111: mMsgSvc.disableStatistics();
112: }
113:
114: public void disableTimingStatistics() {
115: mMsgSvc.disableStatistics();
116: }
117:
118: /**
119: * Enable statistics collection. This method causes collection for this
120: * object and all its child objects to be enabled.
121: */
122: public boolean isEnabled() {
123: return (mMsgSvc.areStatisticsEnabled());
124: }
125:
126: public void setEnabled() {
127: mMsgSvc.enableStatistics();
128: }
129:
130: public void enableTimingStatistics() {
131: mMsgSvc.enableStatistics();
132: }
133:
134: //
135: // Methods defined in the MessageServiceStatisticsMBean interface. These
136: // methods provide all the MBean attributes visible to JMX clients.
137: //
138:
139: /**
140: * Get the time that the message service was last started.
141: * @return The time of the last successful start() call.
142: */
143: public Date getLastRestartTime() {
144: return mLastRestartTime;
145: }
146:
147: /**
148: * Get the current number of services registered with the message service.
149: * @return The total number of registered services.
150: */
151: public int getRegisteredServices() {
152: return mRegisteredServices;
153: }
154:
155: /**
156: * Get the current number of endpoints registered with the message service.
157: * @return The total number of registered endpoints.
158: */
159: public int getRegisteredEndpoints() {
160: return mRegisteredEndpoints;
161: }
162:
163: /** Returns the identifiers of all the active channels.
164: * @return names of all the active channels.
165: */
166: public String[] getActiveChannels() {
167: return (mMsgSvc.getChannelNames());
168: }
169:
170: /** Returns a list of active endpoints in the NMR.
171: * @return list of activated endpoints
172: */
173: public String[] getActiveEndpoints() {
174: return (mMsgSvc.getEndpointNames());
175: }
176:
177: /**
178: * Get the list of endpoints for a specific DeliveryChannel.
179: * @return The delivery channel statistics in a CompositedData instance
180: */
181: public String[] getEndpointsForDeliveryChannel(String dcName) {
182: return (mMsgSvc.getActiveEndpoints(dcName));
183: }
184:
185: /**
186: * Get the list of consuming endpoints for a specific DeliveryChannel.
187: * @return The delivery channel statistics in a CompositedData instance
188: */
189: public String[] getConsumingEndpointsForDeliveryChannel(
190: String dcName) {
191: return (mMsgSvc.getActiveConsumingEndpoints(dcName));
192: }
193:
194: /**
195: * Get the CompositeData instance that represents the current values for
196: * the MessagingStatistics instance.
197: * @return The messaging statistics in a CompositedData instance
198: */
199: public CompositeData getMessagingStatistics() {
200: CompositeData mscd = null;
201: try {
202: mscd = mMessagingStatistics.toCompositeData();
203: } catch (javax.management.openmbean.OpenDataException odEx) {
204: odEx.printStackTrace();
205: }
206: return mscd;
207: }
208:
209: /**
210: * Get the CompositeData instance that represents the current values for
211: * the specific DeliveryChannel.
212: * @return The delivery channel statistics in a CompositedData instance
213: */
214: public CompositeData getDeliveryChannelStatistics(String dcName) {
215: CompositeData mscd = null;
216: ChannelStatistics cs = mMsgSvc.getChannelStatistics(dcName);
217:
218: if (cs != null) {
219: mscd = cs.getStatistics();
220: }
221: return mscd;
222: }
223:
224: /**
225: * Get the CompositeData instance that represents the current values for
226: * the specific Endpoint.
227: * @return The endpoint statistics in a CompositedData instance
228: */
229: public CompositeData getEndpointStatistics(String epName) {
230: CompositeData mscd = null;
231: EndpointStatistics es = mMsgSvc.getEndpointStatistics(epName);
232:
233: if (es != null) {
234: mscd = es.getStatistics();
235: }
236: return mscd;
237: }
238:
239: //
240: // Methods used only within the message service code to set statistics
241: // values.
242: //
243:
244: /**
245: * Get the MessagingStatistics instance.
246: * @return the MessagingStatisticsBase instance for this object.
247: */
248: MessagingStatistics getMessagingStatisticsInstance() {
249: return mMessagingStatistics;
250: }
251:
252: /**
253: * Get the StatisticsBase instance.
254: * @return the StatisticsBase instance for this object.
255: */
256: StatisticsBase getStatisticsBase() {
257: return mStatisticsBase;
258: }
259:
260: /**
261: * Set the time that this component was last started.
262: * @param startTime The time of the last successful start() call.
263: */
264: void setLastRestartTime(Date startTime) {
265: mLastRestartTime = startTime;
266: mMessagingStatistics.setLastRestartTime(startTime);
267: }
268:
269: /**
270: * Decrement the current number of endpoints registered with the message
271: * service.
272: */
273: void decrementRegisteredEndpoints() {
274: --mRegisteredEndpoints;
275: }
276:
277: /**
278: * Increment the current number of endpoints registered with the message
279: * service.
280: */
281: void incrementRegisteredEndpoints() {
282: ++mRegisteredEndpoints;
283: }
284:
285: /**
286: * Decrement the current number of services registered with the message
287: * service.
288: */
289: void decrementRegisteredServices() {
290: --mRegisteredServices;
291: }
292:
293: /**
294: * Increment the current number of services registered with the message
295: * service.
296: */
297: void incrementRegisteredServices() {
298: ++mRegisteredServices;
299: }
300:
301: /**
302: * Reset all statistics.
303: */
304: void resetStatistics() {
305: mRegisteredServices = 0;
306: mRegisteredEndpoints = 0;
307: mMessagingStatistics.resetStatistics();
308: }
309:
310: //
311: // Notification Listener Interface
312: //
313:
314: /**
315: * handle a notification from the Logger Configuration MBean
316: *
317: * @param notification - the notification
318: * @param the callback passed in
319: */
320: public void handleNotification(Notification notification,
321: Object handback) {
322: if (notification instanceof AttributeChangeNotification) {
323: AttributeChangeNotification notif = (AttributeChangeNotification) notification;
324:
325: Boolean enableTimingStatsStr = (Boolean) notif
326: .getNewValue();
327: boolean enableTimingStats = enableTimingStatsStr
328: .booleanValue();
329:
330: if (enableTimingStats) {
331: enableTimingStatistics();
332: } else {
333: disableTimingStatistics();
334: }
335: }
336: }
337: }
|