jdk/src/sample/share/jmx/jmx-scandir/index.html
author darcy
Tue, 19 May 2015 13:30:03 -0700
changeset 30678 a8b7fd8ede97
parent 27565 729f9700483a
permissions -rw-r--r--
8075284: fix up miscellaneous TM constructions Reviewed-by: lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
<html>
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
  <head>
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
<!--
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 21278
diff changeset
     6
 Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 Redistribution and use in source and binary forms, with or without
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 modification, are permitted provided that the following conditions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 are met:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
   - Redistributions of source code must retain the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
     notice, this list of conditions and the following disclaimer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
   - Redistributions in binary form must reproduce the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
     notice, this list of conditions and the following disclaimer in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
     documentation and/or other materials provided with the distribution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
   - Neither the name of Oracle nor the names of its
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
     contributors may be used to endorse or promote products derived
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
     from this software without specific prior written permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
-->
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
30678
a8b7fd8ede97 8075284: fix up miscellaneous TM constructions
darcy
parents: 27565
diff changeset
    36
    <title>JMX&trade; "scandir" Example</title>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
  </head>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
  <body>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    39
30678
a8b7fd8ede97 8075284: fix up miscellaneous TM constructions
darcy
parents: 27565
diff changeset
    40
  <h1><center>Java&trade; Management Extensions (JMX&trade;) <i>scandir</i> Example</center></h1>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    41
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
  <h2><a name="h2-Introduction">Introduction</a></h2>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
  <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
  <p>The JMX <i>scandir</i> example is an application that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     scans parts of a filesystem - e.g. a set of directories
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     used by a number of lab machines when running tests - in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     order to clean up and optimize disk space by removing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     obsolete files - e.g. files that are leaked by the test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     suites running on those machines, like coredump files, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     temporary files that might remain after a test crash.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     It could also serve as a basis for an application that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     would monitor disk usage and suggest removal of old big
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     long-unaccessed files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
  <p>The JMX <i>scandir</i> example does not however implement
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     the full fledged logic that such an application might
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     have. It implements a subset of this logic which is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     sufficient to demonstrate common patterns and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     solutions used when implementing a monitoring and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     management interface for an application with JMX
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     Technology.</p>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    62
  <p>This example is an advanced JMX example, which presents
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     advanced JMX concepts. It is assumed that the reader is already
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     familiar with the JMX API. Newcomers to JMX Technology are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     invited to have a look at the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     href="http://java.sun.com/javase/6/docs/technotes/guides/jmx/"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     >JMX API Overview, Tutorial and Examples</a> before going any further.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
  <p></p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
      <hr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
  <blockquote>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    72
    <u>Note:</u> This example was developed using <a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     href="http://www.netbeans.org">NetBeans 5.0 IDE</a>. The instructions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     given in this document to build, run, and test the example assume that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     you have at your disposal:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     <ul><li>either <a href="http://www.netbeans.org">NetBeans 5.0 IDE</a>,</li>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    77
         <li>or <a href="http://ant.apache.org/">Apache Ant 1.6.5</a> and
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
             <a href="http://sourceforge.net/projects/junit/">JUnit 3.8.1 or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
             3.8.2</a><br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
             (JUnit is only needed to run the example's unit tests).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
         </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     </ul>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    83
     <p><a name="setup">In order to build the example</a>,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     <u>you may need to copy the jmx-scandir</u>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    85
     directory to somewhere where you have write permissions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     <br>In that case, you will need to update the <i>nbjdk.home</i> variable
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    87
     in the copied <i><a href="build.properties">build.properties</a></i>
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    88
     file located at the root of the copied project directory.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     Please make sure that this variable points to the JDK 6 home directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     </p>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    91
     <p>If you wish to run the testsuite from within the <a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     href="http://www.netbeans.org">NetBeans IDE</a> you will also have
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    93
     to set the <i>libs.junit.classpath</i> variable in
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    94
     <a href="build.properties">build.properties</a>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     The <i>libs.junit.classpath</i>  variable should point to your
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
    96
     <a href="http://sourceforge.net/projects/junit/">junit.jar</a>,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     version 3.8.1 or 3.8.2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
  </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     <hr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     <p></p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     <p><u>Table Of Contents:</u></p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
  <p><center>[<a href="#h2-Generating">Generating&nbsp;the&nbsp;Java&nbsp;Documentation</a>]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
  [<a href="#h2-Overview">Overview&nbsp;of&nbsp;the&nbsp;<i>scandir</i>&nbsp;Example</a>]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
  [<a href="#h2-API-Doc">API&nbsp;Documentation&nbsp;and&nbsp;Sources</a>]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
  [<a href="#h2-Patterns">Patterns,&nbsp;Best&nbsp;Practices,&nbsp;and&nbsp;Common&nbsp;Pitfalls</a>]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
  [<a href="#h2-Testing">Testing&nbsp;the&nbsp;<i>scandir</i>&nbsp;Example</a>]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
  [<a href="#h2-Running">Running&nbsp;the&nbsp;<i>scandir</i>&nbsp;Example</a>]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
  [<a href="#h2-Playing">Playing&nbsp;with&nbsp;JConsole</a>]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
  [<a href="#h2-Turning">Turning&nbsp;the&nbsp;example&nbsp;into&nbsp;a&nbsp;Secure&nbsp;JMX&nbsp;Application</a>]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
  [<a href="#h2-Connecting">Connecting&nbsp;to&nbsp;the&nbsp;Secure&nbsp;JMX&nbsp;Application</a>]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
  [<a href="#h2-Conclusion">Conclusion</a>]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
  [<a href="#h2-References">References</a>]</center></p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
  </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
  <h2><a name="h2-Generating">Generating the Java Documentation</a></h2>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    <ul>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   119
        <p>Before reading further, you will need to generate the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        Java Documentation for the example's sources.</p>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   121
        <p>In the example root directory (where the <code>build.xml</code>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
           file is located) run the following command:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
           <pre>ant javadoc</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        </p>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   125
        <p>Alternatively you can open the jmx-scandir project with the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
           NetBeans IDE and generate the Javadoc from its <code>Build</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
           menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        </p>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   129
        <p>If building the documentation fails, please make sure to read the
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   130
           <a href="#setup">note</a> at the beginning of this document.</p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    </ul>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   132
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
  <h2><a name="h2-Overview">Overview of the <i>scandir</i> Example</a></h2>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   134
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
  <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    <p>The JMX <i>scandir</i> example is built around the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    following MBeans:</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        <li>The first MBean we will present here is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
href="dist/javadoc/com/sun/jmx/examples/scandir/DirectoryScannerMXBean.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
title="A DirectoryScannerMXBean looks for file matching a given set of criteria, starting at a given root."
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        >DirectoryScannerMXBean</a>. <br>A
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        <code>DirectoryScannerMXBean</code> is an MBean that scans a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        file system starting at a given root directory, and then looks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        for files that match the given criteria.  When such a file is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        found, the <code>DirectoryScannerMXBean</code> takes the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        action for which it was configured: emit a notification,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        <i>and/or</i> log a <code>record</code> for this file,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        <i>and/or</i> delete that file. The code that would actually
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        delete the file is commented out - so that nothing valuable is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        lost if the example is run by mistake on the wrong set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        directories.<br> <code>DirectoryScannerMXBeans</code> are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        created by the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
href="dist/javadoc/com/sun/jmx/examples/scandir/ScanManagerMXBean.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
title="The ScanManagerMXBean is the main MBean of the scandir application"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        >ScanManagerMXBean</a> - see next item on the list, from its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        configuration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            The <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
href="dist/javadoc/com/sun/jmx/examples/scandir/ScanManagerMXBean.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
title="The ScanManagerMXBean is the main MBean of the scandir application"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            >ScanManagerMXBean</a> is the actual entry point of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            application. It reads the application's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            configuration, and from that configuration,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            will create a <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
href="dist/javadoc/com/sun/jmx/examples/scandir/ResultLogManager.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
title="The ResultLogManager is in charge of managing result logs"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        >ResultLogManager</a> and some <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
href="dist/javadoc/com/sun/jmx/examples/scandir/DirectoryScannerMXBean.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
title="A DirectoryScannerMXBean looks for file matching a given set of criteria, starting at a given root."
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        >DirectoryScannerMXBeans</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            <br>The <code>ScanManagerMXBean</code> lets you start, stop, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            schedule directory scans. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            <code>ScanManagerMXBean</code> is a singleton
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            MBean: there can be at most one instance of such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            an MBean registered in a given MBeanServer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        <li>The <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
href="dist/javadoc/com/sun/jmx/examples/scandir/ScanDirConfigMXBean.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
title="The ScanDirConfigMXBean is in charge of the configuration"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
           >ScanDirConfigMXBean</a> is an MBean which is able to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
           load/save the configuration to/from an XML file. It
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
           will also let you modify that configuration - by e.g.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
           creating new directory scanners in there.
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   187
           The corresponding MBeans will be created later, only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
           when you later
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   189
           ask the <code><a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
href="dist/javadoc/com/sun/jmx/examples/scandir/ScanManagerMXBean.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
title="The ScanManagerMXBean is the main MBean of the scandir application"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
             >ScanManagerMXBean</a> </code> to apply the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
           configuration again.<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
           The <code>ScanDirConfigMXBean</code> is created by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
           <code>ScanManagerMXBean</code>, when the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
           <code>ScanManagerMXBean</code> is registered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
           It is also possible to create an alternate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
           <code>ScanDirConfigMXBean</code>, and to switch the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
           <code>ScanDirConfigMXBean</code> to use one or the other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
           configuration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
           <br>An example of XML configuration file is given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
           <a href="src/etc/testconfig.xml"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
           title="An Example Of Configuration"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
           >here</a>. Although you could edit such a file by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
           hand, it is easier to do it programmatically (or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
           with <a href="#JConsole">JConsole</a>) through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
           the <code>ScanDirConfigMXBean</code> interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        <li>The <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
href="dist/javadoc/com/sun/jmx/examples/scandir/ResultLogManagerMXBean.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
title="The ResultLogManagerMXBean is in charge of managing result logs"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        >ResultLogManagerMXBean</a> is in charge of managing result logs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        <br>Directory Scanners can be configured to log a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
href="dist/javadoc/com/sun/jmx/examples/scandir/config/ResultRecord.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
title="A ResultRecord contains information about a file matching the criteria of a Directory Scanner"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        >ResultRecord</a> whenever they take action upon a file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        matches their criteria. The <code>ResultLogManagerMXBean</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        responsible for logging these result records.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        The <code>ResultLogManagerMXBean</code> can be configured to log
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        such records to a flat file, or into a log held in memory, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        both. Both logs (file and memory) can be configured with a
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   223
        maximum capacity.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        <br>When the maximum capacity of the memory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        log is reached, its first entry (i.e. its oldest entry) is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        removed to make place for the latest one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        <br>When the maximum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        capacity of the file log is reached, the file is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        renamed by appending a tilde '~' to its name and a
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   230
        new result log is created.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        <br>The <code>ResultLogManagerMXBean</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        will let you interactively clear these result logs, change their
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        capacity, and decide where (memory or file) to log.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        The memory log is useful in that its content can be interactively
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        returned by the <code>ResultLogManagerMXBean</code>, while
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        the file log doesn't have this facility.<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        The result logs are intended to be used by e.g. an offline
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        program that would take some actions on the files that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        matched the scan criteria:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        <br>The <i>scandir</i> application
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        could be configured to only produce logs (i.e. takes no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        action but logging the matching files), and the real
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        action could be performed by another program or module (e.g. mail the result log to the engineer who
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        maintains the lab, or parse that log and delete all the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        files listed there, or parse the log and prepare and send
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        a single mail to each owner of matching files, containing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        the list of files they should consider deleting).<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        The <code>ResultLogManagerMXBean</code> is a singleton
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   249
        MBean created by the <code><a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
href="dist/javadoc/com/sun/jmx/examples/scandir/ScanManagerMXBean.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
title="The ScanManagerMXBean is the main MBean of the scandir application"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
             >ScanManagerMXBean</a> </code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        which reads and writes its configuration from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        <code><a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
href="dist/javadoc/com/sun/jmx/examples/scandir/ScanDirConfigMXBean.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
title="The ScanDirConfigMXBean is in charge of the configuration"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
           >ScanDirConfigMXBean</a></code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    <p>An application <code>main()</code> method is
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   261
       provided in the <a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
       href="dist/javadoc/com/sun/jmx/examples/scandir/ScanDirAgent.html"
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   263
       >ScanDirAgent</a> class. The <code>main()</code> simply registers
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   264
       a <code><a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
href="dist/javadoc/com/sun/jmx/examples/scandir/ScanManagerMXBean.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
title="The ScanManagerMXBean is the main MBean of the scandir application"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
             >ScanManagerMXBean</a> </code> in the platform MBeanServer, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
       then waits for someone to call <code>close()</code> on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
       <code>ScanManagerMXBean</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     <p>When the <code>ScanManagerMXBean</code> is registered, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        will create a default <code><a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
href="dist/javadoc/com/sun/jmx/examples/scandir/ScanDirConfigMXBean.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
title="The ScanDirConfigMXBean is in charge of the configuration"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
           >ScanDirConfigMXBean</a></code> bound
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        to a default XML config file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     <p>The application's default XML config file is determined as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        follows:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            <li>If the property <code>scandir.config.file</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                defined, the default application file will be the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                file pointed to by this property. If that file
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   284
                doesn't exist, it will be created when
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                <code>ScanDirConfigMXBean.save()</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            <li>Otherwise the application config file is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                assumed to be a file called <code>jmx-scandir.xml</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                located in the user's directory (as defined by
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   291
                the System property <code>user.home</code>).
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   292
                If that file doesn't exists, it will be created when
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                <code>ScanDirConfigMXBean.save()</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        <p>It is worth noting that this project is defined to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
           run with the following properties:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
           <pre>-Djava.util.logging.config.file=logging.properties</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
           <pre>-Dscandir.config.file=src/etc/testconfig.xml</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
           With <code>ScanDirAgent</code> defined as the project's
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   302
           main class. Hence when you invoke from the NetBeans IDE
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   303
           <i>Run Project</i> on the <i>jmx-scandir</i> project,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
           or <i>Run file</i> on the <code>ScanDirAgent</code>, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
           application starts with the test configuration provided in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
           <a href="src/etc/testconfig.xml"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
           title="An Example Of Configuration"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
           >src/etc/testconfig.xml</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
  </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
  <h2><a name="h2-API-Doc">API Documentation and Sources</a></h2>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
  <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
      <p>Once generated, the Javadoc of example classes can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
      be found starting from <a href="dist/javadoc/index.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
      title="The API Documentation"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
      ><code>dist/javadoc/index.html</code></a>.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
      <p>You can view the sources in the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
      href="src"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
      title="The Example Source Tree"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
      ><code>src</code></a> subdirectory.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
  </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
  <h2><a name="h2-Patterns">Patterns, Best Practices, and Common Pitfalls</a></h2>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   323
  <ul>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
  <p>This section discusses some common patterns and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     design choices that this example demonstrates, and some pitfalls that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     it avoids.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
  </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
  <h3>MBeans or MXBeans?</h3>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
  <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
  <p>What is an MXBean? MXBeans made their appearance in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     J2SE 5.0 (Tiger), with the Management and Monitoring
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     API of the JVM. However, Java SE 6 is the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     Java SE release that contains a standard framework which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     makes it possible to create and register your own MXBeans.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
  </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
  <p>MXBeans are a special kind of MBean, which once registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     in the MBeanServer, get automatically transformed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     OpenMBeans. From a developer point of view, nothing changes:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     A Wombat MBean can become an MXBean simply by renaming
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     its <code>WombatMBean</code> interface into <code>WombatMXBean</code>.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
  <p>Using MXBeans rather than plain Standard MBean brings its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     own advantages:</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
         <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
             Generic tools, like JConsole, will be able to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
             display and interact with your MXBeans nicely, even
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
             if your MXBean interfaces reference custom types
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
             - e.g. custom Java enums. This is because all the types
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
             exposed by your MXBeans are converted to Open Types.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
             Just look at the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
