View Javadoc

1   /*
2    * $Id: ServiceStats.java 11234 2008-03-06 23:44:34Z tcarlson $
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.management.stats.ServiceStatistics;
14  import org.mule.management.stats.RouterStatistics;
15  
16  import javax.management.MBeanRegistration;
17  import javax.management.MBeanServer;
18  import javax.management.ObjectName;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
22  
23  /**
24   * <code>ServiceStats</code> TODO
25   */
26  public class ServiceStats implements ServiceStatsMBean, MBeanRegistration
27  {
28  
29      /**
30       * logger used by this class
31       */
32      private static Log LOGGER = LogFactory.getLog(ServiceStats.class);
33  
34      private MBeanServer server;
35  
36      private ObjectName name;
37      private ObjectName inboundName;
38      private ObjectName outboundName;
39  
40      private ServiceStatistics statistics;
41  
42      public ServiceStats(ServiceStatistics statistics)
43      {
44          this.statistics = statistics;
45      }
46  
47      /**
48       * 
49       */
50      public void clearStatistics()
51      {
52          statistics.clear();
53      }
54  
55      /**
56       * @return
57       */
58      public long getAsyncEventsReceived()
59      {
60          return statistics.getAsyncEventsReceived();
61      }
62  
63      /**
64       * @return
65       */
66      public long getAsyncEventsSent()
67      {
68          return statistics.getAsyncEventsSent();
69      }
70  
71      /**
72       * @return
73       */
74      public long getAverageExecutionTime()
75      {
76          return statistics.getAverageExecutionTime();
77      }
78  
79      /**
80       * @return
81       */
82      public long getAverageQueueSize()
83      {
84          return statistics.getAverageQueueSize();
85      }
86  
87      /**
88       * @return
89       */
90      public long getExecutedEvents()
91      {
92          return statistics.getExecutedEvents();
93      }
94  
95      /**
96       * @return
97       */
98      public long getExecutionErrors()
99      {
100         return statistics.getExecutionErrors();
101     }
102 
103     /**
104      * @return
105      */
106     public long getFatalErrors()
107     {
108         return statistics.getFatalErrors();
109     }
110 
111     /**
112      * @return
113      */
114     public long getMaxExecutionTime()
115     {
116         return statistics.getMaxExecutionTime();
117     }
118 
119     /**
120      * @return
121      */
122     public long getMaxQueueSize()
123     {
124         return statistics.getMaxQueueSize();
125     }
126 
127     /**
128      * @return
129      */
130     public long getMinExecutionTime()
131     {
132         return statistics.getMinExecutionTime();
133     }
134 
135     /**
136      * @return
137      */
138     public String getName()
139     {
140         return statistics.getName();
141     }
142 
143     /**
144      * @return
145      */
146     public long getQueuedEvents()
147     {
148         return statistics.getQueuedEvents();
149     }
150 
151     /**
152      * @return
153      */
154     public long getReplyToEventsSent()
155     {
156         return statistics.getReplyToEventsSent();
157     }
158 
159     /**
160      * @return
161      */
162     public long getSyncEventsReceived()
163     {
164         return statistics.getSyncEventsReceived();
165     }
166 
167     /**
168      * @return
169      */
170     public long getSyncEventsSent()
171     {
172         return statistics.getSyncEventsSent();
173     }
174 
175     /**
176      * @return
177      */
178     public long getTotalEventsReceived()
179     {
180         return statistics.getTotalEventsReceived();
181     }
182 
183     /**
184      * @return
185      */
186     public long getTotalEventsSent()
187     {
188         return statistics.getTotalEventsSent();
189     }
190 
191     /**
192      * @return
193      */
194     public long getTotalExecutionTime()
195     {
196         return statistics.getTotalExecutionTime();
197     }
198 
199     /*
200      * (non-Javadoc)
201      * 
202      * @see javax.management.MBeanRegistration#preRegister(javax.management.MBeanServer,
203      *      javax.management.ObjectName)
204      */
205     public ObjectName preRegister(MBeanServer server, ObjectName name) throws Exception
206     {
207         this.server = server;
208         this.name = name;
209         return name;
210     }
211 
212     /*
213      * (non-Javadoc)
214      * 
215      * @see javax.management.MBeanRegistration#postRegister(java.lang.Boolean)
216      */
217     public void postRegister(Boolean registrationDone)
218     {
219 
220         try
221         {
222             RouterStatistics is = this.statistics.getInboundRouterStat();
223             if (is != null)
224             {
225                 inboundName = new ObjectName(name.getDomain() + ":type=org.mule.Statistics,service="
226                                              + statistics.getName() + ",router=inbound");
227                 // unregister old version if exists
228                 if (this.server.isRegistered(inboundName))
229                 {
230                     this.server.unregisterMBean(inboundName);
231                 }
232                 this.server.registerMBean(new RouterStats(is), this.inboundName);
233             }
234             RouterStatistics os = this.statistics.getOutboundRouterStat();
235             if (os != null)
236             {
237                 outboundName = new ObjectName(name.getDomain() + ":type=org.mule.Statistics,service="
238                                               + statistics.getName() + ",router=outbound");
239                 // unregister old version if exists
240                 if (this.server.isRegistered(outboundName))
241                 {
242                     this.server.unregisterMBean(outboundName);
243                 }
244                 this.server.registerMBean(new RouterStats(os), this.outboundName);
245             }
246         }
247         catch (Exception e)
248         {
249             LOGGER.error("Error post-registering MBean", e);
250         }
251 
252     }
253 
254     /*
255      * (non-Javadoc)
256      * 
257      * @see javax.management.MBeanRegistration#preDeregister()
258      */
259     public void preDeregister() throws Exception
260     {
261         // nothing to do
262     }
263 
264     /*
265      * (non-Javadoc)
266      * 
267      * @see javax.management.MBeanRegistration#postDeregister()
268      */
269     public void postDeregister()
270     {
271         try
272         {
273             if (this.server.isRegistered(inboundName))
274             {
275                 this.server.unregisterMBean(inboundName);
276             }
277         }
278         catch (Exception ex)
279         {
280             LOGGER.error("Error unregistering ServiceStats child " + inboundName.getCanonicalName(), ex);
281         }
282         try
283         {
284             if (this.server.isRegistered(outboundName))
285             {
286                 this.server.unregisterMBean(outboundName);
287             }
288         }
289         catch (Exception ex)
290         {
291             LOGGER.error("Error unregistering ServiceStats child " + inboundName.getCanonicalName(), ex);
292         }
293     }
294 
295     /*
296      * (non-Javadoc)
297      * 
298      * @see org.mule.management.mbeans.ServiceStatsMBean#getInboundRouter()
299      */
300     public ObjectName getRouterInbound()
301     {
302         return this.inboundName;
303     }
304 
305     /*
306      * (non-Javadoc)
307      * 
308      * @see org.mule.management.mbeans.ServiceStatsMBean#getOutboundRouter()
309      */
310     public ObjectName getRouterOutbound()
311     {
312         return this.outboundName;
313     }
314 }