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