href="dist/javadoc/com/sun/jmx/examples/scandir/ScanDirConfigMXBean.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
title="The ScanDirConfigMXBean is in charge of the configuration"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
             >ScanDirConfigMXBean</a> with JConsole and you will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
             understand the benefits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
         </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
         <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
             When writing a programmatic client, you can obtain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
             a proxy that implements the original MXBean interface,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
             and forget about the Open Type conversion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
             The JUnit unit tests that come with this example
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
             use this feature very widely. Have a look at them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
         </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
         <li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            The MXBean framework also lets you nicely navigate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            from one MXBean to another: your MXBeans can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            have attributes and parameters which are proxies
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            to other MXBeans! We demonstrate this in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
href="dist/javadoc/com/sun/jmx/examples/scandir/ScanManagerMXBean.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
title="The ScanManagerMXBean is the main MBean of the scandir application"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            >ScanManagerMXBean</a> which exposes a list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            of <code><a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
href="dist/javadoc/com/sun/jmx/examples/scandir/DirectoryScannerMXBean.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
title="A DirectoryScannerMXBean looks for file matching a given set of criteria, starting at a given root."
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        >DirectoryScannerMXBean</a></code> and points
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            towards a <code><a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
href="dist/javadoc/com/sun/jmx/examples/scandir/ScanDirConfigMXBean.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
title="The ScanDirConfigMXBean is in charge of the configuration"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
           >ScanDirConfigMXBean</a></code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
         </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     <p>In short, MXBeans are so much easier to use that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        this example doesn't even have a single regular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        Standard MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     <p>See also <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
href="http://weblogs.java.net/blog/emcmanus/archive/2006/02/what_is_an_mxbe.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
title="What is an MXBean?"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     >What is an MXBean?</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     and <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
href="http://weblogs.java.net/blog/emcmanus/archive/2006/06/intermxbean_ref.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
title="Inter-MXBean references"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     >Inter-MXBean References</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     </p>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   395
     <blockquote><u>Hint:</u> In order to simplify the task of coding a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        JMX programmatic client, we recommend that getters, setters, and
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   397
        operations defined in MBean and MXBean interfaces throw
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        <code>IOException</code>. Proxy objects will then be able
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        to rethrow directly any <code>IOException</code> received from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        their underlying MBean Server connection, without wrapping
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        them into <code>UndeclaredThrowableExceptions</code>.<br>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   402
        Since the life cycle of the proxy object is not directly tied to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        the life cycle of the MBean it proxies, you may also want to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        have all methods in the MBean or MXBean interface throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        <code>InstanceNotFoundException</code> or more generally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        <code>JMException</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
  </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
  <h3>MBean Names - aka ObjectNames</h3>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
  <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
  <p>As you must know if you've been studying JMX, MBeans are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     named objects. The names of MBeans are represented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     instances of <code>ObjectName</code>. An ObjectName is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     composed of a <i>domain</i>, followed by a colon ':',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     followed by a comma-separated list of <i>key=value</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     pairs.<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     The ordering of the <i>key=value</i> pairs is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     important, but <code>ObjectNames</code> are case sensitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     (both keys and values are case sensitive) and <b>white space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     is not ignored</b>.<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     A common pitfall for JMX beginners is to inadvertently
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     insert white space after commas into an ObjectName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     and expect that two ObjectNames which differ only by such white
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     space will be considered identical. This is not the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     case.<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     As an example, the ObjectName '<b><code>D:k1=v1, k2=v2, k3=v3</code></b>' has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     three keys, which are '<b><code>k1</code></b>', '<b><code> k2</code></b>',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     and '<b><code> k3</code></b>': beware
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     of the space in the name of the second and third
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     keys!<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     It is therefore a different ObjectName from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     '<b><code>D:k1=v1,k2=v2,k3=v3</code></b>' (the keys are now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     '<b><code>k1</code></b>', '<b><code>k2</code></b>', and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     '<b><code>k3</code></b>'), but the same ObjectName as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     '<b><code>D: k2=v2, k3=v3,k1=v1</code></b>', and yet different
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     from '<b><code>D:k2=v2, k3=v3, k1=v1</code></b>'!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     <p>In this example, we are following the rules
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        for ObjectName suggested in the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
href="http://java.sun.com/products/JavaManagement/best-practices.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        >JMX Best Practices</a>:</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
         <li>ObjectNames should be <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
         href="http://java.sun.com/products/JavaManagement/best-practices.html#mozTocId654884"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
         >predictable</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
         </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        <li>The domain part of our ObjectNames starts with a Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            package name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        <li>Our ObjectNames contain a <code>type=</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            key property. This property is different for every
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            object type in our domain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        <li>For every ObjectName with a given type, we have the same set of key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            properties with the same syntax and semantics for their values - in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            fact we only use an additional <code>name=</code> key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        <li>When there can only be one instance of a given type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            there aren't any other key properties than <code>type=</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            The ObjectNames of the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
href="dist/javadoc/com/sun/jmx/examples/scandir/ScanManagerMXBean.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
title="The ScanManagerMXBean is the main MBean of the scandir application"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            >ScanManagerMXBean</a> and <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
href="dist/javadoc/com/sun/jmx/examples/scandir/ResultLogManagerMXBean.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
title="The ResultLogManagerMXBean is in charge of managing result logs"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        >ResultLogManagerMXBean</a>, which are both singleton MBeans, are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        composed in this way.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        <li>When there can be several instances of a given type,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            we differentiate them by further key properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            To achieve this, we are using the most usual key property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            in addition to <code>type=</code>: the <code>name=</code> key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            In this example, a key property list of the form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            <code>type=X,name=Y</code> is always enough to uniquely name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            an MBean. Tools like jconsole are usually aware
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            of the semantics of the <code>type=</code> key and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            <code>name=</code> key, and are therefore able to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            display this form of name in a way that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            is easier to read than other name forms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     <p>The rules listed above are implemented by a couple
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        of static helper functions in the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
href="src/com/sun/jmx/examples/scandir/ScanManager.java"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
title="ScanManager.java"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
      >ScanManager</a> class. See the code of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
      <b><code>makeSingletonName</code></b> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
      <b><code>makeMBeanName</code></b> methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
  </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
  <h3>Inter MBean Navigation</h3>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
  <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
  <p>One of the most common problems that needs to be solved
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     when designing a management interface with JMX is to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     choose a representation for inter-MBean relationships.<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     Prior to Java 6, there were basically three possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     choices:</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
         <li><b>Make the relation appear in the ObjectName</b>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
             For instance, if MBean B was contained in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
             MBean A, you could choose to name MBean B so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
             that its parent relationship with MBean A
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
             appeared in its name. <br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
             The obvious limitation of this solution is that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
             it only allows to model one such relation (an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
             MBean has only one name) and the relation must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
             be fixed - it cannot change during the life of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
             the MBean since the name of an MBean cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
             change.<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
             This scheme is therefore mostly used when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
             the application MBeans are modeling objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
             which are conceptually contained within
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
             each other in a tree-like structure.
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   513
             <br>For instance, most MBean names defined by
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
             <a href="http://jcp.org/en/jsr/detail?id=77"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
              >J2EE Management (JSR 77)</a> follow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
              this scheme.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
         </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
         <li><b>Design getters and setters (or operations) which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
             return <code>ObjectName</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
             <code>ObjectName[]</code> values</b>. The ObjectNames
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
             point to the MBeans which are related to that
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   522
             object. For instance , <a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
             href="http://glassfish.dev.java.net/"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
             title="Open Source Java EE 5 Application Server"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
             >GlassFish</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
             defines MBeans which also use this pattern.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
         </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
         <li><b>Use the JMX RelationService</b>. The JMX RelationService
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
             is quite powerful, but simple relationships often
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
             do not justify that overhead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
         </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     <p>In Java 6, these three possibilities still remain, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        the new MXBean framework brings up an interesting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        alternative. Instead of returning an ObjectName or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        an ObjectName array, <b>an MXBean can return a proxy</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        to its related MXBeans. This is how we have chosen to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        implement our inter MBean relationships in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        <br>For instance the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        <code>ScanManagerMXBean</code>/<code>DirectoryScannerMXBean</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        relationship and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        <code>ScanManagerMXBean</code>/<code>ScanDirConfigMXBean</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        relationships are implemented in this way.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        The additional benefit, as compared to returning ObjectNames or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        using the RelationService is that interface type of the MBeans
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        which are pointed to by the relationship becomes directly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        apparent. The method:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            public Map&lt;String,DirectoryScannerMXBean&gt; getDirectoryScanners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        makes it immediately obvious that the MBeans to which we point are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
href="dist/javadoc/com/sun/jmx/examples/scandir/DirectoryScannerMXBean.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
title="A DirectoryScannerMXBean looks for file matching a given set of criteria, starting at a given root."
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
        >DirectoryScannerMXBeans</a>. It would have been much less obvious in prior
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
        versions of Java SE, were the returned type would have had to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        <code>Map&lt;String,ObjectName&gt;</code>, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        even worse just <code>Map</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     <p>However, it must be clear that the behaviour will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        quite different when an MXBean is returned as compared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        to when a simple bean is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     <p>When an MXBean is returned, the remote client sees either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        an ObjectName, if it is a generic client like jconsole, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        a proxy to a remote MXBean, if the client is working with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        MXBean interface. Invoking an operation on one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        proxy returned by a method such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        <code>getDirectoryScanners</code> will cause the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        MBean to be invoked on the remote server side.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     <p>If <code>getDirectoryScanners</code> were
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        defined as:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            public Map&lt;String,DirectoryScannerConfig&gt; getDirectoryScanners();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        then invoking a method on one of the returned objects
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        would have absolutely no effect on the remote
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        server side - because the returned objects in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        case would simply be a bunch of serialized data objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     <p>It is worth noting that although an MXBean interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        can have getters and operations which return an MXBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        interface, a regular standard MBean shouldn't have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        any getters or methods which return MBean interfaces or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        MXBean interfaces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     <p>For more information see also <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
href="http://weblogs.java.net/blog/emcmanus/archive/2006/06/intermxbean_ref.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
title="Inter-MXBean references"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     >Inter-MXBean References</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
  </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
  <h3>The MBeanRegistration interface, or how an MBean can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
      know or provide its own name</h3>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
  <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
      <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        Sometimes, an MBean needs to have a reference to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        MBeanServer in which it is registered, or needs to know
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        with which ObjectName it has been registered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
      </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
      <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
         Sometimes also, an MBean may need to perform some
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
         checks before being registered, or will need
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   607
         to carry out some actions right after it has been
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
         successfully registered in the MBeanServer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
      </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
      <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        Sometimes again, an MBean may need to perform some
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        checks, or some cleaning actions, just before, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        just after, it is unregistered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
      </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
      <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
      When an MBean has such needs, the easiest solution
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
      for it is to implement the <code>MBeanRegistration</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
      interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
      </p>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   620
      <p>The <code>MBeanRegistration</code> interface is a callback
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   621
      interface which defines pre and post registration and
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
      unregistration callbacks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
      </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
      <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
       When an MBean implementing this interface is created
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   626
      (with <code>createMBean</code>) or registered
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
      (with <code>registerMBean</code>) in an MBeanServer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
      the MBeanServer will call the <code>preRegister</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
      and <code>postRegister</code> method implemented by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
      the MBean. The <code>preRegister</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
      has an <code>MBeanServer</code> and <code>ObjectName</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
      parameter, which are passed by the MBeanServer to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
      MBean. The MBean can store the reference it is being passed
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   634
      in a private instance variable for later use.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
      </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
      <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
      Most of the MXBeans we have defined in this example
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
      implement the <code>MBeanRegistration</code> interface. The table
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
      below show how our MBeans use this interface to control
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   640
      their own names, make sanity checks, perform
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
      initialization steps or cleanup actions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
      </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
      <p><br><center>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   644
      <table border="1" cellpadding="4" cellspacing="2"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
             bgcolor="#eeeeee" width="95%">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
          <thead>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
              <tr bgcolor="#cecece">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                  <th width="20%">MBean Requirement</th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                  <th>callback</th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                  <th>use case example</th>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
              </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
          </thead>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
          <tbody>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
              <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                  <td bgcolor="#dedede">get a reference to the MBeanServer</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                  <td><code>preRegister</code></td>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   657
                  <td bgcolor="#fafafa">The <a
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   658
href="dist/javadoc/com/sun/jmx/examples/scandir/ScanManagerMXBean.html"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
title="The ScanManagerMXBean is the main MBean of the scandir application"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
             >ScanManagerMXBean</a>  needs a reference
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                  to the MBeanServer in order to create and
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   662
                  register other MBeans, such as the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                  <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
