1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
package net.admin4j.ui.servlets; |
15 | |
|
16 | |
import javax.servlet.ServletConfig; |
17 | |
import javax.servlet.ServletException; |
18 | |
|
19 | |
import net.admin4j.config.Admin4JConfiguration; |
20 | |
import net.admin4j.monitor.Detector; |
21 | |
import net.admin4j.monitor.LowMemoryDetector; |
22 | |
import net.admin4j.util.Daemon; |
23 | |
import net.admin4j.util.ServletUtils; |
24 | |
import net.admin4j.util.notify.NotifierUtils; |
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | 6 | public class MemoryMonitorStartupServlet extends Admin4JServlet { |
39 | |
|
40 | |
private static final long serialVersionUID = 694573116545416300L; |
41 | |
public static final String PUBLIC_HANDLE="memory"; |
42 | |
|
43 | 6 | private long sleepIntervalMillis = LowMemoryDetector.DEFAULT_SLEEP_INTERVAL; |
44 | 6 | private int memoryThresholdPct = LowMemoryDetector.DEFAULT_MEMORY_USAGE_THRESHOLD_PCT; |
45 | 6 | private int nbrIntervalsBetweenWarnings = LowMemoryDetector.DEFAULT_NBR_INTERVALS_BETWEEN_WARNINGS; |
46 | 6 | private int nbrLowWatermarkIntervals = LowMemoryDetector.DEFAULT_NBR_LOW_WATERMARK_INTERVALS; |
47 | 6 | private long lowWatermarkMonitorIntervalInMillis = LowMemoryDetector.DEFAULT_LOW_WATERMARK_MONITOR_INTERVAL_IN_MILLIS; |
48 | |
|
49 | |
@SuppressWarnings("unused") |
50 | 6 | private static Daemon memoryMonitorDaemon = null; |
51 | |
|
52 | |
@Override |
53 | |
public void init(ServletConfig config) throws ServletException { |
54 | 6 | super.init(config); |
55 | |
try { |
56 | |
|
57 | 6 | String sleepIntervalMillisStr = ServletUtils.getConfigurationSetting( |
58 | |
new String[]{PUBLIC_HANDLE + ".sleep.interval.millis", |
59 | |
"sleep.interval.millis"}, config); |
60 | 6 | if (sleepIntervalMillisStr != null) { |
61 | |
try { |
62 | 3 | sleepIntervalMillis = Long.parseLong(sleepIntervalMillisStr); |
63 | 0 | } catch (Exception e) { |
64 | 0 | sleepIntervalMillis = LowMemoryDetector.DEFAULT_SLEEP_INTERVAL; |
65 | 0 | this.logger.error("Invalid sleep.interval.millis parameter for MemoryMonitorStartupServlet. Default used. sleep.interval.millis={}", |
66 | |
sleepIntervalMillisStr, e); |
67 | 3 | } |
68 | |
} |
69 | 3 | else if ( Admin4JConfiguration.getMemorySleepIntervalMillis() != null ) { |
70 | 0 | sleepIntervalMillis = Admin4JConfiguration.getMemorySleepIntervalMillis(); |
71 | |
} |
72 | |
|
73 | 6 | String memoryThresholdPctStr = ServletUtils.getConfigurationSetting( |
74 | |
new String[]{PUBLIC_HANDLE + ".threshold.pct"}, config); |
75 | 6 | if (memoryThresholdPctStr != null) { |
76 | |
try { |
77 | 3 | memoryThresholdPct = Integer.parseInt(memoryThresholdPctStr); |
78 | 0 | } catch (Exception e) { |
79 | 0 | memoryThresholdPct = LowMemoryDetector.DEFAULT_MEMORY_USAGE_THRESHOLD_PCT; |
80 | 0 | this.logger.error("Invalid memory.threshold.pct parameter for MemoryMonitorStartupServlet. Default used. memory.threshold.pct={}", |
81 | |
memoryThresholdPctStr, e); |
82 | 3 | } |
83 | |
} |
84 | 3 | else if ( Admin4JConfiguration.getMemoryThresholdPct() != null ) { |
85 | 0 | memoryThresholdPct = Admin4JConfiguration.getMemoryThresholdPct(); |
86 | |
} |
87 | |
|
88 | 6 | String nbrIntervalsBetweenWarningsStr = ServletUtils.getConfigurationSetting( |
89 | |
new String[]{PUBLIC_HANDLE + ".nbr.intervals.between.warnings", |
90 | |
"nbr.intervals.between.warnings"}, config); |
91 | 6 | if (nbrIntervalsBetweenWarningsStr != null) { |
92 | |
try { |
93 | 3 | nbrIntervalsBetweenWarnings = Integer.parseInt(nbrIntervalsBetweenWarningsStr); |
94 | 0 | } catch (Exception e) { |
95 | 0 | nbrIntervalsBetweenWarnings = LowMemoryDetector.DEFAULT_NBR_INTERVALS_BETWEEN_WARNINGS; |
96 | 0 | this.logger.error("Invalid nbr.intervals.between.warnings for MemoryMonitorStartupServlet. Default used. nbr.intervals.between.warnings={}", |
97 | |
nbrIntervalsBetweenWarningsStr, e); |
98 | 3 | } |
99 | |
} |
100 | 3 | else if ( Admin4JConfiguration.getMemoryNbrIntervalsBetweenWarnings() != null ) { |
101 | 0 | nbrIntervalsBetweenWarnings = Admin4JConfiguration.getMemoryNbrIntervalsBetweenWarnings(); |
102 | |
} |
103 | |
|
104 | 6 | String lowWatermarkMonitorIntervalStr = ServletUtils.getConfigurationSetting( |
105 | |
new String[]{PUBLIC_HANDLE + ".low.watermark.monitor.interval.millis"}, config); |
106 | 6 | if (lowWatermarkMonitorIntervalStr != null) { |
107 | |
try { |
108 | 3 | lowWatermarkMonitorIntervalInMillis = Long.parseLong(lowWatermarkMonitorIntervalStr); |
109 | 0 | } catch (Exception e) { |
110 | 0 | lowWatermarkMonitorIntervalInMillis = LowMemoryDetector.DEFAULT_LOW_WATERMARK_MONITOR_INTERVAL_IN_MILLIS; |
111 | 0 | this.logger.error("Invalid low.watermark.monitor.interval.millis parameter for MemoryMonitorStartupServlet. Default used. low.watermark.monitor.interval.millis={}", |
112 | |
lowWatermarkMonitorIntervalStr, e); |
113 | 3 | } |
114 | |
} |
115 | 3 | else if ( Admin4JConfiguration.getMemoryLowWatermarkMonitorIntervalInMillis() != null ) { |
116 | 0 | lowWatermarkMonitorIntervalInMillis = Admin4JConfiguration.getMemoryLowWatermarkMonitorIntervalInMillis(); |
117 | |
} |
118 | |
|
119 | 6 | String nbrLowWatermarkIntervalsStr = ServletUtils.getConfigurationSetting( |
120 | |
new String[]{PUBLIC_HANDLE + ".nbr.low.watermark.intervals"}, config); |
121 | 6 | if (nbrLowWatermarkIntervalsStr != null) { |
122 | |
try { |
123 | 3 | nbrLowWatermarkIntervals = Integer.parseInt(nbrLowWatermarkIntervalsStr); |
124 | 0 | } catch (Exception e) { |
125 | 0 | nbrLowWatermarkIntervals = LowMemoryDetector.DEFAULT_NBR_LOW_WATERMARK_INTERVALS; |
126 | 0 | this.logger.error("Invalid memory.threshold.pct parameter for MemoryMonitorStartupServlet. Default used. memory.threshold.pct={}", |
127 | |
nbrLowWatermarkIntervalsStr, e); |
128 | 3 | } |
129 | |
} |
130 | 3 | else if ( Admin4JConfiguration.getMemoryNbrLowWatermarkIntervals() != null ) { |
131 | 0 | nbrLowWatermarkIntervals = Admin4JConfiguration.getMemoryNbrLowWatermarkIntervals(); |
132 | |
} |
133 | |
|
134 | 6 | String notifierClassName = config.getInitParameter("notifier"); |
135 | 6 | Detector lowMemoryDetector = new LowMemoryDetector(NotifierUtils.configure(config, notifierClassName) |
136 | |
, memoryThresholdPct |
137 | |
, nbrIntervalsBetweenWarnings |
138 | |
, nbrLowWatermarkIntervals |
139 | |
, lowWatermarkMonitorIntervalInMillis); |
140 | 6 | memoryMonitorDaemon = new Daemon( lowMemoryDetector, "Admin4j Low Memory Detector", sleepIntervalMillis); |
141 | |
} |
142 | 0 | catch (Throwable t) { |
143 | 0 | logger.error("Error in boot sequence", t); |
144 | 0 | throw new ServletException(t); |
145 | 6 | } |
146 | 6 | } |
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
@Override |
152 | |
public boolean hasDisplay() { |
153 | 0 | return false; |
154 | |
} |
155 | |
|
156 | |
protected long getSleepIntervalMillis() { |
157 | 0 | return sleepIntervalMillis; |
158 | |
} |
159 | |
|
160 | |
protected int getMemoryThresholdPct() { |
161 | 3 | return memoryThresholdPct; |
162 | |
} |
163 | |
|
164 | |
protected int getNbrIntervalsBetweenWarnings() { |
165 | 3 | return nbrIntervalsBetweenWarnings; |
166 | |
} |
167 | |
|
168 | |
protected int getNbrLowWatermarkIntervals() { |
169 | 3 | return nbrLowWatermarkIntervals; |
170 | |
} |
171 | |
|
172 | |
protected long getLowWatermarkMonitorIntervalInMillis() { |
173 | 3 | return lowWatermarkMonitorIntervalInMillis; |
174 | |
} |
175 | |
|
176 | |
} |