Appendix A. Running MX4J's HttpAdaptor

Below is an agent that sets up MX4j's HttpAdaptor to serve HTML, then runs it. Setup is trivial with Java 1.5 or later. I ran this adaptor for years with Java 1.4, but if you run Java older than 1.5, you will need to make sure that your CLASSPATH includes a JMX implementation and several XML and XSLT libraries (contact me if you want an exact list which works).

To run with Java 1.5 or newer, just make sure that the MX4JHttpAdaptor class and mx4j-tools.jar from the MX4J distribution are in your CLASSPATH, then run java MX4JHttpAdaptor.

You can download this source code from MX4JHttpAdaptor.java.

import mx4j.tools.adaptor.http.HttpAdaptor;
import javax.management.MBeanServerFactory;
import javax.management.MBeanServer;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import mx4j.tools.adaptor.http.ProcessorMBean;
import mx4j.tools.adaptor.http.XSLTProcessor;

/**
 * Runs MX4J's HttpAdaptor with an XSLT Processor.
 * This results in a generic HTML over HTTP JMX Management Console.
 *
 * Syntax:  java MX4JHtmlAdaptor [PORT]   (port defaults to 8112)
 */
public class MX4JHttpAdaptor {
    /**
     * Runs as a standalone Java process.
     * Serves its own HTTP, so no http container needed.
     * Also has its own JMX Agent, so no JMX ConnectorServer is needed.
     */
    static public void main(String[] sa) throws Exception {
        // Must use an MBeanServer (a.o.t. a MBeanServerConnection, since
        // the method HttpAdaptor.setProcessor() is not JMX-exposed.
        MBeanServer mbs = MBeanServerFactory.createMBeanServer();

        ObjectName name = new ObjectName(
                "mx4j:class=mx4j.tools.adaptor.http.HttpAdaptor,id=1");
        // Make our own Java object so we can run adaptor.setProcessor() below.
        mx4j.tools.adaptor.http.HttpAdaptor adaptor =
                new mx4j.tools.adaptor.http.HttpAdaptor(
                        ((sa.length == 0) ? 8112 : Integer.parseInt(sa[0])),
                        "0.0.0.0");
        // Previous line sets the listen port and hostname.
        // "0.0.0.0" listens to all IP addresses.  You can use a specific
        // host name or IP address if you wish.

        mbs.registerMBean(adaptor, name);
        mbs.invoke(name, "start", null, null);
        adaptor.setProcessor(new XSLTProcessor());
        /* De-comment this block to run this in foreground.
        System.err.println("Adaptor started.  Hit ENTER to exit completely.");
        System.in.read();
        mbs.invoke(name, "stop", null, null);
        System.out.println("Exiting Mx4j.main()");
         */
    }
}
Viewing a SampleADM MBean instance with MX4J Http Adaptor as described

For the convenience of UNIX users, you can download my startup script from runmx4j.ksh or script from runmx4j.bash (depending on your preference for Korn or Bash). You will have to fix the file paths for your environment. You can also modify it to run other programs as daemons. Invoke it like nohup ./runmx4j.ksh or nohup ./runmx4j.bash in order to disassociate it from your login shell.