href="dist/javadoc/com/sun/jmx/examples/scandir/ResultLogManagerMXBean.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
title="The ResultLogManagerMXBean is in charge of managing result logs"
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   666
        >ResultLogManagerMXBean</a>, and the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                  <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
href="dist/javadoc/com/sun/jmx/examples/scandir/DirectoryScannerMXBean.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
title="A DirectoryScannerMXBean looks for file matching a given set of criteria, starting at a given root."
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        >DirectoryScannerMXBeans</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                  </td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
              </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
              <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                  <td bgcolor="#dedede">reject registration if conditions are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                      not met.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                  </td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                  <td><code>preRegister</code></td>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   678
                  <td bgcolor="#fafafa">The <a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
href="dist/javadoc/com/sun/jmx/examples/scandir/ScanManagerMXBean.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
title="The ScanManagerMXBean is the main MBean of the scandir application"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
             >ScanManagerMXBean</a>  will throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                  an IllegalArgumentException in <code>preRegister</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                  if the ObjectName it is being passed is
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   684
                  illegal. Throwing an exception in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                  <code>preRegister</code> makes the registration fail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                  </td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
              </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
              <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                  <td bgcolor="#dedede">get my client-assigned MBean name</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                  <td><code>preRegister</code></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                  <td bgcolor="#fafafa">The <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
href="dist/javadoc/com/sun/jmx/examples/scandir/ScanDirConfigMXBean.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
title="The ScanDirConfigMXBean is in charge of the configuration"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
           >ScanDirConfigMXBean</a> propagates the
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   695
                  value of the <code>name=</code> property of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                  the ObjectName it is given into its
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   697
                  ScanManagerConfig bean.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                  </td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
              </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
              <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                  <td bgcolor="#dedede">provide my own default ObjectName if none
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                      was given to the MBeanServer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                  </td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                  <td><code>preRegister</code></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                  <td bgcolor="#fafafa">The name that is returned by <code>preRegister</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                  is the ObjectName with which the MBean will be
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   707
                  eventually registered.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                  The <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
href="dist/javadoc/com/sun/jmx/examples/scandir/ScanDirConfigMXBean.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
title="The ScanDirConfigMXBean is in charge of the configuration"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
           >ScanDirConfigMXBean</a> is able to suggest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                  a value for its own ObjectName if none was
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   713
                  provided. Similarly, the <a
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   714
href="dist/javadoc/com/sun/jmx/examples/scandir/ScanManagerMXBean.html"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
title="The ScanManagerMXBean is the main MBean of the scandir application"
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   716
             >ScanManagerMXBean</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                  always returns its singleton ObjectName
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   718
                  defined by <a
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   719
href="dist/javadoc/com/sun/jmx/examples/scandir/ScanManagerMXBean.html#SCAN_MANAGER_NAME"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
title="The ScanManagerMXBean is the main MBean of the scandir application"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
             >ScanManagerMXBean.SCAN_MANAGER_NAME</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                  </td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
              </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
              <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                  <td bgcolor="#dedede">perform initialization steps</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                  <td><code>preRegister</code></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                  <td bgcolor="#fafafa">The <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
href="dist/javadoc/com/sun/jmx/examples/scandir/ScanDirConfigMXBean.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
title="The ScanDirConfigMXBean is in charge of the configuration"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
           >ScanDirConfigMXBean</a> uses <code>preRegister</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                  to initialize its internal ScanManagerConfig bean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                  </td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
              </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
              <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                  <td bgcolor="#dedede">perform initialization steps, once it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                  known that the registration was successful.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                  </td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                  <td><code>postRegister</code></td>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   739
                  <td bgcolor="#fafafa">The <code>postRegister</code> method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                  can be used to implement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                  initialization steps that need to be done once it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                  is known that the registration was successful, or to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                  undo any action performed by <code>preRegister</code> once it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                  is known that registration was not successful.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                  The <code>postRegister</code> method has a Boolean parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                  which tells the MBean whether it was or wasn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                  successfully registered in the MBeanServer.
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   748
                  The <a
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   749
href="dist/javadoc/com/sun/jmx/examples/scandir/ScanManagerMXBean.html"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
title="The ScanManagerMXBean is the main MBean of the scandir application"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
             >ScanManagerMXBean</a>  uses <code>postRegister</code> to create
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   752
                  and register other MBeans, such as the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                  <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
href="dist/javadoc/com/sun/jmx/examples/scandir/ResultLogManagerMXBean.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
title="The ResultLogManagerMXBean is in charge of managing result logs"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        >ResultLogManagerMXBean</a> and the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                  <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
href="dist/javadoc/com/sun/jmx/examples/scandir/ScanDirConfigMXBean.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
title="The ScanDirConfigMXBean is in charge of the configuration"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
           >ScanDirConfigMXBean</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                  Note that <code>postRegister</code> is not expected to throw any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                  exception. If an exception needs to be thrown, it should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                  be thrown in <code>preRegister</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                  </td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
              </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
              <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                  <td bgcolor="#dedede">check whether the MBean can be deregistered</td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                  <td><code>preDeregister</code></td>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   769
                  <td bgcolor="#fafafa">The <a
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   770
href="dist/javadoc/com/sun/jmx/examples/scandir/ScanManagerMXBean.html"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
title="The ScanManagerMXBean is the main MBean of the scandir application"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
             >ScanManagerMXBean</a>  uses this method to verify
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   773
                   that its state allows it to be deregistered.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                   In particular, it will refuse to be deregistered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                   if it is in the RUNNING or SCHEDULED state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                   If <code>preDeregister</code> throws an exception, the unregisterMBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                   call will fail and the MBean will remain registered in
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   778
                   the MBeanServer.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                   Take particular care when implementing business logic
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   780
                   in this method: if the logic you implement has an
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   781
                   unfortunate bug which makes it always throw an
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                   exception, you will never be able to unregister
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                   that MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                  </td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
              </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
              <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                  <td bgcolor="#dedede">clean up resources, refusing to be deregistered if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                      it fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                  </td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                  <td><code>preDeregister</code></td>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   791
                  <td bgcolor="#fafafa">The <a
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   792
href="dist/javadoc/com/sun/jmx/examples/scandir/ScanManagerMXBean.html"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
title="The ScanManagerMXBean is the main MBean of the scandir application"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
             >ScanManagerMXBean</a>  uses this method to unregister
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                  all the other MBeans it has created and registered in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                  MBeanServer. This includes the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
href="dist/javadoc/com/sun/jmx/examples/scandir/ResultLogManagerMXBean.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
title="The ResultLogManagerMXBean is in charge of managing result logs"
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   799
        >ResultLogManagerMXBean</a>, the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                  <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
href="dist/javadoc/com/sun/jmx/examples/scandir/ScanDirConfigMXBean.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
title="The ScanDirConfigMXBean is in charge of the configuration"
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   803
           >ScanDirConfigMXBeans</a> it has created, and the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                  <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
href="dist/javadoc/com/sun/jmx/examples/scandir/DirectoryScannerMXBean.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
title="A DirectoryScannerMXBean looks for file matching a given set of criteria, starting at a given root."
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        >DirectoryScannerMXBeans</a> it has created when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                  applying its configuration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                  </td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
              </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
              <tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                  <td bgcolor="#dedede">clean up resources which need to be released in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                  a best-effort way, when it is known that the MBean is no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                  longer registered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                  </td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                  <td><code>postDeregister</code></td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                  <td bgcolor="#fafafa"><code>postDeregister</code> is only called if the MBean was succesfully
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                  unregistered.
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   819
                  The <a
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   820
href="dist/javadoc/com/sun/jmx/examples/scandir/ScanManagerMXBean.html"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
title="The ScanManagerMXBean is the main MBean of the scandir application"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
             >ScanManagerMXBean</a>  uses this method to cancel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
                  its internal java.util.Timer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
                  </td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
              </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
          </tbody>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
      </table>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
      </center><br></p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
  </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
  <h3>The Singleton MBean Pattern</h3>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
  <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
      <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        A singleton MBean is an MBean which can only have one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        instance registered in a given MBeanServer. <br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        A singleton MBean usually has a well-known name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        which can be defined as a constant. In that case,
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   837
        clients no longer need to call <code>new ObjectName(...)</code>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        and catch the declared <code>MalformedObjectNameException</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
      </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
      <p>There are already quite a few examples of singleton
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   841
         MBeans in the java.lang.management API. The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
         ThreadingMXBean, ClassLoadingMXBean, RuntimeMXBean, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
         are all singleton MBeans.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
      </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
      <p>In this example, we have two singleton MBeans:
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   846
         The <code><a
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   847
href="dist/javadoc/com/sun/jmx/examples/scandir/ScanManagerMXBean.html"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
title="The ScanManagerMXBean is the main MBean of the scandir application"
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   849
             >ScanManagerMXBean</a></code> and the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
         <code><a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
