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.module.management.mbean;
8   
9   import org.mule.api.MuleContext;
10  import org.mule.construct.AbstractFlowConstruct;
11  import org.mule.management.stats.FlowConstructStatistics;
12  import org.mule.module.management.support.AutoDiscoveryJmxSupportFactory;
13  import org.mule.module.management.support.JmxSupport;
14  import org.mule.module.management.support.JmxSupportFactory;
15  
16  import javax.management.MBeanRegistration;
17  import javax.management.MBeanServer;
18  import javax.management.ObjectName;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  
23  /**
24   * <code>FlowConstructService</code> exposes service information about a Mule Managed
25   * flow construct.
26   */
27  public class FlowConstructService implements FlowConstructServiceMBean, MBeanRegistration, FlowConstructStatsMBean
28  {
29      private static Log LOGGER = LogFactory.getLog(FlowConstructService.class);
30  
31      protected FlowConstructStatistics statistics;
32  
33      protected MBeanServer server;
34  
35      protected String name;
36  
37      protected String type;
38  
39      protected ObjectName statsName;
40  
41      protected ObjectName objectName;
42  
43      protected MuleContext muleContext;
44      
45      // JmxSupport in order to build MBean's ObjectNames properly.
46      protected JmxSupportFactory jmxSupportFactory = AutoDiscoveryJmxSupportFactory.getInstance();
47      protected JmxSupport jmxSupport = jmxSupportFactory.getJmxSupport();
48  
49      public FlowConstructService(String type, String name, MuleContext muleContext, FlowConstructStatistics statistics)
50      {
51          this.muleContext = muleContext;
52          this.type = type;
53          this.name = name;
54          this.statistics = statistics;
55      }
56  
57      protected FlowConstructService(String type, String name, MuleContext muleContext)
58      {
59          this.muleContext = muleContext;
60          this.type = type;
61          this.name = name;
62      }
63      
64      public String getName()
65      {
66          return name;
67      }
68  
69      public String getType()
70      {
71          return type;
72      }
73  
74      public ObjectName getStatistics()
75      {
76          return statsName;
77      }
78  
79      public void clearStatistics()
80      {
81          statistics.clear();
82      }
83  
84      public long getAsyncEventsReceived()
85      {
86          return statistics.getAsyncEventsReceived();
87      }
88  
89      public long getSyncEventsReceived()
90      {
91          return statistics.getSyncEventsReceived();
92      }
93     
94      public long getTotalEventsReceived()
95      {
96          return statistics.getTotalEventsReceived();
97      }
98  
99      public long getAverageProcessingTime()
100     {
101         return statistics.getAverageProcessingTime();
102     }
103 
104     public long getProcessedEvents()
105     {
106         return statistics.getProcessedEvents();
107     }
108 
109     public long getMaxProcessingTime()
110     {
111         return statistics.getMaxProcessingTime();
112     }
113 
114     public long getMinProcessingTime()
115     {
116         return statistics.getMinProcessingTime();
117     }
118 
119     public long getTotalProcessingTime()
120     {
121         return statistics.getTotalProcessingTime();
122     }
123 
124     public long getExecutionErrors()
125     {
126         return statistics.getExecutionErrors();
127     }
128 
129     public long getFatalErrors()
130     {
131         return statistics.getFatalErrors();
132     }
133 
134     public ObjectName preRegister(MBeanServer server, ObjectName name) throws Exception
135     {
136         this.server = server;
137         this.objectName = name;
138         return name;
139     }
140 
141     public void postRegister(Boolean registrationDone)
142     {
143         AbstractFlowConstruct flow = muleContext.getRegistry().lookupObject(getName());
144         try
145         {
146             if (flow.getStatistics() != null)
147             {
148                 statsName = jmxSupport.getObjectName(String.format("%s:type=org.mule.Statistics,%s=%s", objectName.getDomain(), 
149                     flow.getConstructType(), jmxSupport.escape(getName())));
150                 
151                 // unregister old version if exists
152                 if (this.server.isRegistered(statsName))
153                 {
154                     this.server.unregisterMBean(statsName);
155                 }
156 
157                 this.server.registerMBean(new FlowConstructStats(flow.getStatistics()), this.statsName);
158             }
159         }
160         catch (Exception e)
161         {
162             LOGGER.error("Error post-registering the MBean", e);
163         }
164     }
165 
166     public void preDeregister() throws Exception
167     {
168         try
169         {
170             if (this.server.isRegistered(statsName))
171             {
172                 this.server.unregisterMBean(statsName);
173             }
174         }
175         catch (Exception ex)
176         {
177             LOGGER.error("Error unregistering ServiceService child " + statsName.getCanonicalName(), ex);
178         }
179     }
180 
181     public void postDeregister()
182     {
183         // nothing to do
184     }
185 }