View Javadoc

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