001: /*
002: * $Id: ServiceStatistics.java 11373 2008-03-15 05:03:10Z dfeist $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.management.stats;
012:
013: import org.mule.api.management.stats.Statistics;
014: import org.mule.management.stats.printers.SimplePrinter;
015:
016: import java.io.PrintWriter;
017:
018: public class ServiceStatistics implements Statistics {
019: /**
020: * Serial version
021: */
022: private static final long serialVersionUID = -2086999226732861674L;
023:
024: private String name;
025: private long receivedEventSync = 0;
026: private long receivedEventASync = 0;
027: private long queuedEvent = 0;
028: private long maxQueuedEvent = 0;
029: private long averageQueueSize = 0;
030: private long totalQueuedEvent = 0;
031: private long executionError = 0;
032: private long fatalError = 0;
033:
034: private int threadPoolSize = 0;
035: private long samplePeriod = 0;
036: private boolean enabled = false;
037:
038: private RouterStatistics inboundRouterStat = null;
039: private ComponentStatistics componentStat = null;
040: private RouterStatistics outboundRouterStat = null;
041:
042: public ServiceStatistics(String name) {
043: this (name, 0);
044: }
045:
046: /**
047: * The constructor
048: *
049: * @param name
050: */
051: public ServiceStatistics(String name, int threadPoolSize) {
052: super ();
053: this .name = name;
054:
055: this .threadPoolSize = threadPoolSize;
056: clear();
057: }
058:
059: /**
060: * Are statistics logged
061: */
062: public boolean isEnabled() {
063: return enabled;
064: }
065:
066: /**
067: * Enable statistics logs (this is a dynamic parameter)
068: */
069: public synchronized void setEnabled(boolean b) {
070: enabled = b;
071:
072: if (inboundRouterStat != null) {
073: inboundRouterStat.setEnabled(b);
074: }
075: if (componentStat != null) {
076: componentStat.setEnabled(b);
077: }
078: if (outboundRouterStat != null) {
079: outboundRouterStat.setEnabled(b);
080: }
081: }
082:
083: public synchronized void incReceivedEventSync() {
084: receivedEventSync++;
085: }
086:
087: public synchronized void incReceivedEventASync() {
088: receivedEventASync++;
089: }
090:
091: public synchronized void incExecutionError() {
092: executionError++;
093: }
094:
095: public synchronized void incFatalError() {
096: fatalError++;
097: }
098:
099: public synchronized void incQueuedEvent() {
100: queuedEvent++;
101: totalQueuedEvent++;
102: if (queuedEvent > maxQueuedEvent) {
103: maxQueuedEvent = queuedEvent;
104: }
105: // if(queuedEvent > 1) {
106: averageQueueSize = Math.round(getAsyncEventsReceived()
107: / totalQueuedEvent);
108: // }
109: }
110:
111: public synchronized void decQueuedEvent() {
112: queuedEvent--;
113: }
114:
115: public long getAverageExecutionTime() {
116: return componentStat.getAverageExecutionTime();
117: }
118:
119: public long getAverageQueueSize() {
120: return averageQueueSize;
121: }
122:
123: public long getMaxQueueSize() {
124: return maxQueuedEvent;
125: }
126:
127: /**
128: * @deprecated
129: * @return
130: */
131: public long getMaxExecutionTime() {
132: return componentStat.getMaxExecutionTime();
133: }
134:
135: public long getFatalErrors() {
136: return fatalError;
137: }
138:
139: /**
140: * @deprecated
141: * @return
142: */
143: public long getMinExecutionTime() {
144: return componentStat.getMinExecutionTime();
145: }
146:
147: /**
148: * @deprecated
149: * @return
150: */
151: public long getTotalExecutionTime() {
152: return componentStat.getTotalExecutionTime();
153: }
154:
155: public long getQueuedEvents() {
156: return queuedEvent;
157: }
158:
159: public long getAsyncEventsReceived() {
160: return receivedEventASync;
161: }
162:
163: public long getSyncEventsReceived() {
164: return receivedEventSync;
165: }
166:
167: public long getReplyToEventsSent() {
168: return componentStat.getReplyToEventsSent();
169: }
170:
171: public long getSyncEventsSent() {
172: return componentStat.getSyncEventsSent();
173: }
174:
175: public long getAsyncEventsSent() {
176: return componentStat.getAsyncEventsSent();
177: }
178:
179: public long getTotalEventsSent() {
180: return getSyncEventsSent() + getAsyncEventsSent();
181: }
182:
183: public long getTotalEventsReceived() {
184: return getSyncEventsReceived() + getAsyncEventsReceived();
185: }
186:
187: public long getExecutedEvents() {
188: return componentStat.getExecutedEvents();
189: }
190:
191: public long getExecutionErrors() {
192: return executionError;
193: }
194:
195: public synchronized String getName() {
196: return name;
197: }
198:
199: public synchronized void setName(String name) {
200: this .name = name;
201: }
202:
203: /**
204: * log in info level the main statistics
205: */
206: public void logSummary() {
207: logSummary(new SimplePrinter(System.out));
208: }
209:
210: public void logSummary(PrintWriter printer) {
211: printer.print(this );
212: }
213:
214: public synchronized void clear() {
215: receivedEventSync = 0;
216: receivedEventASync = 0;
217: queuedEvent = 0;
218: maxQueuedEvent = 0;
219: totalQueuedEvent = 0;
220: averageQueueSize = 0;
221:
222: executionError = 0;
223: fatalError = 0;
224:
225: if (getInboundRouterStat() != null) {
226: getInboundRouterStat().clear();
227: }
228: if (getOutboundRouterStat() != null) {
229: getOutboundRouterStat().clear();
230: }
231:
232: samplePeriod = System.currentTimeMillis();
233:
234: }
235:
236: /**
237: * @return Returns the inboundRouterStat.
238: */
239: public RouterStatistics getInboundRouterStat() {
240: return inboundRouterStat;
241: }
242:
243: /**
244: * @param inboundRouterStat The inboundRouterStat to set.
245: */
246: public void setInboundRouterStat(RouterStatistics inboundRouterStat) {
247: this .inboundRouterStat = inboundRouterStat;
248: this .inboundRouterStat.setEnabled(enabled);
249: }
250:
251: /**
252: * @return Returns the outboundRouterStat.
253: */
254: public RouterStatistics getOutboundRouterStat() {
255: return outboundRouterStat;
256: }
257:
258: /**
259: * @param outboundRouterStat The outboundRouterStat to set.
260: */
261: public void setOutboundRouterStat(
262: RouterStatistics outboundRouterStat) {
263: this .outboundRouterStat = outboundRouterStat;
264: this .outboundRouterStat.setEnabled(enabled);
265: }
266:
267: /**
268: * @return Returns the outboundRouterStat.
269: */
270: public ComponentStatistics getComponentStat() {
271: return componentStat;
272: }
273:
274: /**
275: * @param outboundRouterStat The outboundRouterStat to set.
276: */
277: public void setComponentStat(ComponentStatistics componentStat) {
278: this .componentStat = componentStat;
279: this .componentStat.setEnabled(enabled);
280: }
281:
282: public int getThreadPoolSize() {
283: return threadPoolSize;
284: }
285:
286: public long getSamplePeriod() {
287: return System.currentTimeMillis() - samplePeriod;
288: }
289: }
|