View Javadoc

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