View Javadoc

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