Skip to content

Instantly share code, notes, and snippets.

@zedar
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zedar/82f10d1064b00cbea79d to your computer and use it in GitHub Desktop.
Save zedar/82f10d1064b00cbea79d to your computer and use it in GitHub Desktop.
ratpack with remote jmx

Intro

To enable remote jmx monitoring across firewall it is necessary to pass following JVM arguments:

-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=5999
-Dcom.sun.management.jmxremote.local.only=false
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.rmi.port=5998

IMPORTANT: jmxremote.port supplies RMI registry port. JMX RMI connections objects are exposed through another, random port or through the port defined by jmxremote.rmi.port property.

ratpack default configuration contains scripts to run server with Gradle Wrapper (./gradlew run). Then there are two processes running. One is for gradle itself and the other is for ratpack application.

JMX properties have to be passed to ratpack application process. Setting them in gradlew scripts does not help. Then they apply to gradle process only.

Thanks to the discussion on ratpack forum two solutions are feasible.

Loading properties with jvmArgs

Inside build.gradle file add run{} task with jvmArgs line for everyone setting.

run {
  jvmArgs "-Dcom.sun.management.jmxremote"
  jvmArgs "-Dcom.sun.management.jmxremote.port=5999"
  jvmArgs "-Dcom.sun.management.jmxremote.local.only=false"
  jvmArgs "-Dcom.sun.management.jmxremote.authenticate=false"
  jvmArgs "-Dcom.sun.management.jmxremote.ssl=false"
  jvmArgs "-Dcom.sun.management.jmxremote.rmi.port=5998"
}

Loading properties with configuration file

Add new file in for example application root folder called jmx.env. Put following settings inside it:

com.sun.management.jmxremote
com.sun.management.jmxremote.port=5999
com.sun.management.jmxremote.local.only=false
com.sun.management.jmxremote.authenticate=false
com.sun.management.jmxremote.ssl=false
com.sun.management.jmxremote.rmi.port=5998

Inside build.gradle file add run{} task and read all properties from jmx.env file:

run {
  Properties properties = new Properties(System.getProperties())
  try {
    FileInputStream inputStream = new FileInputStream("./jmx.env")
    properties.load(inputStream)
    System.setProperties(properties)
    System.getProperties().each { prop ->
        systemProperty prop.key, prop.value
    }
  }
  catch (FileNotFoundException ex) {
    println "Could not find env file"
    println "This is normal in production."
  } 
  catch (Exception e) {
    e.printStackTrace()
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment