KickJava   Java API By Example, From Geeks To Geeks.

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


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.bridge.*;
11 import org.mmbase.util.functions.*;
12 import org.mmbase.util.logging.*;
13 import java.text.NumberFormat JavaDoc;
14 import java.util.Locale JavaDoc;
15
16 /**
17  * Performs Runtime.gc(), and if afterwards the used memory is above a certain fraction of the
18  * maximally used memory, mail a warning to someone. The idea is that that someone can then take
19  * action, like exploring what is the matter, or clearing some caches.
20  *
21  * You need mmbase-email.jar installed for this.
22  *
23    <pre>
24    &lt;property name="memory"&gt;*&#047;10 * * * *|org.mmbase.applications.crontab.MemoryWarner||0.8;Michiel.Meeuwissen@omroep.nl&lt;/property&gt;
25    </pre>
26  * @author Michiel Meeuwissen
27  * @version $Id: MemoryWarner.java,v 1.3 2006/06/16 09:14:04 michiel Exp $
28  */

29
30 public class MemoryWarner extends AbstractCronJob {
31     private static final Logger log = Logging.getLoggerInstance(MemoryWarner.class);
32
33
34     public final void run() {
35         try {
36             String JavaDoc[] config = cronEntry.getConfiguration().split(";");
37
38             Runtime JavaDoc rt = Runtime.getRuntime();
39             rt.gc();
40
41             long usedMemory = rt.totalMemory() - rt.freeMemory();
42             long maxMemory = rt.maxMemory();
43
44             double use = (double) usedMemory / rt.maxMemory();
45             double limit = Double.parseDouble(config[0]);
46             String JavaDoc usePerc = NumberFormat.getPercentInstance(Locale.US).format(use);
47             String JavaDoc limitPerc = NumberFormat.getPercentInstance(Locale.US).format(limit);
48             if (use > limit) {
49                 log.info("Memory use " + usePerc + " > " + limitPerc);
50                 log.info("Used memory over " + limitPerc + " , mailing " + config[1]);
51                 Cloud cloud = ContextProvider.getDefaultCloudContext().getCloud("mmbase", "class", null);
52                 if (cloud.hasNodeManager("email")) {
53                     NodeManager email = cloud.getNodeManager("email");
54                     Node message = email.createNode();
55                     message.setValue("from", "memorywarner@" + java.net.InetAddress.getLocalHost().getHostName());
56                     message.setValue("to", config[1]);
57                     message.setValue("subject", "Out of memory warning: more than " + limitPerc + " in use, for " +
58                                      org.mmbase.module.core.MMBaseContext.getHtmlRootUrlPath() + "@" +
59                                      java.net.InetAddress.getLocalHost().getHostName());
60                     message.setValue("body", "Memory use " + usePerc + " > " + limitPerc);
61                     message.commit();
62                     Function mail = message.getFunction("mail");
63                     Parameters params = mail.createParameters();
64                     params.set("type", "oneshot");
65                     mail.getFunctionValue(params);
66                 } else {
67                     log.warn("No mail builder installed");
68                     // could introduce depedency on java-mail here.
69
}
70             } else {
71                 log.info("Memory use " + usePerc + " < " + limitPerc);
72             }
73         } catch (java.net.UnknownHostException JavaDoc uhe) {
74             log.error(uhe);
75         }
76     }
77 }
78
Popular Tags