dp

Java Management Extension (JMX)

JMX (Java Management Extension) is an extension allowing a JMX client, e.g. JConsole (delivered with the JDK) to connect to a JMX server and setting variables, invoking methods on the fly. this can be very useful for e.g. changing log-levels, configuration values, etc., thus it can be used as a simple admin interface

an example can be found in src1, where u can see that we have a interface named HelloMBean defining the methods that should be visible, and a class implementing the interface HelloMBean named Hello.

it’s not explicitly stated, but when using MBean both the interface and the class implementing the interface have to be in the same package.

with a JDK newer or equal to 1.6 MXBean s were introduced, where u don’t have to put both the interface and the class into the same package.

sources

src1, src2, src3, src4

mbean server

in order to start the mbean server with the application the following has to be added to the run configuration of your IDE, or just to a start script (shell, batch)


#!/bin/sh
java -Dcom.sun.management.jmxremote \
-Dcom.sun.management.jmxremote.port=1617 \
-Dcom.sun.management.jmxremote.authenticate=false \
-Dcom.sun.management.jmxremote.ssl=false \
App

sample application

run the sample application and connect to it via JConsole and check how the bean is presented there.

main class

as u can see, the MBeanServer has to be started, and the object have to be registered.

public class App {

private static Logger log = Logger.getLogger("app");
private MBeanServer mbs = null;

public static void main(String[] args) {
System.out.println("Hello World!");
App app = new App();
for (int cnt = 0; cnt < 5; cnt++) { app.waitForEnterPressed(); } } Hello hello = new Hello(); LogLevelChanger llchanger = new LogLevelChanger(); public App() { mbs = ManagementFactory.getPlatformMBeanServer(); try { ObjectName name = new ObjectName("SS:name=hello"); mbs.registerMBean(hello, name); name = new ObjectName("SS:log=log"); mbs.registerMBean(llchanger, name); } catch (Exception e) { log.error(e.getMessage(), e); } } private void waitForEnterPressed() { try { log.info("waiting info"); log.debug("waiting debug"); log.error("waiting error"); log.warn("waiting warn"); hello.sayHello(); bla.test(); System.out.println("Press to continue..."); System.in.read(); } catch (Exception e) { log.error(e.getMessage(), e); } } }

interface


public interface HelloMBean {
public void setMessage(String msg);
public String getMessage();
public void sayHello();
}

interface implementation


public class Hello implements HelloMBean {
private String msg = "";
public Hello() {
msg = "howdy!";
}
public Hello(String msg) {
this.msg = msg;
}
public void setMessage(String msg) {
this.msg = msg;
}
public String getMessage() {
return msg;
}
public void sayHello() {
System.out.println(msg);
}
}

security

security (SSL, authentication) is discussed here

authentication

taken from here

Single User Environment: set up the password file in the JRE_HOME/lib/management directory as follows:

  1. Copy the password template file, jmxremote.password.template, to management.jmxremote.password.
  2. Set file permissions so that only the owner can read and write the password file.
  3. Add passwords for roles such as monitorRole and controlRole.

Multi-user Environment: set up the password file in the JRE_HOME/lib/management directory as follows:

  1. Copy the password template file, jmxremote.password.template, to your home directory.
  2. Set file permissions so that only you can read and write the password file.
  3. Add passwords for the roles such as monitorRole and controlRole.
  4. Set this system property when you start the JVM: com.sun.management.jmxremote.password.file=pwFilePath

Comments are closed.