View Javadoc

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