@Dougnukem回答帮助我做了很多工作。 我采用了格罗维尼办法(采用2.3.3格罗夫办法)。
I did some changes on Dougnukem code.
This will work with Java 7 and will print two attributes to stdout every 10 sec.
package com.my.company.jmx
import groovy.util.GroovyMBean;
import javax.management.remote.JMXServiceURL
import javax.management.remote.JMXConnectorFactory
import java.lang.management.*
class Monitor {
static main(args) {
def serverUrl = service:jmx:rmi:///jndi/rmi://localhost:5019/jmxrmi
String beanName = "Catalina:type=DataSource,class=javax.sql.DataSource,name="jdbc/CommonDB""
println "numIdle,numActive"
while(1){
def server = JMXConnectorFactory.connect(new JMXServiceURL(serverUrl))
//make sure to reconnect in case the jvm was restrated
server.connect()
GroovyMBean mbean = new GroovyMBean(server.MBeanServerConnection, beanName)
println "${mbean.numIdle},${mbean.numActive}"
server.close()
sleep(10000)
}
}
}
Compile this code into a jar using maven-compiler-plugin so you will not require groovy installation only the groovy-all.jar .
Below is the relevant plugin definition and dependency.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerId>groovy-eclipse-compiler</compilerId>
<source>1.7</source>
<target>1.7</target>
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-compiler</artifactId>
<version>2.8.0-01</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-batch</artifactId>
<version>2.3.4-01</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.4.3</version>
</dependency>
</dependencies>
用批量或批量进行校对,并将印刷数据,以便停用。