View Javadoc

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