Version 4.1 by Vincent Massol on 2011/05/06 14:52

cogGet and set JMX attributes
TypeSnippet
Category
Developed byUnknown
Rating
0 Votes
LicenseUnknown

Description

Shows how to use Groovy to access (get/set) JMX attributes.

Example 1: TCPPING get/set log level

Shows how to display and how to change the current log level of a JGroups Protocol (TCPPING). This allows enabling logging JGroups information in a running XWiki instance, which is useful when trying to diagnose issues in a XWiki Cluster (it uses JGroups).

{{groovy}}
import java.lang.management.ManagementFactory;
import javax.management.MBeanServer;

MBeanServer server = ManagementFactory.getPlatformMBeanServer();
def mbean = new GroovyMBean(server, 'jgroups:type=protocol,cluster=event,protocol=TCPPING')

println "Current log level for TCPPING:"
println mbean.Level

println "Set log level to DEBUG"
mbean.Level = "debug"
{{/groovy}}

Example 2: List all JGroups attribute values

{{groovy}}
import java.lang.management.ManagementFactory;
import javax.management.MBeanServer;
import javax.management.ObjectName;

MBeanServer server = ManagementFactory.getPlatformMBeanServer();

def query = new ObjectName('jgroups:*')
def allNames = server.queryNames(query, null)

for(protocol in allNames) {
 def mbean = new GroovyMBean(server, protocol)
  println "== Config for {{{ ${protocol} }}} =="
 for(attr in mbean.listAttributeNames()) {
    println "* ${attr}: ${mbean.getProperty(attr)}"
  }
}
{{/groovy}}

Results in:

jmx.png

Get Connected