View Javadoc

1   /*
2    * $Id: FlowConstructService.java 20320 2010-11-24 15:03:31Z dfeist $
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  package org.mule.module.management.mbean;
11  
12  import org.mule.api.MuleContext;
13  import org.mule.construct.AbstractFlowConstruct;
14  import org.mule.management.stats.FlowConstructStatistics;
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      public FlowConstructService(String type, String name, MuleContext muleContext, FlowConstructStatistics statistics)
46      {
47          this.muleContext = muleContext;
48          this.type = type;
49          this.name = name;
50          this.statistics = statistics;
51      }
52  
53      protected FlowConstructService(String type, String name, MuleContext muleContext)
54      {
55          this.muleContext = muleContext;
56          this.type = type;
57          this.name = name;
58      }
59      
60      public String getName()
61      {
62          return name;
63      }
64  
65      public String getType()
66      {
67          return type;
68      }
69  
70      public ObjectName getStatistics()
71      {
72          return statsName;
73      }
74  
75      public void clearStatistics()
76      {
77          statistics.clear();
78      }
79  
80      public long getAsyncEventsReceived()
81      {
82          return statistics.getAsyncEventsReceived();
83      }
84  
85      public long getSyncEventsReceived()
86      {
87          return statistics.getSyncEventsReceived();
88      }
89     
90      public long getTotalEventsReceived()
91      {
92          return statistics.getTotalEventsReceived();
93      }
94  
95      public long getAverageProcessingTime()
96      {
97          return statistics.getAverageProcessingTime();
98      }
99  
100     public long getProcessedEvents()
101     {
102         return statistics.getProcessedEvents();
103     }
104 
105     public long getMaxProcessingTime()
106     {
107         return statistics.getMaxProcessingTime();
108     }
109 
110     public long getMinProcessingTime()
111     {
112         return statistics.getMinProcessingTime();
113     }
114 
115     public long getTotalProcessingTime()
116     {
117         return statistics.getTotalProcessingTime();
118     }
119 
120     public long getExecutionErrors()
121     {
122         return statistics.getExecutionErrors();
123     }
124 
125     public long getFatalErrors()
126     {
127         return statistics.getFatalErrors();
128     }
129 
130     public ObjectName preRegister(MBeanServer server, ObjectName name) throws Exception
131     {
132         this.server = server;
133         this.objectName = name;
134         return name;
135     }
136 
137     public void postRegister(Boolean registrationDone)
138     {
139         AbstractFlowConstruct flow = muleContext.getRegistry().lookupObject(getName());
140         try
141         {
142             if (flow.getStatistics() != null)
143             {
144                 statsName = new ObjectName(objectName.getDomain() + ":type=org.mule.Statistics," +
145                     flow.getConstructType() + "=" + getName());
146                 // unregister old version if exists
147                 if (this.server.isRegistered(statsName))
148                 {
149                     this.server.unregisterMBean(statsName);
150                 }
151 
152                 this.server.registerMBean(new FlowConstructStats(flow.getStatistics()), this.statsName);
153             }
154         }
155         catch (Exception e)
156         {
157             LOGGER.error("Error post-registering the MBean", e);
158         }
159     }
160 
161     public void preDeregister() throws Exception
162     {
163         try
164         {
165             if (this.server.isRegistered(statsName))
166             {
167                 this.server.unregisterMBean(statsName);
168             }
169         }
170         catch (Exception ex)
171         {
172             LOGGER.error("Error unregistering ServiceService child " + statsName.getCanonicalName(), ex);
173         }
174     }
175 
176     public void postDeregister()
177     {
178         // nothing to do
179     }
180 }