View Javadoc

1   /*
2    * $Id: ServiceStatistics.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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 java.io.PrintWriter;
14  
15  import org.mule.management.stats.printers.SimplePrinter;
16  
17  import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicLong;
18  
19  public class ServiceStatistics extends FlowConstructStatistics implements QueueStatistics
20  {
21      private static final long serialVersionUID = -2086999226732861675L;
22  
23      private final AtomicLong sentEventSync = new AtomicLong(0);
24      private final AtomicLong sentReplyToEvent = new AtomicLong(0);
25      private final AtomicLong sentEventASync = new AtomicLong(0);
26      private final AtomicLong executionError = new AtomicLong(0);
27      private final AtomicLong fatalError = new AtomicLong(0);
28  
29      // these can't sensibly converted to AtomicLong as they are processed together
30      // in incQueuedEvent
31      private long queuedEvent = 0;
32      private long maxQueuedEvent = 0;
33      private long averageQueueSize = 0;
34      private long totalQueuedEvent = 0;
35  
36      private int threadPoolSize = 0;
37      private long samplePeriod = 0;
38  
39      private RouterStatistics inboundRouterStat = null;
40      private ComponentStatistics componentStat = null;
41      private RouterStatistics outboundRouterStat = null;
42  
43      public ServiceStatistics(String name)
44      {
45          this(name, 0);
46      }
47  
48      public ServiceStatistics(String name, int threadPoolSize)
49      {
50          super(name);
51  
52          this.threadPoolSize = threadPoolSize;
53          clear();
54      }
55  
56      /**
57       * Enable statistics logs (this is a dynamic parameter)
58       */
59      @Override
60      public synchronized void setEnabled(boolean b)
61      {
62          super.setEnabled(b);
63  
64          if (inboundRouterStat != null)
65          {
66              inboundRouterStat.setEnabled(b);
67          }
68          if (componentStat != null)
69          {
70              componentStat.setEnabled(b);
71          }
72          if (outboundRouterStat != null)
73          {
74              outboundRouterStat.setEnabled(b);
75          }
76      }
77  
78      public void incExecutionError()
79      {
80          executionError.addAndGet(1);
81      }
82  
83      public void incFatalError()
84      {
85          fatalError.addAndGet(1);
86      }
87  
88      public void incSentEventSync()
89      {
90          sentEventSync.addAndGet(1);
91      }
92  
93      public void incSentEventASync()
94      {
95          sentEventASync.addAndGet(1);
96      }
97  
98      public void incSentReplyToEvent()
99      {
100         sentReplyToEvent.addAndGet(1);
101     }
102 
103     public synchronized void incQueuedEvent()
104     {
105         queuedEvent++;
106         totalQueuedEvent++;
107         if (queuedEvent > maxQueuedEvent)
108         {
109             maxQueuedEvent = queuedEvent;
110         }
111         averageQueueSize = receivedEventASync.get() / totalQueuedEvent;
112     }
113 
114     public synchronized void decQueuedEvent()
115     {
116         queuedEvent--;
117     }
118 
119     public long getAverageExecutionTime()
120     {
121         return componentStat.getAverageExecutionTime();
122     }
123 
124     public synchronized long getAverageQueueSize()
125     {
126         return averageQueueSize;
127     }
128 
129     public synchronized long getMaxQueueSize()
130     {
131         return maxQueuedEvent;
132     }
133 
134     /**
135      * @deprecated
136      */
137     @Deprecated
138     public long getMaxExecutionTime()
139     {
140         return componentStat.getMaxExecutionTime();
141     }
142 
143     public long getFatalErrors()
144     {
145         return fatalError.get();
146     }
147 
148     /**
149      * @deprecated
150      */
151     @Deprecated
152     public long getMinExecutionTime()
153     {
154         return componentStat.getMinExecutionTime();
155     }
156 
157     /**
158      * @deprecated
159      */
160     @Deprecated
161     public long getTotalExecutionTime()
162     {
163         return componentStat.getTotalExecutionTime();
164     }
165 
166     public synchronized long getQueuedEvents()
167     {
168         return queuedEvent;
169     }
170 
171     public long getReplyToEventsSent()
172     {
173         return sentReplyToEvent.get();
174     }
175 
176     public long getSyncEventsSent()
177     {
178         return sentEventSync.get();
179     }
180 
181     public long getAsyncEventsSent()
182     {
183         return sentEventASync.get();
184     }
185 
186     public long getTotalEventsSent()
187     {
188         return getSyncEventsSent() + getAsyncEventsSent();
189     }
190 
191     public long getTotalEventsReceived()
192     {
193         return getSyncEventsReceived() + getAsyncEventsReceived();
194     }
195 
196     public long getExecutedEvents()
197     {
198         return componentStat.getExecutedEvents();
199     }
200 
201     public long getExecutionErrors()
202     {
203         return executionError.get();
204     }
205 
206     public void logSummary()
207     {
208         logSummary(new SimplePrinter(System.out));
209     }
210 
211     public void logSummary(PrintWriter printer)
212     {
213         printer.print(this);
214     }
215 
216     @Override
217     public synchronized void clear()
218     {
219         super.clear();
220         queuedEvent = 0;
221         maxQueuedEvent = 0;
222         totalQueuedEvent = 0;
223         averageQueueSize = 0;
224 
225         sentEventSync.set(0);
226         sentEventASync.set(0);
227         sentReplyToEvent.set(0);
228 
229         executionError.set(0);
230         fatalError.set(0);
231 
232         if (getInboundRouterStat() != null)
233         {
234             getInboundRouterStat().clear();
235         }
236         if (getOutboundRouterStat() != null)
237         {
238             getOutboundRouterStat().clear();
239         }
240 
241         samplePeriod = System.currentTimeMillis();
242     }
243 
244     public RouterStatistics getInboundRouterStat()
245     {
246         return inboundRouterStat;
247     }
248 
249     public void setInboundRouterStat(RouterStatistics inboundRouterStat)
250     {
251         this.inboundRouterStat = inboundRouterStat;
252         this.inboundRouterStat.setEnabled(enabled);
253     }
254 
255     public RouterStatistics getOutboundRouterStat()
256     {
257         return outboundRouterStat;
258     }
259 
260     public void setOutboundRouterStat(RouterStatistics outboundRouterStat)
261     {
262         this.outboundRouterStat = outboundRouterStat;
263         this.outboundRouterStat.setEnabled(enabled);
264     }
265 
266     public ComponentStatistics getComponentStat()
267     {
268         return componentStat;
269     }
270 
271     public void setComponentStat(ComponentStatistics componentStat)
272     {
273         this.componentStat = componentStat;
274         this.componentStat.setEnabled(enabled);
275     }
276 
277     public int getThreadPoolSize()
278     {
279         return threadPoolSize;
280     }
281 
282     public long getSamplePeriod()
283     {
284         return System.currentTimeMillis() - samplePeriod;
285     }
286 }