001: package wingset;
002:
003: import org.apache.commons.logging.Log;
004: import org.apache.commons.logging.LogFactory;
005: import org.wings.session.WingsStatistics;
006: import org.wings.util.SStringBuilder;
007:
008: import java.io.File;
009: import java.io.FileWriter;
010: import java.util.Timer;
011: import java.util.TimerTask;
012:
013: /**
014: * A simple {@link TimerTask} implementation logging some statistics received from the WingS framework
015: * and the JVM to a file in a ergular interval.
016: * Nice to monitor application load and resource needs.
017: *
018: * @author Benjamin Schmid
019: */
020: public class StatisticsTimerTask extends TimerTask {
021: private final static Log log = LogFactory
022: .getLog(StatisticsTimerTask.class);
023: private static final TimerTask infoTask = new StatisticsTimerTask();
024: private static final Timer timer = new Timer();
025: private static long oldRequestCount = 0;
026: private static long oldSessionCount = 0;
027: private static FileWriter infoWriter;
028:
029: private StatisticsTimerTask() {
030: }
031:
032: /**
033: * Do not call this method directly. Used by Timer.
034: */
035: public void run() {
036: SStringBuilder result = new SStringBuilder();
037: long totalmem = Runtime.getRuntime().totalMemory();
038: long freemem = Runtime.getRuntime().freeMemory();
039:
040: WingsStatistics stats = WingsStatistics.getStatistics();
041:
042: result.append(System.currentTimeMillis()).append(' ').append(
043: stats.getUptime()).append(' ').append(
044: stats.getOverallSessionCount()).append(' ').append(
045: stats.getOverallSessionCount() - oldSessionCount)
046: .append(' ').append(stats.getActiveSessionCount())
047: .append(' ').append(stats.getAllocatedSessionCount())
048: .append(' ').append(stats.getRequestCount())
049: .append(' ').append(
050: stats.getRequestCount() - oldRequestCount)
051: .append(' ').append(totalmem).append(' ').append(
052: freemem).append(' ').append(totalmem - freemem)
053: .append('\n');
054:
055: oldRequestCount = stats.getRequestCount();
056: oldSessionCount = stats.getOverallSessionCount();
057:
058: try {
059: if (infoWriter != null) {
060: infoWriter.write(result.toString());
061: infoWriter.flush();
062: }
063: } catch (Exception ex) {
064: ex.printStackTrace();
065: }
066: }
067:
068: /**
069: * Start logging of some wings statistics information into a temporary file calles wings-statisticsxxx.log
070: *
071: * @param intervalSeconds Interval to do logging in.
072: */
073: public static void startStatisticsLogging(int intervalSeconds) {
074: initInfoWriter();
075: timer.scheduleAtFixedRate(infoTask, 0, intervalSeconds * 1000);
076: }
077:
078: private static void initInfoWriter() {
079: try {
080: if (infoWriter == null) {
081: final File outputFile = File.createTempFile(
082: "wings-statistics", "log");
083: log.info("Logging session statistics to "
084: + outputFile.getAbsolutePath());
085: infoWriter = new FileWriter(outputFile, false);
086:
087: SStringBuilder result = new SStringBuilder();
088: result.append("timestamp").append(' ').append("uptime")
089: .append(' ').append("overall_sessions").append(
090: ' ').append("new_sessions").append(' ')
091: .append("active_sessions").append(' ').append(
092: "allocated_sessions").append(' ')
093: .append("overall_processed requests").append(
094: ' ').append("processed_requests")
095: .append(' ').append("total_memory").append(' ')
096: .append("free_memory").append(' ').append(
097: "used_memory").append('\n');
098: infoWriter.write(result.toString());
099: infoWriter.flush();
100: }
101: } catch (Exception ex) {
102: log.error("Exception on logging session statistics.");
103: }
104: }
105:
106: }
|