KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > applications > crontab > MMBaseStatsJob


1 /*
2  This software is OSI Certified Open Source Software.
3 OSI Certified is a certification mark of the Open Source Initiative.
4
5 The license (Mozilla version 1.0) can be read at the MMBase site.
6 See http://www.MMBase.org/license
7  */

8 package org.mmbase.applications.crontab;
9
10 import org.mmbase.util.ThreadPools;
11 import org.mmbase.cache.Cache;
12 import org.mmbase.util.logging.*;
13
14 /**
15  * An example cron-job.
16  *
17  * A Job to log MMBase statistics to a logger. (By means of logj4 you can configure the time stamp and logfile location).
18  * The configuration string is one of the following
19  <ul>
20   <li>MEMORY: Logs free and total memory</li>
21   <li>CACHE.&lt;cache-name&gt;: Logs hits and total request of cache with given name</li>
22  </ul>
23 In log4j.xml you may add something like this:
24 <pre>
25   &lt;appender name="stats" class="org.apache.log4j.FileAppender" &gt;
26     &lt;param name="File" value="/tmp/mmbase.stats" /&gt;
27     &lt;param name="Encoding" value="UTF-8" /&gt;
28     &lt;layout class="org.apache.log4j.PatternLayout"&gt;
29       &lt;param name="ConversionPattern" value="%d{YYYY-MM-dd HH:mm:ss} %c{1} %m%n" /&gt;
30     &lt;/layout&gt;
31   &lt;/appender&gt;
32 </pre>
33 and:
34 <pre>
35  &lt;logger name="org.mmbase.STATS" additivity="false"&gt;
36     &lt;level class="&mmlevel;" value ="service" /&gt;
37     &lt;appender-ref ref="stats" /&gt;
38   &lt;/logger&gt;
39 </pre>
40  * @author Michiel Meeuwissen
41  * @version $Id: MMBaseStatsJob.java,v 1.4 2006/06/23 18:11:56 michiel Exp $
42  */

43
44 public class MMBaseStatsJob extends AbstractCronJob {
45     private static final Logger log = Logging.getLoggerInstance(MMBaseStatsJob.class);
46
47     private Runnable JavaDoc job;
48
49     private Logger statsLogger;
50
51     protected void init() {
52         // determin what needs to be done in run().
53
String JavaDoc what = cronEntry.getConfiguration();
54         statsLogger = Logging.getLoggerInstance("org.mmbase.STATS." + what);
55         String JavaDoc w = what.toUpperCase();
56         if (w.equals("MEMORY")) {
57             job = new Runnable JavaDoc() {
58                     public void run() {
59                         Runtime JavaDoc runtime = Runtime.getRuntime();
60                         statsLogger.service("" + runtime.freeMemory() + "\t" + runtime.totalMemory());
61                     }
62                 };
63         } else if (w.equals("QUERIES")) {
64             job = new Runnable JavaDoc() {
65                     public void run() {
66                         statsLogger.service("" + org.mmbase.module.database.MultiConnection.queries);
67                     }
68                 };
69         } else if (w.equals("JOBSPOOL")) {
70             job = new Runnable JavaDoc() {
71                     public void run() {
72                         edu.emory.mathcs.backport.java.util.concurrent.ThreadPoolExecutor j =
73                             (edu.emory.mathcs.backport.java.util.concurrent.ThreadPoolExecutor) ThreadPools.jobsExecutor;
74                         statsLogger.service("" + j.getCompletedTaskCount() + '\t' + j.getActiveCount() + '\t'+ j.getQueue().size() + '\t' +
75                                             j.getPoolSize() + '\t' + j.getLargestPoolSize() + '\t' + j.getCorePoolSize() + '\t' + j.getMaximumPoolSize());
76                     }
77                 };
78         } else if (w.startsWith("CACHE.")) {
79             job = new Runnable JavaDoc() {
80                     private Cache cache = getCache();
81                     {
82                         if (cache == null) {
83                             log.info("No cache with name " + cronEntry.getConfiguration().substring(6) + " found (yet).");
84                         }
85                     }
86                     public void run() {
87                         if (cache == null) cache = getCache();
88                         if (cache != null) {
89                             int h = cache.getHits();
90                             statsLogger.service("" + h + "\t" + (h + cache.getMisses()));
91                         }
92                     }
93                 };
94         } else {
95             job = new Runnable JavaDoc() {
96                     public void run() {
97                     }
98                 };
99         }
100
101     }
102     /**
103      * Fills the 'cache' member.
104      * @return Whether successful.
105      */

106     private Cache getCache() {
107         String JavaDoc cacheName = cronEntry.getConfiguration().substring(6);
108         return Cache.getCache(cacheName);
109     }
110
111     public final void run() {
112         job.run();
113     }
114 }
115
Popular Tags