View Javadoc

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