1
2
3
4
5
6
7 package org.mule.management.stats;
8
9 import org.mule.api.MuleContext;
10 import org.mule.api.MuleSession;
11 import org.mule.api.construct.FlowConstruct;
12
13 import java.io.Serializable;
14 import java.util.concurrent.atomic.AtomicLong;
15
16
17
18
19 public class ProcessingTime implements Serializable
20 {
21
22
23
24
25 private static final long serialVersionUID = 1L;
26
27 private AtomicLong accumulator = new AtomicLong();
28 private FlowConstructStatistics statistics;
29
30
31
32
33
34
35 public static ProcessingTime newInstance(MuleSession session, MuleContext muleContext)
36 {
37 if (session != null)
38 {
39 FlowConstruct fc = session.getFlowConstruct();
40 if (fc != null)
41 {
42 FlowConstructStatistics stats = fc.getStatistics();
43 if (stats != null && fc.getStatistics().isEnabled())
44 {
45 return new ProcessingTime(stats, muleContext);
46 }
47 }
48
49 }
50
51 return null;
52 }
53
54
55
56
57
58
59
60 private ProcessingTime(FlowConstructStatistics stats, MuleContext muleContext)
61 {
62 this.statistics = stats;
63 ProcessingTimeWatcher processorTimeWatcher = muleContext.getProcessorTimeWatcher();
64 processorTimeWatcher.addProcessingTime(this);
65 }
66
67
68
69
70
71
72 public void addFlowExecutionBranchTime(long startTime)
73 {
74 if (statistics.isEnabled())
75 {
76 long elapsedTime = getEffectiveTime(System.currentTimeMillis() - startTime);
77 statistics.addFlowExecutionBranchTime(elapsedTime, accumulator.addAndGet(elapsedTime));
78 }
79 }
80
81
82
83
84
85 public static long getEffectiveTime(long time)
86 {
87 return (time <= 0) ? 1L : time;
88 }
89
90 public FlowConstructStatistics getStatistics()
91 {
92 return statistics;
93 }
94
95 public AtomicLong getAccumulator()
96 {
97 return accumulator;
98 }
99 }