View Javadoc

1   /*
2    * $Id: ServiceStatistics.java 11488 2008-03-24 14:38:49Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.management.stats;
12  
13  import org.mule.api.management.stats.Statistics;
14  import org.mule.management.stats.printers.SimplePrinter;
15  
16  import java.io.PrintWriter;
17  
18  public class ServiceStatistics implements Statistics
19  {
20      /**
21       * Serial version
22       */
23      private static final long serialVersionUID = -2086999226732861674L;
24  
25      private String name;
26      private long receivedEventSync = 0;
27      private long receivedEventASync = 0;
28      private long queuedEvent = 0;
29      private long maxQueuedEvent = 0;
30      private long averageQueueSize = 0;
31      private long totalQueuedEvent = 0;
32      private long sentEventSync = 0;
33      private long sentReplyToEvent = 0;
34      private long sentEventASync = 0;
35      private long executionError = 0;
36      private long fatalError = 0;
37  
38      private int threadPoolSize = 0;
39      private long samplePeriod = 0;
40      private boolean enabled = false;
41      
42      private RouterStatistics inboundRouterStat = null;
43      private ComponentStatistics componentStat = null;
44      private RouterStatistics outboundRouterStat = null;
45  
46      public ServiceStatistics(String name)
47      {
48          this(name, 0);
49      }
50  
51      /**
52       * The constructor
53       *
54       * @param name
55       */
56      public ServiceStatistics(String name, int threadPoolSize)
57      {
58          super();
59          this.name = name;
60  
61          this.threadPoolSize = threadPoolSize;
62          clear();
63      }
64  
65      /**
66       * Are statistics logged
67       */
68      public boolean isEnabled()
69      {
70          return enabled;
71      }
72  
73      /**
74       * Enable statistics logs (this is a dynamic parameter)
75       */
76      public synchronized void setEnabled(boolean b)
77      {
78          enabled = b;
79  
80          if (inboundRouterStat != null)
81          {
82              inboundRouterStat.setEnabled(b);
83          }
84          if (componentStat != null)
85          {
86              componentStat.setEnabled(b);
87          }
88          if (outboundRouterStat != null)
89          {
90              outboundRouterStat.setEnabled(b);
91          }
92      }
93  
94      public synchronized void incReceivedEventSync()
95      {
96          receivedEventSync++;
97      }
98  
99      public synchronized void incReceivedEventASync()
100     {
101         receivedEventASync++;
102     }
103 
104     public synchronized void incExecutionError()
105     {
106         executionError++;
107     }
108 
109     public synchronized void incFatalError()
110     {
111         fatalError++;
112     }
113 
114     public synchronized void incSentEventSync()
115     {
116         sentEventSync++;
117     }
118 
119     public synchronized void incSentEventASync()
120     {
121         sentEventASync++;
122     }
123 
124     public synchronized void incSentReplyToEvent()
125     {
126         sentReplyToEvent++;
127     }
128 
129     public synchronized void incQueuedEvent()
130     {
131         queuedEvent++;
132         totalQueuedEvent++;
133         if (queuedEvent > maxQueuedEvent)
134         {
135             maxQueuedEvent = queuedEvent;
136         }
137         // if(queuedEvent > 1) {
138         averageQueueSize = Math.round(getAsyncEventsReceived() / totalQueuedEvent);
139         // }
140     }
141 
142     public synchronized void decQueuedEvent()
143     {
144         queuedEvent--;
145     }
146 
147     public long getAverageExecutionTime()
148     {
149         return componentStat.getAverageExecutionTime();
150     }
151 
152     public long getAverageQueueSize()
153     {
154         return averageQueueSize;
155     }
156 
157     public long getMaxQueueSize()
158     {
159         return maxQueuedEvent;
160     }
161 
162     /**
163      * @deprecated
164      * @return
165      */
166     public long getMaxExecutionTime()
167     {
168         return componentStat.getMaxExecutionTime();
169     }
170 
171     public long getFatalErrors()
172     {
173         return fatalError;
174     }
175 
176     /**
177      * @deprecated
178      * @return
179      */
180     public long getMinExecutionTime()
181     {
182         return componentStat.getMinExecutionTime();
183     }
184 
185     /**
186      * @deprecated
187      * @return
188      */
189     public long getTotalExecutionTime()
190     {
191         return componentStat.getTotalExecutionTime();
192     }
193 
194     public long getQueuedEvents()
195     {
196         return queuedEvent;
197     }
198 
199     public long getAsyncEventsReceived()
200     {
201         return receivedEventASync;
202     }
203 
204     public long getSyncEventsReceived()
205     {
206         return receivedEventSync;
207     }
208 
209     public long getReplyToEventsSent()
210     {
211         return sentReplyToEvent;
212     }
213 
214     public long getSyncEventsSent()
215     {
216         return sentEventSync;
217     }
218 
219     public long getAsyncEventsSent()
220     {
221         return sentEventASync;
222     }
223 
224     public long getTotalEventsSent()
225     {
226         return getSyncEventsSent() + getAsyncEventsSent();
227     }
228 
229     public long getTotalEventsReceived()
230     {
231         return getSyncEventsReceived() + getAsyncEventsReceived();
232     }
233 
234     public long getExecutedEvents()
235     {
236         return componentStat.getExecutedEvents();
237     }
238 
239     public long getExecutionErrors()
240     {
241         return executionError;
242     }
243 
244     public synchronized String getName()
245     {
246         return name;
247     }
248 
249     public synchronized void setName(String name)
250     {
251         this.name = name;
252     }
253 
254     /**
255      * log in info level the main statistics
256      */
257     public void logSummary()
258     {
259         logSummary(new SimplePrinter(System.out));
260     }
261 
262     public void logSummary(PrintWriter printer)
263     {
264         printer.print(this);
265     }
266 
267     public synchronized void clear()
268     {
269         receivedEventSync = 0;
270         receivedEventASync = 0;
271         queuedEvent = 0;
272         maxQueuedEvent = 0;
273         totalQueuedEvent = 0;
274         averageQueueSize = 0;
275 
276         sentEventSync = 0;
277         sentEventASync = 0;
278         sentReplyToEvent = 0;
279 
280         executionError = 0;
281         fatalError = 0;
282 
283         if (getInboundRouterStat() != null)
284         {
285             getInboundRouterStat().clear();
286         }
287         if (getOutboundRouterStat() != null)
288         {
289             getOutboundRouterStat().clear();
290         }
291 
292         samplePeriod = System.currentTimeMillis();
293 
294     }
295 
296     /**
297      * @return Returns the inboundRouterStat.
298      */
299     public RouterStatistics getInboundRouterStat()
300     {
301         return inboundRouterStat;
302     }
303 
304     /**
305      * @param inboundRouterStat The inboundRouterStat to set.
306      */
307     public void setInboundRouterStat(RouterStatistics inboundRouterStat)
308     {
309         this.inboundRouterStat = inboundRouterStat;
310         this.inboundRouterStat.setEnabled(enabled);
311     }
312 
313     /**
314      * @return Returns the outboundRouterStat.
315      */
316     public RouterStatistics getOutboundRouterStat()
317     {
318         return outboundRouterStat;
319     }
320 
321     /**
322      * @param outboundRouterStat The outboundRouterStat to set.
323      */
324     public void setOutboundRouterStat(RouterStatistics outboundRouterStat)
325     {
326         this.outboundRouterStat = outboundRouterStat;
327         this.outboundRouterStat.setEnabled(enabled);
328     }
329     
330     /**
331      * @return Returns the outboundRouterStat.
332      */
333     public ComponentStatistics getComponentStat()
334     {
335         return componentStat;
336     }
337 
338     /**
339      * @param outboundRouterStat The outboundRouterStat to set.
340      */
341     public void setComponentStat(ComponentStatistics componentStat)
342     {
343         this.componentStat = componentStat;
344         this.componentStat.setEnabled(enabled);
345     }
346 
347     public int getThreadPoolSize()
348     {
349         return threadPoolSize;
350     }
351 
352     public long getSamplePeriod()
353     {
354         return System.currentTimeMillis() - samplePeriod;
355     }
356 }