View Javadoc

1   /*
2    * $Id: ComponentService.java 7976 2007-08-21 14:26:13Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.mbeans;
12  
13  import org.mule.impl.model.AbstractComponent;
14  import org.mule.impl.model.ModelHelper;
15  import org.mule.impl.model.seda.SedaComponent;
16  import org.mule.management.stats.ComponentStatistics;
17  import org.mule.umo.UMOComponent;
18  import org.mule.umo.UMOException;
19  
20  import javax.management.MBeanRegistration;
21  import javax.management.MBeanServer;
22  import javax.management.ObjectName;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  
27  /**
28   * <code>ComponentService</code> exposes service information about a Mule Managed
29   * component
30   * 
31   * @author <a href="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
32   * @version $Revision: 7976 $
33   */
34  public class ComponentService implements ComponentServiceMBean, MBeanRegistration, ComponentStatsMBean
35  {
36  
37      /**
38       * logger used by this class
39       */
40      private static Log LOGGER = LogFactory.getLog(ComponentService.class);
41  
42      private MBeanServer server;
43  
44      private String name;
45  
46      private ObjectName statsName;
47  
48      private ObjectName objectName;
49  
50      private ComponentStatistics statistics;
51  
52      public ComponentService(String name)
53      {
54          this.name = name;
55          this.statistics = getComponent().getStatistics();
56  
57      }
58  
59      public int getQueueSize()
60      {
61          UMOComponent c = getComponent();
62          if (c instanceof SedaComponent)
63          {
64              return ((SedaComponent)c).getQueueSize();
65          }
66          else
67          {
68              return -1;
69          }
70      }
71  
72      /**
73       * Pauses event processing for theComponent. Unlike stop(), a paused component
74       * will still consume messages from the underlying transport, but those messages
75       * will be queued until the component is resumed. <p/> In order to persist these
76       * queued messages you can set the 'recoverableMode' property on the
77       * Muleconfiguration to true. this causes all internal queues to store their
78       * state.
79       * 
80       * @throws org.mule.umo.UMOException if the component failed to pause.
81       * @see org.mule.config.MuleConfiguration
82       */
83      public void pause() throws UMOException
84      {
85          getComponent().pause();
86      }
87  
88      /**
89       * Resumes the Component that has been paused. If the component is not paused
90       * nothing is executed.
91       * 
92       * @throws org.mule.umo.UMOException if the component failed to resume
93       */
94      public void resume() throws UMOException
95      {
96          getComponent().resume();
97      }
98  
99      public boolean isPaused()
100     {
101         return getComponent().isPaused();
102     }
103 
104     public boolean isStopped()
105     {
106         return getComponent().isStopped();
107     }
108 
109     public void stop() throws UMOException
110     {
111         getComponent().stop();
112     }
113 
114     public void forceStop() throws UMOException
115     {
116         getComponent().forceStop();
117     }
118 
119     public boolean isStopping()
120     {
121         return getComponent().isStopping();
122     }
123 
124     public void dispose() throws UMOException
125     {
126         getComponent().dispose();
127     }
128 
129     public void start() throws UMOException
130     {
131         getComponent().start();
132     }
133 
134     /*
135      * (non-Javadoc)
136      * 
137      * @see org.mule.management.mbeans.ComponentServiceMBean#getStatistics()
138      */
139     public ObjectName getStatistics()
140     {
141         return statsName;
142     }
143 
144     /*
145      * (non-Javadoc)
146      * 
147      * @see javax.management.MBeanRegistration#preRegister(javax.management.MBeanServer,
148      *      javax.management.ObjectName)
149      */
150     public ObjectName preRegister(MBeanServer server, ObjectName name) throws Exception
151     {
152         this.server = server;
153         this.objectName = name;
154         return name;
155     }
156 
157     /*
158      * (non-Javadoc)
159      * 
160      * @see javax.management.MBeanRegistration#postRegister(java.lang.Boolean)
161      */
162     public void postRegister(Boolean registrationDone)
163     {
164         try
165         {
166             if (getComponent().getStatistics() != null)
167             {
168                 statsName = new ObjectName(objectName.getDomain() + ":type=org.mule.Statistics,component="
169                                            + getName());
170                 // unregister old version if exists
171                 if (this.server.isRegistered(statsName))
172                 {
173                     this.server.unregisterMBean(statsName);
174                 }
175 
176                 this.server.registerMBean(new ComponentStats(getComponent().getStatistics()), this.statsName);
177             }
178         }
179         catch (Exception e)
180         {
181             LOGGER.error("Error post-registering the MBean", e);
182         }
183     }
184 
185     /*
186      * (non-Javadoc)
187      * 
188      * @see javax.management.MBeanRegistration#preDeregister()
189      */
190     public void preDeregister() throws Exception
191     {
192         try
193         {
194             if (this.server.isRegistered(statsName))
195             {
196                 this.server.unregisterMBean(statsName);
197             }
198         }
199         catch (Exception ex)
200         {
201             LOGGER.error("Error unregistering ComponentService child " + statsName.getCanonicalName(), ex);
202         }
203     }
204 
205     /*
206      * (non-Javadoc)
207      * 
208      * @see javax.management.MBeanRegistration#postDeregister()
209      */
210     public void postDeregister()
211     {
212         // nothing to do
213     }
214 
215     private AbstractComponent getComponent()
216     {
217         return (AbstractComponent)ModelHelper.getComponent(getName());
218     }
219 
220     // ///// Component stats impl /////////
221 
222     /**
223      *
224      */
225     public void clearStatistics()
226     {
227         statistics.clear();
228     }
229 
230     /**
231      * @return
232      */
233     public long getAsyncEventsReceived()
234     {
235         return statistics.getAsyncEventsReceived();
236     }
237 
238     /**
239      * @return
240      */
241     public long getAsyncEventsSent()
242     {
243         return statistics.getAsyncEventsSent();
244     }
245 
246     /**
247      * @return
248      */
249     public long getAverageExecutionTime()
250     {
251         return statistics.getAverageExecutionTime();
252     }
253 
254     /**
255      * @return
256      */
257     public long getAverageQueueSize()
258     {
259         return statistics.getAverageQueueSize();
260     }
261 
262     /**
263      * @return
264      */
265     public long getExecutedEvents()
266     {
267         return statistics.getExecutedEvents();
268     }
269 
270     /**
271      * @return
272      */
273     public long getExecutionErrors()
274     {
275         return statistics.getExecutionErrors();
276     }
277 
278     /**
279      * @return
280      */
281     public long getFatalErrors()
282     {
283         return statistics.getFatalErrors();
284     }
285 
286     /**
287      * @return
288      */
289     public long getMaxExecutionTime()
290     {
291         return statistics.getMaxExecutionTime();
292     }
293 
294     /**
295      * @return
296      */
297     public long getMaxQueueSize()
298     {
299         return statistics.getMaxQueueSize();
300     }
301 
302     /**
303      * @return
304      */
305     public long getMinExecutionTime()
306     {
307         return statistics.getMinExecutionTime();
308     }
309 
310     /**
311      * @return
312      */
313     public String getName()
314     {
315         return name;
316     }
317 
318     /**
319      * @return
320      */
321     public long getQueuedEvents()
322     {
323         return statistics.getQueuedEvents();
324     }
325 
326     /**
327      * @return
328      */
329     public long getReplyToEventsSent()
330     {
331         return statistics.getReplyToEventsSent();
332     }
333 
334     /**
335      * @return
336      */
337     public long getSyncEventsReceived()
338     {
339         return statistics.getSyncEventsReceived();
340     }
341 
342     /**
343      * @return
344      */
345     public long getSyncEventsSent()
346     {
347         return statistics.getSyncEventsSent();
348     }
349 
350     /**
351      * @return
352      */
353     public long getTotalEventsReceived()
354     {
355         return statistics.getTotalEventsReceived();
356     }
357 
358     /**
359      * @return
360      */
361     public long getTotalEventsSent()
362     {
363         return statistics.getTotalEventsSent();
364     }
365 
366     /**
367      * @return
368      */
369     public long getTotalExecutionTime()
370     {
371         return statistics.getTotalExecutionTime();
372     }
373 }