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