href="dist/javadoc/com/sun/jmx/examples/scandir/ResultLogManagerMXBean.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
title="The ResultLogManagerMXBean is in charge of managing result logs"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        >ResultLogManagerMXBean</a></code>. But in fact,
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   854
         the only real singleton MBean is the
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   855
        <code>ScanManagerMXBean</code>. The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
        <code>ResultLogManagerMXBean</code> just happens to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        be a singleton MBean because it has a 1-1 relationship
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        with the <code>ScanManagerMXBean</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
      </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
      <p>The <code>ScanManagerMXBean</code> implements the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
         singleton MBean pattern in this way:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
      </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
      <ul>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   864
          <li>The <code>ScanManagerMXBean</code> name has a single
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
              key property: <code>type=ScanManagerMXBean</code>.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
          <li>Its name is defined by an ObjectName constant called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
              <code>SCAN_MANAGER_NAME</code> in the <code>ScanManager</code> class</li>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   868
          <li>The <code>ScanManagerMXBean</code> enforces its status of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
              singleton MBean. It will refuse to be registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
              with a name other than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
              the <code>SCAN_MANAGER_NAME</code>. You can therefore depend on
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   872
              the fact that the <code>ScanManagerMXBean</code> will always
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
              be registered with its singleton <code>SCAN_MANAGER_NAME</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
              (see <code>preRegister</code>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
          </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
          <li>You are not obliged to provide a name when you
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
              register the <code>ScanManagerMXBean</code>: if you pass null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
              then the <code>ScanManager</code> will be registered with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
              its singleton <code>SCAN_MANAGER_NAME</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
              (see <code>preRegister</code>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
          </li>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   882
          <li>The <code>ScanManager</code> class has a no-arg static
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
              <code>register</code> method that will register
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
              the singleton instance in the Platform MBeanServer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
              This static <code>register</code> method returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
              a proxy to the registered singleton.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
          </li>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   888
          <li>The <code>ScanManager</code> class has also a static
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
              <code>register</code> method that will create
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
              a singleton instance in a (possibly remote)
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   891
              MBeanServerConnection - using
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
              <code>createMBean</code>.
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   893
              This static <code>register</code> method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
              also returns a proxy to the registered singleton.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
          </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
          <li>Only the MBeanServer has a reference to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
              singleton instance. The singleton instance is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
              not returned to the caller, and not kept
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
              in any other static data structure.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
          </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
      </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
      <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
      On the other hand, the <code><a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
href="dist/javadoc/com/sun/jmx/examples/scandir/ResultLogManagerMXBean.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
title="The ResultLogManagerMXBean is in charge of managing result logs"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
        >ResultLogManagerMXBean</a></code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
      has a much more relaxed implementation of the pattern:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
      <br>It simply provides its own singleton name if it is
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   909
      registered with a null ObjectName, but will not enforce
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
      the use of that name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
      </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
      <p>Note that all singleton MBean names in this example
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
      are created using the <code>ScanManager.makeSingletonName</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
      method, which implements the pattern for ObjectNames suggested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
      in the JMX Best Practices.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
      </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
  </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
  <h3>Managing the Life Cycle of dependent MBeans</h3>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
  <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
      <p>A common task that many JMX applications have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        is to manage the life cycle of MBeans registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        in the MBeanServer.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
      <p>In this example, we have decided to follow a simple
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
      pattern:</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
      <ul>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   926
          <li>The application is initialized simply
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   927
              by registering the singleton
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   928
              <a
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   929
href="dist/javadoc/com/sun/jmx/examples/scandir/ScanManagerMXBean.html"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
title="The ScanManagerMXBean is the main MBean of the scandir application"
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   931
             >ScanManagerMXBean</a> in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
              the MBeanServer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
          </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
          <li>The <code>ScanManagerMXBean</code> will then
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   935
              in turn register any other MBean that the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
              application might need:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
              <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
                  <li>It creates and registers the singleton
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
                      <code><a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
href="dist/javadoc/com/sun/jmx/examples/scandir/ResultLogManagerMXBean.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
title="The ResultLogManagerMXBean is in charge of managing result logs"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        >ResultLogManagerMXBean</a></code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
                  </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                  <li>It creates and registers the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
                      <code><a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
href="dist/javadoc/com/sun/jmx/examples/scandir/ScanDirConfigMXBean.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
title="The ScanDirConfigMXBean is in charge of the configuration"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
           >ScanDirConfigMXBean</a></code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
                      which loads the initial configuration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
                  </li>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   951
                  <li>It creates as many
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                     <code><a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
href="dist/javadoc/com/sun/jmx/examples/scandir/DirectoryScannerMXBean.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
title="A DirectoryScannerMXBean looks for file matching a given set of criteria, starting at a given root."
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        >DirectoryScannerMXBeans</a></code> as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
                     needed when the configuration is applied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
                  </li>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   958
                  <li>It lets you create alternate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
                      <code>ScanDirConfigMXBean</code>, to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
                      which you can later switch in order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
                      to apply a new alternate configuration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
                  </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
              </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
          </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
          <li>When a new configuration is applied (or if the
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   966
              current configuration is reapplied), the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
              <code>ScanManagerMXBean</code> will unregister
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
              any <code>DirectoryScannerMXBeans</code> it has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
              previously registered, and will re-create
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
              brand new <code>DirectoryScannerMXBeans</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
              from the applied configuration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
          </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
          <li>When you unregister the <code>ScanManagerMXBean</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
              it does all the cleanup for you, by unregistering
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
              all the MBeans that it has created during the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
              course of the application.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
          </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
      </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
      <p>The <code>ScanManagerMXBean</code> makes use of its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
         <code>MBeanRegistration</code> interface in order
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
         to register the other MBeans it needs (see the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
         <code>ScanManager.postRegister</code> method) and to unregister
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
         every MBean it has created (see the <code>ScanManager.preDeregister</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
         method).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
      </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
      <p>You will note that the <code>ScanManagerMXBean</code>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   987
         will only allow itself to be deregistered if it can be
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
         closed - that is if there's no other action in
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   989
         progress.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
         This is to make sure that the deregistration of
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   991
         dependent MBeans will work smoothly.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
         <br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
         The deregistration of related MBeans will happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
         in the <code>ScanManager.preDeregister</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
         method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
         <br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
         If one of these MBeans could not be deregistered,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
         then the <code>ScanManagerMXBean</code> will throw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
         an exception, refusing to be deregistered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
         <br>This leaves you a chance to try to deregister it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
         again later. Since the <code>ScanManagerMXBean</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
         has switched its state to CLOSED before starting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
         to unregister its dependent MBeans, it will refuse
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
         any further actions, ensuring that e.g. nobody
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
         can try to start it or schedule it while it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
         is in that partially-deregistered state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
      </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
      <p>Handling the LifeCycle of all the application's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
         MBeans in a single MBean is usually a good design
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1010
         pattern, especially if the application is a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
         module which is intended to share a JVM - or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
         an MBeanServer - with other modules.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
      </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
      <p>This is specially useful if the application needs to
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1015
         be loaded and unloaded on demand: in that
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1016
         case, simply registering or unregistering the top level
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
         MBean (in our example the <code>ScanManagerMXBean</code>) does
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
         the trick.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
      </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
  </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
  <h3>Emitting Notifications</h3>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
  <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
       <p>In order to emit notifications, an MBean must be
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1024
       an instance of <code>NotificationEmitter</code>.
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1025
       The <code>NotificationEmitter</code> interface defines methods
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
       that the MBeanServer will call on the MBean in order
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
       to register <code>NotificationListeners</code> with the MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
       </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
       <p>It is worth noting that the MBean may not be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
          invoked each time a JMX client wants to register
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
          a listener. For instance, the RMIConnectorServer
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1032
          registers <i>only once</i> a single listener with each MBean
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
          which is a <code>NotificationEmitter</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
          In that specific case, the listener may even be registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
          with the MBean before any client has actually subscribed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
          for notifications from that particular MBean.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
       </p>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1038
       <p>An MBean can therefore make no assumption about
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
          which client or how many clients have registered for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
          notifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
       </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
       <p>It is also worth noting that the logic of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
       methods defined in <code>NotificationEmitter</code> would not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
       be trivial to implement from scratch. Fortunately
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1045
       the JMX API defines a helper class, called
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1046
       <code>NotificationBroadcasterSupport</code>, which
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
       provides an implementation for these methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
       </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
       <p>There are actually three ways for an MBean to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
       implement <code>NotificationEmitter</code>, of which only two
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
       are recommended.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
       </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
  </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
  <h4>Extending NotificationBroadcasterSupport</h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
  <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
    <p>This is the simplest way of coding an MBean which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
       is a <code>NotificationEmitter</code>:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
    </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
    <p>Simply extend <code>NotificationBroadcasterSupport</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
    then override its <code>getNotificationInfo</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
    which returns the <code>MBeanNotificationInfo[]</code> array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
    that should be included in your MBean's <code>MBeanInfo</code>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1064
    and that's it.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
    <br>You just need to call the <code>sendNotification</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
    inherited from <code>NotificationBroadcasterSupport</code> whenever
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
    your MBean needs to send a notification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
    </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
    <p>In our example, both the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
href="dist/javadoc/com/sun/jmx/examples/scandir/ScanDirConfigMXBean.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
title="The ScanDirConfigMXBean is in charge of the configuration"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
           >ScanDirConfigMXBean</a> and <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
href="dist/javadoc/com/sun/jmx/examples/scandir/ResultLogManagerMXBean.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
title="The ResultLogManagerMXBean is in charge of managing result logs"
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1075
        >ResultLogManagerMXBean</a> extend
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
      <code>NotificationBroadcasterSupport</code> in order
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
      to send notifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
    </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
  </ul>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1080
  <h4>The Delegation Pattern: delegating to a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
      NotificationBroadcasterSupport delegate</h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
  <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
      <p>There may be cases however where delegating to a
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1084
      wrapped <code>NotificationBroadcasterSupport</code>
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1085
      object may be preferred to extending
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
      <code>NotificationBroadcasterSupport</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
      </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
      <p>For instance, if your MBeans already derive from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
      some base class, extending <code>NotificationBroadcasterSupport</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
      might not be an option.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
      </p>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1092
      <p>Similarly, if you do not want to have the inherited
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
      <code>public void sendNotification(Notification notification)</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
      method appear in the Javadoc of the concrete class of your
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
      MBean, you may want to consider using the delegation
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1096
      pattern instead of extending
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
      <code>NotificationBroadcasterSupport</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
      </p>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1099
      <p>In our example both the <a
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1100
href="dist/javadoc/com/sun/jmx/examples/scandir/ScanManagerMXBean.html"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
title="The ScanManagerMXBean is the main MBean of the scandir application"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
             >ScanManagerMXBean</a> and the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
href="dist/javadoc/com/sun/jmx/examples/scandir/DirectoryScannerMXBean.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
title="A DirectoryScannerMXBean looks for file matching a given set of criteria, starting at a given root."
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1105
        >DirectoryScannerMXBean</a> use the delegation
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1106
       pattern rather than extending
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
       <code>NotificationBroadcasterSupport</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
       In the end, choosing between one or the other method
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1109
       is more a question of taste, although the delegation
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
       pattern could be considered more flexible since it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
       doesn't require extending any given superclass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
      </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
      <p>It may be also worth noting that some tools like
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1114
      the JMX Module of <a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
href="http://www.netbeans.org"
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1116
      >NetBeans IDE</a>, will be able to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
      generate for you all the code that delegates to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
      wrapped <code>NotificationBroadcasterSupport</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
      </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
  </ul>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1121
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
  <h4>Implementing NotificationEmitter from scratch</h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
  <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
    <p>This is the last possibility for an MBean that
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1125
    needs to send notifications: simply implement
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
    <code>NotificationEmitter</code> from scratch. This is highly
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1127
    discouraged since that logic is not trivial, and
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1128
    already provided by
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
    <code>NotificationBroadcasterSupport</code> anyway.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
    </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
  </ul>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1132
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
  <h4>Beware of Synchronization Locks</h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
  <ul>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1135
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
       <p>One thing you must keep in mind when sending
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
       notifications is not to send them from within
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
       a synchronized block, or while holding a lock on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
       some resource.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
       <p>Indeed, what happens when you send a notification
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
          may vary greatly depending on whether the client
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
          which has registered for notifications has done
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1143
          so through a <code>JMXConnector</code> (like the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
          <code>JMXRMIConnector</code>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
          or through a direct reference to the MBeanServer
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1146
          (by calling
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
          <code>MBeanServer.addNotificationListener</code>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
       </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
       <p>In this latter case, the listener will be invoked
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
       synchronously in the same thread that your MBean is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
       using to send its notification. If by misfortune, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
       code of that listener now re-enters your MBean through a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
       call that flows through a JMXConnector, a deadlock
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
       could occur. It is therefore very important to release
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1155
       any lock you may have before calling
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
       <code>sendNotification</code>.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
       <p>An easy way to do that is demonstrated in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
          <code>ScanManager</code> class. The ScanManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
          has an internal private queue of pending notifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
          When a notification needs to be sent (e.g. because the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
          ScanManager state is being switched), the notification
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
          is simply prepared and put into the pending notification
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1163
          queue.
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1164
          The notification queue is then processed later on,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
          at the end of the method, when the processing is finally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
          completed and all the locks have been released.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
          <br>At this point the notification queue might already
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
          have been emptied by another thread - in which case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
          the pending notifications will have already been
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1170
          removed from the queue. Which thread actually gets
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1171
          to send the notifications is of no importance. The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
          important point is that all the locks detained by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
          your MBean code in that thread were released before
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
          the notification was sent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
       </p>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1176
       <p>In our example the <code>ScanManager</code> class
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
          ensures this by:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
          <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
              <li>Only calling <code>sendNotification</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
                  in its private <code>sendQueuedNotifications</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
                  method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
              </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
              <li>Only calling <code>sendQueuedNotifications</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
                  when all locks have been released.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
              </li>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1186
              <li>Never calling a method that calls
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
                  <code>sendQueuedNotifications</code> from within
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
                  a synchronized block.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
          </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
       </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
  </ul>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1192
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1193
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
  <h4>Don't subclass Notification</h4>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
  <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
       <p>Another common best practice when you want
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
          to improve interoperability is to use directly
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
          the Notification base classes provided in the
30678
a8b7fd8ede97 8075284: fix up miscellaneous TM constructions
darcy
parents: 27565
diff changeset
  1200
          JMX&trade; API. Do not create your own
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
          subclasses of these standard classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
       </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
       <p>Indeed, if you code your own subclass, a generic
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
       client, like jconsole, will not be able to receive
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1205
       that notification unless it has that custom
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
       subclass in its classpath.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
       </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
       <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
       If you want your application to be interoperable, it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
       therefore preferable not to subclass any of the standard
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1211
       Notification classes. You can define your own
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
       Notification type string, and if you need to send
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1213
       additional data, you can put a CompositeData, or a
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1214
       HashMap of serializable standard types in the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
       Notification's user data fields.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
       </p>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1217
       <p>In this example, we are using directly the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
       standard notification classes:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
       <ul>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1220
           <li>The <a
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1221
href="dist/javadoc/com/sun/jmx/examples/scandir/ScanManagerMXBean.html"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
title="The ScanManagerMXBean is the main MBean of the scandir application"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
             >ScanManagerMXBean</a> and the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
href="dist/javadoc/com/sun/jmx/examples/scandir/DirectoryScannerMXBean.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
title="A DirectoryScannerMXBean looks for file matching a given set of criteria, starting at a given root."
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
             >DirectoryScannerMXBean</a> both use directly
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
             <code>AttributeChangeNotification</code> to notify
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
             changes in their <code>State</code> attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
           </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
           <li>The <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
href="dist/javadoc/com/sun/jmx/examples/scandir/DirectoryScannerMXBean.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
title="A DirectoryScannerMXBean looks for file matching a given set of criteria, starting at a given root."
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
             >DirectoryScannerMXBean</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
             also uses the base <code>Notification</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
             class directly in order to notify whenever
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
             it finds a matching file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
             <br>In that case, we simply use the base
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
             <code>Notification</code>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1239
             class with a new
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
             <b><code>com.sun.jmx.examples.scandir.filematch</code></b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
             type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
           </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
           <li>The <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
href="dist/javadoc/com/sun/jmx/examples/scandir/ScanDirConfigMXBean.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
title="The ScanDirConfigMXBean is in charge of the configuration"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
             >ScanDirConfigMXBean</a> and <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
href="dist/javadoc/com/sun/jmx/examples/scandir/ResultLogManagerMXBean.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
title="The ResultLogManagerMXBean is in charge of managing result logs"
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1249
             >ResultLogManagerMXBean</a> also both use the base
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
             <code>Notification</code> class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
           </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
       </ul>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1253
       <p>Careful readers will have noted that the <a
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1254
href="dist/javadoc/com/sun/jmx/examples/scandir/ScanManagerMXBean.html"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
title="The ScanManagerMXBean is the main MBean of the scandir application"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
             >ScanManagerMXBean</a> and the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
href="dist/javadoc/com/sun/jmx/examples/scandir/DirectoryScannerMXBean.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
title="A DirectoryScannerMXBean looks for file matching a given set of criteria, starting at a given root."
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1259
             >DirectoryScannerMXBean</a> both use the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
             <code>AttributeChangeNotification</code> class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
             to notify about their state change, whereas the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
             <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
href="dist/javadoc/com/sun/jmx/examples/scandir/ScanDirConfigMXBean.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
title="The ScanDirConfigMXBean is in charge of the configuration"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
             >ScanDirConfigMXBean</a> uses the base
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
             <code>Notification</code> class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
       </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
       <p>In fact, this is because the semantics of these
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
          notifications is not exactly the same - although
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
          both denote a state change:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
          <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
              <p>In the case of <code>ScanManagerMXBean</code>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1273
              and <code>DirectoryScannerMXBean</code>, the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
              notification which is emitted is more about a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
              state transition, from one state to another.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
              For instance, going from <code>RUNNING</code>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1277
              to <code>STOPPED</code>, or from
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
              <code>SCHEDULED</code> to <code>STOPPED</code>.
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1279
              <br>In that case, the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
              <code>AttributeChangeNotification</code> was
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
              more appropriate because it made it possible
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1282
              to send the previous and the new value of the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
              state attribute, thus reflecting the whole
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
              state transition.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
              </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
              <p>In the case of the <code>ScanDirConfigMXBean</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
              however, what is of interest is the state in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
              which the MBean has arrived. Using the base
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1289
              <code>Notification</code> class with three different
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1290
              notification type strings -
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
              <b><code>com.sun.jmx.examples.scandir.config.loaded</code></b>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
              <b><code>com.sun.jmx.examples.scandir.config.modified</code></b>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
              and
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1294
              <b><code>com.sun.jmx.examples.scandir.config.saved</code></b> -
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
              was therefore closer to what we wanted to model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
              </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
          </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
       </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
  </ul>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1300
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
  <h3>Configuration MBeans</h3>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
    <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
    <p>A common practice when designing a management application is
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1304
    to have an MBean, or a set of MBeans, dedicated to configuration.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
    Separating configuration from control and monitoring allows
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1306
    more appropriate logic, and often simplifies the design and
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
    implementation of the management interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
    </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
    <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
    In our example, the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
href="dist/javadoc/com/sun/jmx/examples/scandir/ScanDirConfigMXBean.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
title="The ScanDirConfigMXBean is in charge of the configuration"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
     >ScanDirConfigMXBean</a> is dedicated to the application configuration.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
    </p>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1315
    <p>The <code>ScanDirConfigMXBean</code> will let you interactively
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
        modify, save, or load the application configuration. The modifications
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
        will not be taken into account until it is applied, by invoking
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1318
        <code>applyConfiguration</code> on the <a
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1319
href="dist/javadoc/com/sun/jmx/examples/scandir/ScanManagerMXBean.html"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
title="The ScanManagerMXBean is the main MBean of the scandir application"
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1321
        >ScanManagerMXBean</a>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
    It is also possible to create many configurations, by creating as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
    many <code>ScanDirConfigMXBean</code>s, and then to choose and apply
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1324
    one of these configurations by calling
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
    <code>ScanManagerMXBean.setConfigurationMBean</code> and then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
    <code>ScanManagerMXBean.applyConfiguration</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
    </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
    <p>In this way, all configurations aspects are gathered and concentrated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
    inside the <code>ScanDirConfigMXBean</code> instead of being scattered
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
    throughout all the MBeans that compose the application.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
    </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
    <p>In order to save and store the application configuration data, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
    <code>ScanDirConfigMXBean</code> uses a set of XML serializable Java beans
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1334
    defined in the <a
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1335
href="dist/javadoc/com/sun/jmx/examples/scandir/config/package-summary.html"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
title="The com.sun.jmx.examples.scandir.config package defines XML serializable beans"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
        >com.sun.jmx.examples.scandir.config</a> package. These beans are very
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
   simple Java beans which have been lightly annotated for XML binding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
    </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
    <p>It is worth noting that these same beans can also be handled by the
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1341
    MXBean framework (our beans don't contain recursive data structures) and can
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
    therefore be used directly as attributes and parameters of MXBeans, without
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
    needing to be Java-serializable (the MXBean framework transform them in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
    CompositeData objects - which <b>are</b> serializable).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
    </p>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1346
    <p>The same <a
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1347
href="dist/javadoc/com/sun/jmx/examples/scandir/config/ScanManagerConfig.html"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
title="The com.sun.jmx.examples.scandir.config package defines XML serializable beans"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
        >ScanManagerConfig</a> bean that we use to read from and write to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
        XML configuration file is thus also used as attribute of the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
href="dist/javadoc/com/sun/jmx/examples/scandir/ScanDirConfigMXBean.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
title="The ScanDirConfigMXBean is in charge of the configuration"
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1353
     >ScanDirConfigMXBean</a>. It is transformed into a <code>CompositeData</code>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
       by the MXBean framework, and can be easily introspected with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
       <a href="#JConsole">jconsole</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
    </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
    </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
  <h3>MBeans Must Be Thread-Safe</h3>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
    <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
    <p>A question often asked by newcomers to JMX technology
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1361
    is whether the MBeanServer is thread-safe. Well, the MBeanServer <b>is</b>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
    thread safe, but it doesn't put any locks on the MBeans it contains. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
    MBeans can be concurrently accessed by multiple threads, and must therefore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
    take care of their own thread safety.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
    </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
    <p>In this example, we have been using two methods to ensure thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
    safety for our MBeans: synchronized blocks, and semaphores.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
    </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
    <p>Using synchronized blocks is probably the most common and easiest way
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
    to implement thread safety in Java. When dealing with MBeans though, here
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
    are a couple of rules to keep in mind:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
    <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
        <li>Don't send notifications from within a synchronized block: there's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
        no way to tell whether the listener's code will be executed in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
        same thread or a different thread, and holding a lock in these
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
        conditions is therefore dangerous, as it could lead to deadlocks.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
        <li>Also avoid invoking another MBean from a synchronized block
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
        unless you are completely in control of both MBeans, and you can
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
        ascertain that it won't lead to any deadlock. Indeed, if you invoke an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
        MBean exposed by another application, it can be sometime hard to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
        know with certainty whether holding a lock while invoking that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
        MBean will have any side effect. Maybe that MBean will make
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
        further calls to other MBeans which will in turn try to call
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
        your MBean, or maybe it will emit a
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1385
        notification, and we'll be back to the considerations just
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
        above.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
    </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
    </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
    <p>Another means of implementing thread-safe code is to use semaphores.
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1390
    The <a
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1391
href="dist/javadoc/com/sun/jmx/examples/scandir/ScanManagerMXBean.html"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
title="The ScanManagerMXBean is the main MBean of the scandir application"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
        >ScanManagerMXBean</a> uses a semaphore called
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
        <code>sequencer</code> to ensure
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
    that critical code sections are not executed concurrently. In this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
    MBean, we use <code>Semaphore.tryAcquire</code> to lock the sequencer
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1397
    semaphore before entering the critical section. If the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
    <code>Semaphore.tryAcquire</code> returns true then we enter the critical
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
    section. If it returns false, we throw an IllegalStateException, stating
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
    that we couldn't acquire the lock. The code looks like this:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
    <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
        if (!sequencer.tryAcquire())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
            throw new IllegalStateException("resource locked");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
            // critical code here ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
            // Always use try/finally to ensure that the semaphore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
            // will be released, even if exceptions or errors are raised!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
            sequencer.release();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
    </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
    </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
    <p>Using <code>Semaphore.tryAcquire</code> and throwing an exception if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
        the semaphore is already locked makes it safer to call other MBeans
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
        from within the critical section: in potential deadlock situations
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
        the calling code will get the <code>IllegalStateException</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
        instead of being blocked on the deadlocked lock.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
    </p>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1419
    <p>It is worth noting that each of these techniques has its own
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
    advantages and disadvantages - which can make one of them more or less
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
    appropriate depending on the inner logic of the MBean you're implementing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
    </p>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1423
    <p>Careful readers will also have noted that we used
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1424
       <code>IllegalStateException</code> directly, instead of defining
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
       our own subclass of RuntimeException, which could have had a more
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
       precise semantics. If you define a new exception for your JMX application,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
       you must keep in mind that your client will need to have the class
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1428
       of your exception in its classpath to get that exception.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
       Otherwise your client will get a completely different exception, indicating a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
       deserialization issue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
    </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
    </ul>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1433
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
  <h3>Waiting for Notifications</h3>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
    <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
    <p>Implementing code that needs to wait for notifications is sometimes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
       difficult. Because notifications are asynchronous, doing something
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
       like:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
        <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
          // register a notification listener
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
          ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
          // start a management action
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
          ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
          // wait for a notification
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
          ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
          // do something based on whether the expected notification
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
          // is received
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
          ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
        </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
       is not always trivial. However, there's a very easy way to do that: use
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
       a blocking queue of notifications.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
       <pre>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1453
       final BlockingQueue&lt;Notification&gt; notifQueue =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
                new LinkedBlockingQueue&lt;Notification&gt;();
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1455
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
       final NotificationListener listener = new NotificationListener() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
            public void handleNotification(Notification notification,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
                                           Object handback) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
                    // Just put the received notification in the queue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
                    // It will be consumed later on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
                    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
                    notifQueue.put(notification);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
                } catch (InterruptedException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
                    // OK
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
          };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
       // register the listener - possibly also as a JMXConnectionNotification
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
       // listener to get Notification Lost notification
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
       ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
       // start management action
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
       ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
       // wait for notification
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
       while (expected notif not received and delay not expired) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
            Notification n = notifQueue.poll(3,TimeUnit.SECONDS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
            // if expected notif, do something
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
            ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
       // if expected notification not received do something else.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
       ....
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
       </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
    </p>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1485
    <p>You will note that this is a technique we've been using in the <a
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1486
href="dist/javadoc/com/sun/jmx/examples/scandir/ScanDirAgent.html"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
title="The ScanDirAgent class defines a main method for the scandir application"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
        >ScanDirAgent</a> class and in the example unit tests.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
    </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
    </ul>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1491
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
  <h3>Holding hard references to other MBeans: proxy or direct reference?</h3>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
  <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
    <p>We have seen that MXBeans will let you return proxy references to other
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
    MXBeans. But should that MXBean hold a direct reference to the MXBeans it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
    relates to, or would it be better for it to hold only a proxy?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
    </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
    <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
    As a general rule it is better when an MBean reference is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
    only held by the MBeanServer. It is a better design
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
    to hold a reference to a proxy, rather than to hold
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
    a hard reference to an MBean. However there are two cases
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1503
    when holding a hard reference might be preferred:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
     <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
        <li>When MBean A needs to call a method of method B which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
           is not part of its MBean interface</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
        <li>When the overhead of going through the MBeanServer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
           plus the MXBean framework is too great (frequently-called
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
           method, with creation of OpenType)</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
     </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
    However - holding a hard reference is only advisable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
    when both MBeans are created by the same piece of code,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
    and the application can ensure that the life cycle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
    of each MBean is consistent with regard to the other.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
    </p>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1516
    <p>In our example, the <a
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1517
href="dist/javadoc/com/sun/jmx/examples/scandir/ScanManagerMXBean.html"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
title="The ScanManagerMXBean is the main MBean of the scandir application"
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1519
        >ScanManagerMXBean</a> holds only proxy references to the <a
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1520
href="dist/javadoc/com/sun/jmx/examples/scandir/ScanDirConfigMXBean.html"
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1521
        >ScanDirConfigMXBean</a> and the  <a
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1522
href="dist/javadoc/com/sun/jmx/examples/scandir/DirectoryScannerMXBean.html"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
        >DirectoryScannerMXBeans</a>. <br>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1524
    However it holds a direct reference to the <a
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1525
href="dist/javadoc/com/sun/jmx/examples/scandir/ResultLogManager.html"
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1526
        >ResultLogManager</a>. This makes it possible to pass a direct
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1527
        reference to the <code>DirectoryScannerMXBeans</code>,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
        which can then log their results
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
        more efficiently, and would also make it possible to remove
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1530
        the <code>log</code> method from the <a
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1531
href="dist/javadoc/com/sun/jmx/examples/scandir/ResultLogManagerMXBean.html"
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1532
        >ResultLogManagerMXBean</a> interface - leaving it in the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
        <code>ResultLogManager</code> class (possibly as a package method)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
        should we wish to do so.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
    </p>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1536
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
    </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
  <h3>Agent Class</h3>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
    <ul>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1541
   <p>The <a
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1542
href="dist/javadoc/com/sun/jmx/examples/scandir/ScanDirAgent.html"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
title="The ScanDirAgent class defines a main method for the scandir application"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
     >ScanDirAgent</a> is the Agent class for the <i>scandir</i> application.
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1545
    This class contains the <code>main</code> method to start a standalone
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
    <i>scandir</i> application.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
    </p>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1548
    <p>The <code>main</code> method simply registers a <a
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1549
href="dist/javadoc/com/sun/jmx/examples/scandir/ScanManagerMXBean.html"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
title="The ScanManagerMXBean is the main MBean of the scandir application"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
       >ScanManagerMXBean</a> in the platform MBeanServer, and then waits
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
    for someone to call <code>ScanManagerMXBean.close</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
    </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
    <p>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1555
    When the <code>ScanManagerMXBean</code> state is switched to
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1556
    <code>ScanManagerMXBean.ScanState.CLOSED</code>, the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
    <code>ScanManagerMXBean</code> is unregistered, and the application
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
    terminates (i.e. the main thread completes).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
    </p>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1560
    <p>Standalone JMX applications usually have an Agent class that contain
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
       their <code>main</code> method, which performs all the MBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
       registration steps.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
       However, it is usually not a bad idea if that class can
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
       be easily turned into an MBean. Indeed, this will make your
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
       application easier to integrate in an environment where it would
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
       no longer be standalone and would no longer control the implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
       of <code>main</code>. In our example the Agent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
       class could be easily turned into an MBean, exposing its three
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1569
    <code>init</code>, <code>waitForClose</code> and <code>cleanup</code>
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1570
    method. However we didn't go as far as turning it into an MBean since
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
    the application can be already easily started by registering an instance
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1572
    of <a
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1573
href="dist/javadoc/com/sun/jmx/examples/scandir/ScanManagerMXBean.html"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
title="The ScanManagerMXBean is the main MBean of the scandir application"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
       >ScanManagerMXBean</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
    </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
    </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
  <h3>Secure Client Class</h3>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
    <ul>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1580
   <p>The <a
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1581
href="dist/javadoc/com/sun/jmx/examples/scandir/ScanDirClient.html"
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 5506
diff changeset
  1582
title="The ScanDirClient class is a very short example of secure programmatic client"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
     >ScanDirClient</a> is an example class that shows how a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
    programmatic client can connect to a secured <i>scandir</i> application.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
    This class contains a <code>main</code> method which creates and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
    configures a <code>JMXConnector</code> client to connect with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
    a secured <i>scandir</i> daemon. This class will not work with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
    the default unsecured agent since it requires mutual authentication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
    </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
    <p>How to secure a JMX <i>scandir</i> application and run
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
    the secure <code>ScanDirClient</code> is discussed <a href="#secure"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
    >later</a> in this document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
    </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
    <p>The <code>ScanDirClient</code> is not really part of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
       application - and is given here only for the sake of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
       the example.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
    </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
    </ul>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1599
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
  <h2><a name="h2-Testing">Testing the <i>scandir</i> Example</a></h2>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
    <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
        <p>Make sure that you have access to junit.jar (either 3.8.1 or 3.8.2).
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1603
           Make sure also that you have junit.jar in your
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
           <code>CLASSPATH</code>.<br>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1605
           Then in the example root directory (where the <code>build.xml</code>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
           file is located) run the following command:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
           <pre>ant test -Dlibs.junit.classpath=<i><u>path to junit jar (either 3.8.1 or 3.8.2)</u></i></pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
        </p>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1609
        <p>Alternatively you can open the jmx-scandir project with the
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1610
           NetBeans IDE and test the jmx-scandir project from the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
           <code>Run</code> menu.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
        </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
    </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
  <h2><a name="h2-Running">Running the <i>scandir</i> Example</a></h2>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
    <ul>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1618
        <p>In the example root directory (where the <code>build.xml</code>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
           file is located) run the following commands:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
        <pre>ant jar
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
ant run-single -Drun.class=com.sun.jmx.examples.scandir.ScanDirAgent -Djavac.includes=src</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
           or simply <pre>ant run</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
        </p>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1624
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
        <p>This will run the example using the configuration
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
           file provided in the src/etc directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
        </p>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1628
        <p>Alternatively you can open the jmx-scandir project with the
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1629
           NetBeans IDE. You can run the example by
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
           selecting the <code>ScanDirAgent</code> file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
           and run it with <code>Run File</code> in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
           <code>Run</code> menu or simply
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
           set the <i>jmx-scandir</i> project as main project and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
           select <code>Run Main Project</code> from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
           main menu. Both targets will use the configuration
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
           file provided in the src/etc directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
        </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
        <p>When the application is started, you can connect to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
           it with <a href="#JConsole">jconsole</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
        </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
        <blockquote>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1642
            <u>Note:</u> You can also run the <i>scandir</i>
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1643
            application directly from the <code>java</code>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
            command line. Make sure to build the project jar
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1645
            first.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
            <br>On Unix systems:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
            <pre>ant jar
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
java -Djava.util.logging.config.file=logging.properties \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
     -Dscandir.config.file=src/etc/testconfig.xml \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
     -jar dist/jmx-scandir.jar</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
            <br>On Windows systems:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
            <p><code>ant jar<br>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
java &nbsp;-Djava.util.logging.config.file=logging.properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
     &nbsp;-Dscandir.config.file=src\etc\testconfig.xml
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
     &nbsp;-jar&nbsp;dist\jmx-scandir.jar</code></p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
        </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
    </ul>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1658
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
    <h2><a name="h2-Playing">Playing with JConsole</a></h2>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
    <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
    <p>Run the example as explained in the previous section, so
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1662
    that it uses the provided <code>src/etc/testconfig.xml</code>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
    configuration file. Then start
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
    jconsole. In the connection window choose the process that runs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
    <code>com.sun.jmx.examples.scandir.ScanDirAgent</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
    <code>jmx-scandir.jar</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
    </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
    <p><center>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
        <table border="0" cellpadding="2" cellspacing="2">
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
        <tr><td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
    <a href="docfiles/connect-local-ant-run.jpg"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
    title="jconsole connection window - connect to local process"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
    ><img height="440"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
    src="docfiles/connect-local-ant-run.jpg"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
    alt="jconsole connection window - connect to local process"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
    /></a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
    </td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
    <td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
    <a href="docfiles/connect-local-java-jar.jpg"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
    title="jconsole connection window - connect to local process"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
    ><img height="440"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
    src="docfiles/connect-local-java-jar.jpg"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
    alt="jconsole connection window - connect to local process"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
    /></a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
    </td></tr></table>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
    </center>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
    </p>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1688
    <p>Open the MBeans tab, and look for the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
       <code>ScanDirConfigMXBean</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
       Click on its <code>Attributes</code> node and double click on its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
       <code>Configuration</code> attribute, to look at
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
       the loaded configuration - values in bold can
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
       be expanded by a double-click.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
    </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
    <p><center><a href="docfiles/scandir-config.jpg"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
    title="jconsole MBean tab: ScanDirConfigMXBean"
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1697
    ><img
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
    src="docfiles/scandir-config.jpg"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
    alt="jconsole MBean tab: ScanDirConfigMXBean"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
    /></a></center>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
    </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
    <p>Now go to the <code>ScanManagerMXBean</code>, click on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
       its <code>Notifications</code> node, and subscribe
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1704
       for notifications. Then click on the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
       <code>Operations</code> node and invoke the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
       <code>start()</code> operation:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
    </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
    <p><center><a href="docfiles/scandir-start.jpg"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
    title="jconsole MBean tab: ScanDirConfigMXBean"
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1710
    ><img
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
    src="docfiles/scandir-start.jpg"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
    alt="jconsole MBean tab: ScanDirConfigMXBean"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
    /></a></center>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
    </p>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1715
    <p>You can see that the notifications counter was
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1716
       incremented by three: you have just scheduled,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
       run, and completed a batch of directory scans.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
    </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
    <p>Now go to the <code>ResultLogManagerMXBean</code>,
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1720
       click on its <code>Attributes</code> node, and
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
       expand its <code>MemoryLog</code> attribute:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
    </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
    <p><center><a href="docfiles/scandir-result.jpg"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
    title="jconsole MBean tab: ScanDirConfigMXBean"
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1725
    ><img
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
    src="docfiles/scandir-result.jpg"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
    alt="jconsole MBean tab: ScanDirConfigMXBean"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
    /></a></center>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
    </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
    <p>You can see that the directory scan results have
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
       been logged.</p>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1732
    <p>To make the application terminate go back to the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
       <code>ScanManagerMXBean</code> and invoke
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
       <code>close()</code>. The <code>ScanDirAgent</code>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1735
       will receive the notification, step out of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
       the application main thread, and the application
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
       will terminate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
    </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
    <p>This is of course a very limited scenario. Feel free
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
    to improvise with all the features of the example, creating
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1741
    a new configuration -
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
    <code>ScanManagerMXBean.createOtherConfigurationMBean</code> -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
    adding multiple directory scanners to that configuration -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
    <code>ScanDirConfigMXBean.addDirectoryScanner</code> -
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
    then switching the <code>ScanManagerMXBean</code> current
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
    configuration by changing the value of the <i>ConfigurationMBean</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
    attribute - <code>ScanManagerMXBean.setConfigurationMBean</code>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1748
    - then applying the new configuration -
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1749
    <code>ScanManagerMXBean.applyConfiguration(true)</code> -
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1750
    then scheduling repeated directory scans every 10 seconds -
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1751
    <code>ScanManagerMXBean.schedule(0,10000)</code> -
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
    subscribing for notifications, etc...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
    </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
    </ul>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1755
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
  <a name="secure"></a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
  <h2><a name="h2-Turning">Turning the example into a Secure JMX Application</a></h2>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
    <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
    <p>In this section, we will see how to configure and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
    start the <i>scandir</i> example so that the JVM agent
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1761
    is bootstrapped with a secure JMXConnectorServer. Indeed, until
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1762
    now we have only used the insecure local connection,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
    which can only be used as long as both the client and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
    the server run on the same machine. This section will
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1765
    explain how to start the <code>ScanDirAgent</code> so
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
    that a real secure RMIConnectorServer is started at bootstrap.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
    </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
    <p>To achieve this we will: <a href="#management.properties"
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1769
        >provide our own management.properties</a>, <a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
        href="#password-access">create our own password and access files</a>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
        <a href="#keystore-truststore">provide a keystore and truststore</a>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
        <a href="#start-secure-agent">start the ScanDirAgent with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
        appropriate system properties</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
    </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
    <h3>Configuring the JVM Agent for Secure Remote Connection</h3>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
    <ul>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1777
        <p>The easiest way to <a name="management.properties">configure the
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1778
            JVM Agent</a> for Secure Remote
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
           Connection is to use your own <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
           href="http://java.sun.com/javase/6/docs/technotes/guides/management/agent.html#properties"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
           title="This page describes the properties you can put in your management.properties file"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
           >management.properties</a> file.
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1783
           In this example, we have copied the default
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1784
           <code>$JRE/conf/management/management.properties</code>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
           file to the example's <code>src/etc</code> directory and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
           modified it in <a href="src/etc/management.properties"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
           title="our modified management.properties"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
           >this way</a>:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
           <ul>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1790
               <li>We have set the RMI port to <u>4545</u> - this is just a
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1791
                   random port number we have picked up. Feel free to use your
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
                   own value suited to your environment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
               <pre># For setting the JMX RMI agent port use the following line
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
com.sun.management.jmxremote.port=<b>4545</b></pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
               </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
               <li>We have <u>switched on</u> SSL <u>mutual authentication</u>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
               <pre># For RMI monitoring with SSL client authentication use the following line
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
com.sun.management.jmxremote.ssl.<b>need.client.auth</b>=<b>true</b></pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
               </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
               <li>We have also <u>secured the RMI Registry</u> with SSL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
               <pre># For using an SSL/TLS <b>protected</b> RMI Registry use the following line
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
com.sun.management.jmxremote.<b>registry.ssl</b>=<b>true</b></pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
               </li>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1804
               <li>We have provided <a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
                href="src/etc/password.properties">our own password file</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
               <pre># For a non-default password file location use the following line
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
com.sun.management.jmxremote.password.file=<i>src/etc/password.properties</i></pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
               </li>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1809
               <li>We have provided <a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
                href="src/etc/access.properties">our own access file</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
               <pre># For a non-default password file location use the following line
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
com.sun.management.jmxremote.access.file=<i>src/etc/access.properties</i></pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
               </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
           </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
           <p>You will note that we haven't provided any value
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1816
              for the other security properties, like
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
              <code>com.sun.management.jmxremote.authenticate=true</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
              because these properties already default to a value
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1819
              which enables security by default.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
              Note however that protecting the RMI Registry with SSL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
              improves the application security, but only as long as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
              mutual authentication is also switched on. Otherwise, just
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1823
              anybody would be able to connect to the registry and
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
              get the RMIServer stub.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
           </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
           <p>We do recommend that you <u>use the most secure configuration
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
              when you deploy a JMX agent</u> - which means <u>switching on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
              SSL protection for the RMI registry</u> <b>and</b> <u>requiring
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
              mutual authentication</u>, as we show in this example.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
           </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
           <p>We will use the <code>com.sun.management.config.file</code>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1832
           system property to pass our <a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
           href="src/etc/management.properties">management.properties</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
           file to the <code>ScanDirAgent</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
           </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
    </ul>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1837
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
    <h3>Creating a password and access file</h3>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
    <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
        <p>As explained above, we have created our own
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1841
        <a href="src/etc/password.properties">password file</a>
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1842
        and <a href="src/etc/access.properties">access file</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
        for <a name="password-access">access control and authorization</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
        </p>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1845
        <p>In the password file, we have defined two logins:
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1846
           <i>guest</i> and <i>admin</i>. The password for
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
           <i>guest</i> is <i>guestpasswd</i> and the password
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
           for <i>admin</i> is <i>adminpasswd</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
        </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
        <p>In the access file, we have mapped these two logins
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
        to access rights: the <i>admin</i> login has <i>read-write</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
        access, while the <i>guest</i> login only has <i>read-only</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
        </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
        <p>Before starting the <code>ScanDirAgent</code>, you will
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1855
           need to restrict access permission to the password file,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
           in such a way that nobody but you can read it. Otherwise, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
           JVM Agent will refuse to start the JMXConnectorServer, as it will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
           fear that security can be compromised if other parties can
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
           have read access to the password file. How to restrict
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
           read access to the password file is explained in detail
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
           <a href="http://java.sun.com/javase/6/docs/technotes/guides/management/agent.html#PasswordAccessFiles"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
           title="Using Password and Access Files"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
           >here</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
        </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
        <p>As we have seen above, the location
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1866
           of our access and password files is configured in our own <a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
           href="src/etc/management.properties">management.properties</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
           file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
        </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
    </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
    <h3>Keystore and Truststore</h3>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
    <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
        <p>Using SSL with mutual authentication means that both
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
           client and server will need a <a name="keystore-truststore"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
           >keystore and a truststore</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
           to store their own certificates, and the certificates of
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1877
           the parties they trust. Usually, client and server will
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1878
           have their own keystore and truststore.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
        </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
        <p>For the sake of simplicity - and to get you started
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
        without the tedious necessity of creating your own keystore
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1882
        and truststore, we are providing a dummy keystore and
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
        truststore, containing a certificate self-signed by duke.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
        The password for our keystore is <i>password</i>, and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
        password for our truststore is <i>trustword</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
        We suggest that you first get the example running with the
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1887
        keystore and truststore we are providing before attempting
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
        to use your own keystore and truststore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
        </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
        <p>A secure application will obviously need to use its own
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
        keystore and truststore, <b><u>and should not rely on the keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
        and truststore we are providing here!</u></b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
        </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
        <p>How to create your own keystore and truststore, is explained
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
        in <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
href="http://java.sun.com/javase/6/docs/technotes/guides/management/agent.html#SSL_enabled"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
title="Monitoring and Management Using JMX"
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1898
        >here</a>.
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1899
        As shown <a href="#start-secure-agent">later</a>,
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1900
        we will need to use <a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
        href="http://java.sun.com/javase/6/docs/technotes/guides/management/agent.html#SSL_enabled"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
        >system properties</a> to pass our truststore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
        and keystore to the <code>ScanDirAgent</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
        </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
    </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
    <h3>Starting a Secure <i>scandir</i> agent</h3>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
    <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
        <p>To start a <a name="start-secure-agent"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
        >secure <i>scandir</i> agent</a>, go to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
        <i>scandir</i> example root directory and type the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
        following command:</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
        <p>On Unix Systems:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
<pre>ant jar
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
java \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
    -Djava.util.logging.config.file=logging.properties \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
    -Djavax.net.ssl.keyStore=keystore \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
    -Djavax.net.ssl.keyStorePassword=password \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
    -Djavax.net.ssl.trustStore=truststore \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
    -Djavax.net.ssl.trustStorePassword=trustword \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
    -Dcom.sun.management.config.file=src/etc/management.properties \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
    -Dscandir.config.file=src/etc/testconfig.xml \
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
    -jar dist/jmx-scandir.jar</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
        </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
         <p>On Windows Systems:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
<p><code>ant jar<br>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1926
java
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
    &nbsp;-Djava.util.logging.config.file=logging.properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
    &nbsp;-Djavax.net.ssl.keyStore=keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
    &nbsp;-Djavax.net.ssl.keyStorePassword=password
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
    &nbsp;-Djavax.net.ssl.trustStore=truststore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
    &nbsp;-Djavax.net.ssl.trustStorePassword=trustword
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
    &nbsp;-Dcom.sun.management.config.file=src\etc\management.properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
    &nbsp;-Dscandir.config.file=src\etc\testconfig.xml
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
    &nbsp;-jar&nbsp;dist\jmx-scandir.jar</code></p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
         </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
    <p>If you start jconsole now, you will see that you
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1937
       are still able to connect to the agent using the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
       local connection. However, if you try to connect
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1939
       through the remote connector, using
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1940
       <a href="docfiles/remote-connection.jpg">localhost:4545</a>,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
       the connection will <a href="docfiles/remote-connection-failed.jpg"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
       >fail</a>, even if you provide a correct login/password
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
       pair. Indeed, since the JMXConnectorServer is now protected with SSL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
       jconsole must also be configured with the appropriate SSL parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
       so that it can authenticate the server and get authenticated by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
       server too as the SSL configuration of the server requires mutual
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
       authentication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
    </p>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1949
    <p>The next section will discuss how to connect to the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
    secure agent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
    </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
   </ul>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1953
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
  <h2><a name="h2-Connecting">Connecting to the Secure JMX Application</a></h2>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
    <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
    <p>We will now see how to connect to the secure agent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
       using jconsole, and using a programmatic client.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
    </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
    </ul>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1960
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
    <h3>Using jconsole to connect to the secure agent</h3>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
    <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
    <p>The only special thing you need to do in order to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
        be able to connect to your secure agent with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
        jconsole, is to give it a keystore (containing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
        its client certificate) and a truststore (containing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
        the certificates of the servers it can trust).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
        In our example, we use the same keystore/truststore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
        pair on the client and server side - but this is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
        not what a real application would do.
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1971
        Indeed a real application would have different
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1972
        certificates for the client and the server, and
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
        thus use different keystores (and probably truststores).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
        More information on SSL authentication can be obtained from the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
        href="http://java.sun.com/javase/6/docs/technotes/guides/security/jsse/JSSERefGuide.html#HowSSLWorks"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
        title="How SSL Works"
30678
a8b7fd8ede97 8075284: fix up miscellaneous TM constructions
darcy
parents: 27565
diff changeset
  1977
        >Java&trade; Secure Socket Extension (JSSE) Reference Guide</a>.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
    </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
    <p>To start jconsole with our provided keystore and
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1980
    truststore, go to the scandir example root directory and
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
    type in the following command:
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1982
    <p><code>jconsole
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1983
    &nbsp;-J-Djava.util.logging.config.file=logging.properties
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1984
    &nbsp;-J-Djavax.net.ssl.keyStore=keystore
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1985
    &nbsp;-J-Djavax.net.ssl.keyStorePassword=password
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1986
    &nbsp;-J-Djavax.net.ssl.trustStore=truststore
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
    &nbsp;-J-Djavax.net.ssl.trustStorePassword=trustword</code></p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
    </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
    <p>The <code>-J-Djava.util.logging.config.file=logging.properties</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
       flag is not mandatory, but passing a <code>logging.properties</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
       may help you debug connection problems if anything goes wrong.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
    </p>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  1993
    <p>In jconsole connection window, choose to connect to a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
       remote process, using the address <i>localhost:4545</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
       and the guest login:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
    </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
    <p><center><a href="docfiles/remote-connection.jpg"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
       ><img src="docfiles/remote-connection.jpg"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
        alt="jconsole connection window"/></a></center>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
    </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
    <p>You will see that the agent will let view all the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
       MBeans and their attributes, but will reject any
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
       attribute modification or remote method invocation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
    </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
    <hr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
    <p><u>Note:</u> if jconsole fails to connect and show
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
    you <a href="docfiles/remote-connection-failed.jpg">this screen</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
    you have probably misspelled some of the properties on jconsole
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  2009
    command line, or you didn't start jconsole from the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
    scandir example root directory where our <code>truststore</code>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  2011
    and <code>keystore</code> files are located. This article - <a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
    href="http://blogs.sun.com/roller/page/jmxetc?entry=troubleshooting_connection_problems_in_jconsole"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
    title="Troubleshooting connection problems in JConsole"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
    >Troubleshooting connection problems in JConsole</a> - may help
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
    you figure out what is going wrong.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
    </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
    <hr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
    </ul>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  2019
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
    <h3>Writing a programmatic client to connect to the secure agent</h3>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
    <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
        <p>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  2023
        In this section we will show the steps involved in writing
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
        a programmatic client that will connect to our secure agent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
        </p>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  2026
   <p>The <a
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  2027
href="dist/javadoc/com/sun/jmx/examples/scandir/ScanDirClient.html"
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 5506
diff changeset
  2028
title="The ScanDirClient class is a very short example of secure programmatic client"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
     >ScanDirClient</a> is an example class that shows how a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
    programmatic client can connect to a secured <i>scandir</i> application.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
    This class contains a <code>main</code> method which creates and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
    configures a <code>JMXConnector</code> client to connect with
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  2033
    the secured <i>scandir</i> agent.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
    </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
    <p>The secure client differs only from a non secure client in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
    so far as it needs to use SSL RMI Factories and credentials to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
    connect to the secure agent. The steps required mainly involve:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
       <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
           <li>Creating an empty environment map:
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  2040
           <pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
            // Create an environment map to hold connection properties
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
            // like credentials etc... We will later pass this map
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
            // to the JMX Connector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
            System.out.println("\nInitialize the environment map");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
            final Map&lt;String,Object> env = new HashMap&lt;String,Object>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
           </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
           </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
           <li>Putting the client's credentials in that map:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
           <i>(here the client will log in as <b>guest</b>)</i>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  2051
           <pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
            // Provide the credentials required by the server
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
            // to successfully perform user authentication
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
            final String[] credentials = new String[] { "guest" , "guestpasswd" };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
            env.put("jmx.remote.credentials", credentials);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
           </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
           </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
           <li>Providing an <code>SslRMIClientSocketFactory</code> to interact
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
           with the secure RMI Registry:
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  2061
           <pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
            // Provide the SSL/TLS-based RMI Client Socket Factory required
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
            // by the JNDI/RMI Registry Service Provider to communicate with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
            // the SSL/TLS-protected RMI Registry
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
            env.put("com.sun.jndi.rmi.factory.socket",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
                    new SslRMIClientSocketFactory());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
           </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
           </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
           <li>Creating a JMXConnector and connecting with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
               secure server:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
           <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
            // Create the RMI connector client and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
            // connect it to the secure RMI connector server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
            // args[0] is the server's host - localhost
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
            // args[1] is the secure server port - 4545
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
            System.out.println("\nCreate the RMI connector client and " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
                    "connect it to the RMI connector server");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
            final JMXServiceURL url = new JMXServiceURL(
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  2081
                    "service:jmx:rmi:///jndi/rmi://"+args[0]+":"+args[1]+
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
                    "/jmxrmi");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
            final JMXConnector jmxc = JMXConnectorFactory.connect(url, env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
           </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
           </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
       </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
       <p>For this to work, we also need to start the <code>ScanDirClient</code>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  2088
       with the appropriate system properties that will point to our
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  2089
       <code>keystore</code> and <code>truststore</code>. To start the secure
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
       client, go to the <i>scandir</i> example root directory and type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
       the following command:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
       <p><code>ant jar<br>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  2093
java
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  2094
    &nbsp;-Djava.util.logging.config.file=logging.properties
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
    &nbsp;-Djavax.net.ssl.keyStore=keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
    &nbsp;-Djavax.net.ssl.keyStorePassword=password
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
    &nbsp;-Djavax.net.ssl.trustStore=truststore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
    &nbsp;-Djavax.net.ssl.trustStorePassword=trustword
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
    &nbsp;-classpath&nbsp;dist/jmx-scandir.jar
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
     &nbsp;com.sun.jmx.examples.scandir.ScanDirClient&nbsp;localhost&nbsp;4545
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
       </code></p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
       </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
       <p>You should be seeing this trace:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
<center><table width="90%" border="0" bgcolor="#eeeeee">
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
<tr><td>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
<pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
Initialize the environment map
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
Create the RMI connector client and connect it to the RMI connector server
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
Connecting to: service:jmx:rmi:///jndi/rmi://localhost:4545/jmxrmi
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
Get the MBeanServerConnection
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
Get ScanDirConfigMXBean from ScanManagerMXBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
Get 'Configuration' attribute on ScanDirConfigMXBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
Configuration:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
&lt;ScanManager xmlns="jmx:com.sun.jmx.examples.scandir.config" name="testconfig">
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
    &lt;InitialResultLogConfig>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
        &lt;LogFileMaxRecords>2048&lt;/LogFileMaxRecords>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
        &lt;LogFileName>build/scandir.log&lt;/LogFileName>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
        &lt;MemoryMaxRecords>128&lt;/MemoryMaxRecords>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
    &lt;/InitialResultLogConfig>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
    &lt;DirectoryScannerList>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
        &lt;DirectoryScanner name="scan-build">
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
            &lt;Actions>NOTIFY LOGRESULT&lt;/Actions>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
            &lt;ExcludeFiles/>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
            &lt;IncludeFiles>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
                &lt;FileFilter>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
                    &lt;FilePattern>.*\.class&lt;/FilePattern>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
                    &lt;SizeExceedsMaxBytes>4096&lt;/SizeExceedsMaxBytes>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
                &lt;/FileFilter>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
            &lt;/IncludeFiles>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
            &lt;RootDirectory>build&lt;/RootDirectory>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
        &lt;/DirectoryScanner>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
    &lt;/DirectoryScannerList>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
&lt;/ScanManager>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
Invoke 'close' on ScanManagerMXBean
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  2143
Got expected security exception: java.lang.SecurityException: Access denied!
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
Invalid access level for requested MBeanServer operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
Close the connection to the server
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
Bye! Bye!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
</pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
</td></tr></table></center>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  2151
    <p>If the <code>ScanDirClient</code> fails to connect with
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  2152
       the secure agent, then this article - <a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
    href="http://blogs.sun.com/roller/page/jmxetc?entry=troubleshooting_connection_problems_in_jconsole"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
    title="Troubleshooting connection problems in JConsole"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
    >Troubleshooting connection problems in JConsole</a> - may help
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
    you figure out what is going wrong. Indeed the connection steps
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
    performed by the <code>ScanDirClient</code> are very similar to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
    those performed by <code>jconsole</code>, and the problems you
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  2159
    could encounter are identical. Just remember that
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
   <code>jconsole</code> needs the extra <code>-J</code> flag to pass
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
   system properties to the VM, which is not needed with regular
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
   <code>java</code> launcher invocations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
    </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
    </ul>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  2165
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
    <h2><a name="h2-Conclusion">Conclusion</a></h2>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
    <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
    <p>
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  2169
        In this document, we have presented an advanced
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  2170
        JMX example, and shown how to run a secure
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  2171
        JMX agent in a production environment.
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  2172
        We have also shown how to connect to such a
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  2173
        secure agent with both jconsole and a programmatic
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  2174
        client. We have also discuss various JMX
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  2175
        design-patterns and best practices.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
        Readers who would wish to learn more about JMX, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
        Monitoring and Management of the JVM, are invited
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
        to follow the links given in reference below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
    </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
    </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
  <h2><a name="h2-References">References</a></h2>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
  <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
     <li><a href="http://java.sun.com/products/JavaManagement/best-practices.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
        >JMX Best Practices</a>: This document describes best practices that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
      have been identified for modeling using the JMX API. </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
     <li><a href="http://java.sun.com/javase/6/docs/technotes/guides/management/agent.html"
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  2187
      >Monitoring and Management Using JMX</a>: How to enable, configure, and
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  2188
      connect to the JVM JMX agent.</li>
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  2189
     <li><a name="JConsole"><a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
href="http://java.sun.com/javase/6/docs/technotes/guides/management/jconsole.html"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
>Using JConsole</a>: JConsole is a JMX-Compliant monitoring tool which allows
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
     you to interact graphically with your own MBeans.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
     </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
     <li><a href="http://java.sun.com/javase/6/docs/technotes/guides/management/"
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  2195
     >Monitoring and Management for the Java Platform</a>: The Java Platform
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  2196
      Standard Edition (Java SE) 6 provides comprehensive monitoring and
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
      management support for the Java platform. </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
     <li><a href="http://java.sun.com/products/JavaManagement/community/jmx_blogs.html"
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  2199
         >List of JMX-related Blogs</a>: This page provides links to the
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
  2200
          different web logs written by members of the Sun team working on the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
          JMX API.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
     <li><a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
        href="http://java.sun.com/javase/6/docs/technotes/guides/security/jsse/JSSERefGuide.html#HowSSLWorks"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
        title="The JSSE Reference Guide"
30678
a8b7fd8ede97 8075284: fix up miscellaneous TM constructions
darcy
parents: 27565
diff changeset
  2205
        >Java&trade; Secure Socket Extension (JSSE) Reference Guide</a>:
a8b7fd8ede97 8075284: fix up miscellaneous TM constructions
darcy
parents: 27565
diff changeset
  2206
        comprehensive documentation about the Java&trade; Secure Socket
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
        Extension (JSSE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
     </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
     <li><a href="http://java.sun.com/javase/6/docs/"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
         >Java SE 6 Documentation Index</a>: This document covers the
30678
a8b7fd8ede97 8075284: fix up miscellaneous TM constructions
darcy
parents: 27565
diff changeset
  2211
          Java&trade; Platform, Standard Edition 6 JDK.</li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
  </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
  <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
  <hr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2215
  <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
  </body>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
</html>