- Groovy - Meta Object Programming
- Groovy - Template Engines
- Groovy - Unit Testing
- Groovy - Command Line
- Groovy - Builders
- Groovy - Database
- Groovy - DSLS
- Groovy - JSON
- Groovy - JMX
- Groovy - XML
- Groovy - Annotations
- Groovy - Closures
- Groovy - Traits
- Groovy - Generics
- Groovy - Object Oriented
- Groovy - Exception Handling
- Groovy - Regular Expressions
- Groovy - Dates & Times
- Groovy - Maps
- Groovy - Lists
- Groovy - Ranges
- Groovy - Strings
- Groovy - Numbers
- Groovy - Optionals
- Groovy - File I/O
- Groovy - Methods
- Groovy - Decision Making
- Groovy - Loops
- Groovy - Operators
- Groovy - Variables
- Groovy - Data Types
- Groovy - Basic Syntax
- Groovy - Environment
- Groovy - Overview
- Groovy - Home
Groovy Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Groovy - JMX
JMX is the defacto standard which is used for monitoring all apppcations which have anything to do with the Java virual environment. Given that Groovy sits directly on top of Java, Groovy can leverage the tremendous amount of work already done for JMX with Java.
Monitoring the JVM
One can use the standard classes available in java.lang.management for carrying out the monitoring of the JVM. The following code example shows how this can be done.
import java.lang.management.* def os = ManagementFactory.operatingSystemMXBean println """OPERATING SYSTEM: OS architecture = $os.arch OS name = $os.name OS version = $os.version OS processors = $os.availableProcessors """ def rt = ManagementFactory.runtimeMXBean println """RUNTIME: Runtime name = $rt.name Runtime spec name = $rt.specName Runtime vendor = $rt.specVendor Runtime spec version = $rt.specVersion Runtime management spec version = $rt.managementSpecVersion """ def mem = ManagementFactory.memoryMXBean def heapUsage = mem.heapMemoryUsage def nonHeapUsage = mem.nonHeapMemoryUsage println """MEMORY: HEAP STORAGE: Memory committed = $heapUsage.committed Memory init = $heapUsage.init Memory max = $heapUsage.max Memory used = $heapUsage.used NON-HEAP STORAGE: Non-heap memory committed = $nonHeapUsage.committed Non-heap memory init = $nonHeapUsage.init Non-heap memory max = $nonHeapUsage.max Non-heap memory used = $nonHeapUsage.used """ println "GARBAGE COLLECTION:" ManagementFactory.garbageCollectorMXBeans.each { gc -> println " name = $gc.name" println " collection count = $gc.collectionCount" println " collection time = $gc.collectionTime" String[] mpoolNames = gc.memoryPoolNames mpoolNames.each { mpoolName -> println " mpool name = $mpoolName" } }
When the code is executed, the output will vary depending on the system on which the code is run. A sample of the output is given below.
OPERATING SYSTEM: OS architecture = x86 OS name = Windows 7 OS version = 6.1 OS processors = 4 RUNTIME: Runtime name = 5144@Babup-PC Runtime spec name = Java Virtual Machine Specification Runtime vendor = Oracle Corporation Runtime spec version = 1.7 Runtime management spec version = 1.2 MEMORY: HEAP STORAGE: Memory committed = 16252928 Memory init = 16777216 Memory max = 259522560 Memory used = 7355840 NON-HEAP STORAGE: Non-heap memory committed = 37715968 Non-heap memory init = 35815424 Non-heap memory max = 123731968 Non-heap memory used = 18532232 GARBAGE COLLECTION: name = Copy collection count = 15 collection time = 47 mpool name = Eden Space mpool name = Survivor Space name = MarkSweepCompact collection count = 0 collection time = 0 mpool name = Eden Space mpool name = Survivor Space mpool name = Tenured Gen mpool name = Perm Gen mpool name = Perm Gen [shared-ro] mpool name = Perm Gen [shared-rw]
Monitoring Tomcat
In order to monitor tomcat, the following parameter should be set when tomcat is started −
set JAVA_OPTS = -Dcom.sun.management.jmxremote Dcom.sun.management.jmxremote.port = 9004 -Dcom.sun.management.jmxremote.authenticate=false Dcom.sun.management.jmxremote.ssl = false
The following code uses JMX to discover the available MBeans in the running Tomcat, determine which are the web modules and extract the processing time for each web module.
import groovy.swing.SwingBuilder import javax.management.ObjectName import javax.management.remote.JMXConnectorFactory as JmxFactory import javax.management.remote.JMXServiceURL as JmxUrl import javax.swing.WindowConstants as WC import org.jfree.chart.ChartFactory import org.jfree.data.category.DefaultCategoryDataset as Dataset import org.jfree.chart.plot.PlotOrientation as Orientation def serverUrl = service:jmx:rmi:///jndi/rmi://localhost:9004/jmxrmi def server = JmxFactory.connect(new JmxUrl(serverUrl)).MBeanServerConnection def serverInfo = new GroovyMBean(server, Catapna:type = Server ).serverInfo println "Connected to: $serverInfo" def query = new ObjectName( Catapna:* ) String[] allNames = server.queryNames(query, null) def modules = allNames.findAll { name -> name.contains( j2eeType=WebModule ) }.collect{ new GroovyMBean(server, it) } println "Found ${modules.size()} web modules. Processing ..." def dataset = new Dataset() modules.each { m -> println m.name() dataset.addValue m.processingTime, 0, m.path }Advertisements