tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Ronaldo Arrudas <rdarru...@gmail.com>
Subject Re: Tomcat memory alert
Date Thu, 14 Apr 2005 16:03:41 GMT
Hi carlos:

I think a nice way is to deploy a simple TimerTask inside any webapp to 
do this work. You could use anything like the following to help you:

import java.util.*;
public class MemoryWatchDog extends TimerTask{

    private static Timer currentTimer = null;

    public static void startTimer() {
            if(currentTimer != null)
                return;
            int seconds = 60;
            Timer timer = new Timer();
            timer.scheduleAtFixedRate(new WatchDog(), 1, (seconds * 1000));
            currentTimer = timer;
        }
    }

    public static void stopTimer() {
        if(currentTimer == null)
            return;
        currentTimer.cancel();
        currentTimer = null;
    }

    private WatchDog() {
        // Just for hide the constructor
    }

    public void run() {
        int long lowMemory = 5242880; // 5Mb
        if(Runtime.getRuntime().freeMemory()  <= lowMemory){
            // Using Jakarta Commons-Email to do the hard work...
            SimpleEmail email = new SimpleEmail();
            email.setHostName("mail.myserver.com");
            email.addTo("you@yourdomain.org","John Doe");
            email.setFrom("tomcat@somewhere.org", "Tomcat");
            email.setSubject("Warning!!!");
            email.setMsg("Look at me! I won't have enough memory in few 
seconds!!!"
                                       + "\n Free memory (bytes): "
                                       + Runtime.getRuntime().freeMemory());
            email.send();
        }
    }
}

[]'s
Ronaldo Arrudas
rdarrudas@gmail.com


Carlos Rule wrote:

>Hello all, newbie here.
>
>Does anyone know if there is a reliable way of setting up an alert to tell
>me whenever the Tomcat (5.0.25) process reaches a certain level of memory
>usage? 
>
>Many thanks!
>
>Carlos
>
> 
>
>
>  
>

Mime
View raw message