Debian Codegraph @

logback-site/src/site/pages/manual/jmxConfig.html

raw
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
4 <html xmlns="http://www.w3.org/1999/xhtml">
5 <head>
6 <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
7
8 <link rel="stylesheet" type="text/css" href="../css/common.css" />
9 <link rel="stylesheet" type="text/css" href="../css/screen.css" media="screen" />
10 <link rel="stylesheet" type="text/css" href="../css/_print.css" media="print" />
11 <link rel="stylesheet" type="text/css" href="../css/prettify.css" media="screen" />
12
13 <title>Chapter 10: JMX Configuration</title>
14 </head>
15 <body onload="prettyPrint(); decorate();">
16 <script type="text/javascript">prefix='../';</script>
17 <script type="text/javascript" src="../js/prettify.js"></script>
18 <script type="text/javascript" src="../templates/header.js"></script>
19 <script type="text/javascript" src="../js/jquery-min.js"></script>
20 <script type="text/javascript" src="../js/decorator.js"></script>
21
22 <div id="left">
23 <noscript>Please turn on Javascript to view this menu</noscript>
24 <script src="../templates/left.js" type="text/javascript"></script>
25 </div>
26 <div id="right">
27 <script src="menu.js" type="text/javascript"></script>
28 </div>
29 <div id="content">
30
31 <h1>Chapter 10: JMX Configurator</h1>
32
33 <a href="jmxConfig_ja.html">&#x548C;&#x8A33; (Japanese translation)</a>
34
35 <p>As its name indicates, <code>JMXConfigurator</code> allows
36 configuration of logback via JMX. In a nutshell, it lets you
37 reconfigure logback from the default configuration file, from a
38 designated file or URL, list loggers and modify logger levels.
39 </p>
40
41 <h3>Using the JMX Configurator</h3>
42
43
44 <p>If your server run on JDK 1.6 or later, then you can just
45 invoke the <code>jconsole</code> application on the command line and
46 then connect to your server's MBeanServer. If you are running an
47 older JVM, then you should read the section on <a
48 href="#jmxEnablingServer">JMX enabling your server</a>.
49 </p>
50
51 <p><code>JMXConfigurator</code> is enabled by a single line in
52 your logback configuration file, as shown below:
53 </p>
54
55 <pre class="prettyprint source">&lt;configuration>
56 <b>&lt;jmxConfigurator /></b>
57
58 &lt;appender name="console" class="ch.qos.logback.core.ConsoleAppender">
59 &lt;layout class="ch.qos.logback.classic.PatternLayout">
60 &lt;Pattern>%date [%thread] %-5level %logger{25} - %msg%n&lt;/Pattern>
61 &lt;/layout>
62 &lt;/appender>
63
64 &lt;root level="debug">
65 &lt;appender-ref ref="console" />
66 &lt;/root>
67 &lt;/configuration></pre>
68
69 <p>After you connect to your server with <em>jconsole</em>, on the
70 MBeans panel, under "ch.qos.logback.classic.jmx.Configurator"
71 folder you should see several operations to choose from, as shown
72 in the figure below:
73 </p>
74
75 <h3 class="doAnchor" name="jmxConfigurator">Screen-shot of
76 <code>JMXConfigurator</code> viewed in <code>jconsole</code></h3>
77
78 <img src="images/chapters/jmxConfigurator/jmxConfigurator.gif" alt="jmxConfigurator"/>
79
80 <p>Thus, you can</p>
81
82 <ul>
83 <li>Reload logback configuration using the default configuration
84 file.</li>
85
86 <li>Reload the configuration with the specified URL.</li>
87 <li>Reload the configuration with the specified file.</li>
88
89 <li>Set the level of a specified logger. To set to null, pass
90 the string "null" as value.</li>
91 <li>Get the level of a specified logger. The returned value can be
92 null.</li>
93 <li>Get the <a href="architecture.html#effectiveLevel">effective
94 level</a> of a specified logger.</li>
95 </ul>
96
97 <p><code>JMXConfigurator</code> exposes the list of existing
98 loggers and a status list as attributes.</p>
99
100 <p>The status list can help you diagnose logback's internal
101 state.</p>
102
103 <img src="images/chapters/jmxConfigurator/statusList.gif" alt="statusList.gif"/>
104
105 <h3 class="doAnchor" name="leak">Avoiding memory leaks</h3>
106
107 <p>If your application is deployed in a web-server or an
108 application server, the registration of an
109 <code>JMXConfigurator</code> instance creates a reference from the
110 system class loader into your application which will prevent it
111 from being garbage collected when it is stopped or re-deployed,
112 resulting in a severe memory leak.</p>
113
114 <p>Thus, unless your application is a standalone Java application,
115 you MUST unregister the <code>JMXConfigurator</code> instance from
116 the JVM's Mbeans server. Invoking the <code>reset</code>() method
117 of the appropriate <code>LoggerContext</code> will automatically
118 unregister any JMXConfigurator instance. A good place to reset the
119 logger context is in the <code>contextDestroyed</code>()
120 method of a
121 <code>javax.servlet.ServletContextListener</code>. Here is sample
122 code:</p>
123
124 <pre class="prettyprint source">import javax.servlet.ServletContextEvent;
125 import javax.servlet.ServletContextListener;
126
127 import org.slf4j.LoggerFactory;
128 import ch.qos.logback.classic.LoggerContext;
129
130 public class MyContextListener implements ServletContextListener {
131
132 public void contextDestroyed(ServletContextEvent sce) {
133 <b>LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();</b>
134 <b>lc.stop();</b>
135 }
136
137 public void contextInitialized(ServletContextEvent sce) {
138 }
139 } </pre>
140
141
142 <!-- ============ Multiple web-applications ================== -->
143
144
145 <h2 class="doAnchor" name="multiple"><code>JMXConfigurator</code>
146 with multiple web-applications</h2>
147
148 <p>If you deploy multiple web-applications in the same server,
149 and if you have not overridden the default <a
150 href="contextSelector.html">context selector</a>, and if you
151 have placed a copy of <em>logback-*.jar</em> and
152 <em>slf4j-api.jar</em> under the <em>WEB-INF/lib</em> folder of
153 each web-application, then by default each <code>JMXConfigurator</code>
154 instance will be registered under the same name, that is,
155 "ch.qos.logback.classic:Name=default,Type=ch.qos.logback.classic.jmx.JMXConfigurator". In
156 other words, by default the various <code>JMXConfigurator</code>
157 instances associated with the logger contexts in each of your
158 web-applications will collide.
159 </p>
160
161 <p>To avoid such undesirable collisions, you simply <a
162 href="configuration.html#contextName">set the name of your
163 application's logging context</a> and <code>JMXConfigurator</code>
164 will automatically use the name you have set.
165 </p>
166
167 <p>For example, if you deploy two web-applications named "Koala"
168 and "Wombat", then you would write in Koala's logback
169 configuration
170 </p>
171
172 <pre class="prettyprint source">&lt;configuration>
173 <b>&lt;contextName>Koala&lt;/contextName></b>
174 &lt;jmxConfigurator/>
175 ...
176 &lt;configuration></pre>
177
178 <p>and in Wombat logback configuration file, you would write:</p>
179
180 <pre class="prettyprint source">&lt;configuration>
181 <b>&lt;contextName>Wombat&lt;/contextName></b>x
182 &lt;jmxConfigurator/>
183 ...
184 &lt;configuration></pre>
185
186 <p>In jconsole's MBeans panel, you would see two distinct
187 <code>JMXConfigurator</code> instances:</p>
188
189 <img src="images/chapters/jmxConfigurator/multiple.gif" alt="multiple.gif"/>
190
191 <p>You may fully control the name under which JMXConfigurator is
192 registered with an MBeans server with the help of the "objectName"
193 attribute of the <code>&lt;jmxConfigurator></code> element.</p>
194
195 <!-- ============ JMX enabling your server ================== -->
196
197 <h3 class="doAnchor" name="jmxEnablingServer">JMX enabling your
198 server</h3>
199
200 <p>If your server runs with JDK 1.6 or later, your server should
201 be JMX enabled by default.</p>
202
203 <p>For older JVMs, we suggest that you refer to the JMX-related
204 documentation of your web-server. Such documentation is available
205 for both <a
206 href="http://tomcat.apache.org/tomcat-6.0-doc/monitoring.html">Tomcat</a>
207 and <a
208 href="http://docs.codehaus.org/display/JETTY/JMX">Jetty</a>. In
209 this document, we briefly describe the required configuration
210 steps for Tomcat and Jetty.
211 </p>
212
213 <!-- ================ Configuring Jetty ================== -->
214
215 <h4>Enabling JMX in Jetty (tested under JDK 1.5 and JDK 1.6)</h4>
216
217 <p>The following has been tested under JDK 1.5 and 1.6. Under JDK
218 1.6 and later, your server is JMX enabled by default and you can,
219 but do not need to, follow the steps discussed below. Under JDK
220 1.5, adding JMX support in Jetty requires a number of additions to
221 the <em>$JETTY_HOME/etc/jetty.xml</em> configuration file. Here
222 are the elements that need to be added:
223 </p>
224
225 <pre class="prettyprint source">&lt;Call id="MBeanServer" class="java.lang.management.ManagementFactory"
226 name="getPlatformMBeanServer"/>
227
228 &lt;Get id="Container" name="container">
229 &lt;Call name="addEventListener">
230 &lt;Arg>
231 &lt;New class="org.mortbay.management.MBeanContainer">
232 &lt;Arg>&lt;Ref id="MBeanServer"/>&lt;/Arg>
233 &lt;Call name="start" />
234 &lt;/New>
235 &lt;/Arg>
236 &lt;/Call>
237 &lt;/Get> </pre>
238
239 <p>If you wish to access the MBeans exposed by Jetty via the
240 <code>jconsole</code> application, then you need to start Jetty after
241 having set the "com.sun.management.jmxremote" Java system
242 property.
243 </p>
244
245 <p>For a standalone version of Jetty, this translates to: </p>
246
247
248 <p class="source">java <b>-Dcom.sun.management.jmxremote</b> -jar start.jar [config files]</p>
249
250 <p>And if you wish to launch Jetty as a Maven plugin, then you
251 need to set the "com.sun.management.jmxremote" system property via
252 the <code>MAVEN_OPTS</code> shell variable:
253 </p>
254
255 <p class="source"><b>MAVEN_OPTS="-Dcom.sun.management.jmxremote</b>"
256 mvn jetty:run</p>
257
258 <p>You can then access the MBeans exposed by Jetty as well as
259 logback's <code>JMXConfigurator</code> via
260 <code>jconsole</code>.</p>
261
262 <img src="images/chapters/jmxConfigurator/jconsole15_jetty.gif" alt="jconsole15_jetty.gif"/>
263
264 <p>After you are connected, you should be able to access
265 <code>JMXXConfigurator</code> as shown in the <a
266 href="#jmxConfigurator">screenshot</a> above.</p>
267
268 <h4>MX4J with Jetty (tested under JDK 1.5 and 1.6)</h4>
269
270 <p>If you wish to access <code>JMXConfigurator</code> via MX4J's
271 HTTP interface and assuming you have already downloaded <a
272 href="http://mx4j.sourceforge.net/">MX4J</a>, you then need to
273 modify the Jetty configuration file discussed previously by adding
274 an instruction to set the management port.
275 </p>
276
277 <pre class="prettyprint source">&lt;Call id="MBeanServer"
278 class="java.lang.management.ManagementFactory"
279 name="getPlatformMBeanServer"/>
280
281 &lt;Get id="Container" name="container">
282 &lt;Call name="addEventListener">
283 &lt;Arg>
284 &lt;New class="org.mortbay.management.MBeanContainer">
285 &lt;Arg>&lt;Ref id="MBeanServer"/>&lt;/Arg>
286 <b>&lt;Set name="managementPort">8082&lt;/Set></b>
287 &lt;Call name="start" />
288 &lt;/New>
289 &lt;/Arg>
290 &lt;/Call>
291 &lt;/Get>
292 </pre>
293
294 <p>Moreover, <em>mx4j-tools.jar</em> needs to be added to Jetty's
295 class path.
296 </p>
297
298 <p>If you are running Jetty as a Maven plug-in, then you need to add
299 <em>mx4j-tools</em> as a dependency.</p>
300
301 <pre class="prettyprint source">&lt;plugin>
302 &lt;groupId>org.mortbay.jetty&lt;/groupId>
303 &lt;artifactId>maven-jetty-plugin&lt;/artifactId>
304 &lt;configuration>
305 &lt;jettyConfig>path/to/jetty.xml&lt;/jettyConfig>
306 ...
307 &lt;/configuration>
308 <b>&lt;dependencies>
309 &lt;dependency>
310 &lt;groupId>mx4j&lt;/groupId>
311 &lt;artifactId>mx4j-tools&lt;/artifactId>
312 &lt;version>3.0.1&lt;/version>
313 &lt;/dependency>
314 &lt;/dependencies></b>
315 &lt;/plugin></pre>
316
317 <p>After Jetty is started with the above configuration,
318 <code>JMXConfigurator</code> will be available at the following
319 URL (search for "ch.qos.logback.classic"):
320 </p>
321
322 <p class="source"><a href="http://localhost:8082/">http://localhost:8082/</a></p>
323
324 <p>Below is a screen shot view of the MX4J interface.</p>
325
326 <img src="images/chapters/jmxConfigurator/mx4j_jetty.gif" alt="mx4j_jetty.gif"/>
327
328
329 <!-- ================ Tomcat ================== -->
330
331 <h4>Configuring JMX for Tomcat (tested under JDK 1.5 and 1.6)</h4>
332
333 <p>If you are using JDK 1.6 and later, your server is already JMX
334 enabled by default and you can, but do not need to, follow the
335 steps discussed below. Under JDK 1.5, Tomcat requires the addition
336 of the following lines to the
337 <em>$TOMCAT_HOME/bin/catalina.bat/sh</em> shell script:
338 </p>
339
340 <p class="source">CATALINA_OPTS="-Dcom.sun.management.jmxremote"</p>
341
342 <p>Once started with these options, MBeans exposed by Tomcat as
343 well logback's <code>JMXConfigurator</code> can be accessed with
344 <code>jconsole</code> by issuing the following command in a shell:
345 </p>
346
347 <p class="source">jconsole</p>
348
349 <img src="images/chapters/jmxConfigurator/jconsole15_tomcat.gif" alt="jconsole15_tomcat.gif"/>
350
351 <p>After you are connected, you should be able to access
352 <code>JMXXConfigurator</code> as shown in the <a
353 href="#jmxConfigurator">screenshot</a> above.</p>
354
355
356 <h4>MX4J with Tomcat (tested under JDK 1.5 and 1.6)</h4>
357
358 <p>You might prefer to access JMX components via a web-based
359 interface provided by MX4J. In that case, here are the required
360 steps:
361 </p>
362
363 <p>Assuming you have already downloaded <a
364 href="http://mx4j.sourceforge.net/">MX4J</a>, place the
365 <em>mx4j-tools.jar</em> file under the <em>$TOMCAT_HOME/bin/</em>
366 directory. Then, add the following lines to the
367 <em>$TOMCAT_HOME/bin/catalina.sh</em> configuration file:
368 </p>
369
370 <p class="source">&lt;!-- at the beginning of the file -->
371 CATALINA_OPTS="-Dcom.sun.management.jmxremote"
372
373 &lt;!-- in the "Add on extra jar files to CLASSPATH" section -->
374 CLASSPATH="$CLASSPATH":"$CATALINA_HOME"/bin/mx4j-tools.jar</p>
375
376 <p>Finally, declare a new <code>Connector</code> in the
377 <em>$TOMCAT_HOME/conf/server.xml</em> file:
378 </p>
379
380
381 <pre class="prettyprint source">&lt;Connector port="0"
382 handler.list="mx"
383 mx.enabled="true"
384 mx.httpHost="localhost"
385 mx.httpPort="8082"
386 protocol="AJP/1.3" /></pre>
387
388 <p>Once Tomcat is started, you should be able to find
389 JMXConfigurator by pointing your browser at the following URL
390 (search for "ch.qos.logback.classic"):
391 </p>
392
393 <p class="source"><a href="http://localhost:8082">http://localhost:8082/</a></p>
394
395 <p>Below is a screen shot view of the MX4J interface.</p>
396
397 <img src="images/chapters/jmxConfigurator/mx4j_tomcat.gif" alt="mx4j_tomcat.gif"/>
398
399
400
401
402 <script src="../templates/footer.js" type="text/javascript"></script>
403 </div>
404 </body>
405 </html>