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