View Javadoc

1   /*
2    * $Id: AbstractFlowConstructStatistics.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.api.management.stats.Statistics;
14  
15  import java.util.concurrent.atomic.AtomicLong;
16  
17  /**
18   * Statistics common to flows and services
19   */
20  public abstract class AbstractFlowConstructStatistics implements Statistics
21  {
22      private static final long serialVersionUID = 5337576392583767442L;
23  
24      protected final String flowConstructType;
25      protected String name;
26      protected boolean enabled = false;
27      private long samplePeriod = 0;
28      protected final AtomicLong receivedEventSync = new AtomicLong(0);
29      protected final AtomicLong receivedEventASync = new AtomicLong(0);
30  
31      public AbstractFlowConstructStatistics(String flowConstructType, String name)
32      {
33          this.name = name;
34          this.flowConstructType = flowConstructType;
35      }
36  
37      /**
38       * Enable statistics logs (this is a dynamic parameter)
39       */
40      public synchronized void setEnabled(boolean b)
41      {
42          enabled = b;
43      }
44  
45      /**
46       * Are statistics logged
47       */
48      public boolean isEnabled()
49      {
50          return enabled;
51      }
52  
53      public synchronized String getName()
54      {
55          return name;
56      }
57  
58      public synchronized void setName(String name)
59      {
60          this.name = name;
61      }
62  
63      public synchronized void clear()
64      {
65          receivedEventSync.set(0);
66          receivedEventASync.set(0);
67          samplePeriod = System.currentTimeMillis();
68      }
69  
70  
71      public void incReceivedEventSync()
72      {
73          receivedEventSync.addAndGet(1);
74      }
75  
76      public void incReceivedEventASync()
77      {
78          receivedEventASync.addAndGet(1);
79      }
80  
81      public long getAsyncEventsReceived()
82      {
83          return receivedEventASync.get();
84      }
85  
86      public long getSyncEventsReceived()
87      {
88          return receivedEventSync.get();
89      }
90  
91      public long getTotalEventsReceived()
92      {
93          return getSyncEventsReceived() + getAsyncEventsReceived();
94      }
95  
96      public String getFlowConstructType()
97      {
98          return flowConstructType;
99      }
100 
101     public long getSamplePeriod()
102     {
103         return System.currentTimeMillis() - samplePeriod;
104     }
105 }