是否有任何库允许我从Shell脚本调用JMX MBean方法。我们通过JMX公开了一些操作/管理命令,我们可以让管理员使用JConsole或VisualVM,但最好将某些任务留给自动化。在这种自动化中,我们希望能够在运行的服务器上调用JMX MBean方法,最好是从Shell脚本中调用。
可以使用以下命令行JMX实用程序:
Groovy JMX示例:
import java.lang.management.* import javax.management.ObjectName import javax.management.remote.JMXConnectorFactory as JmxFactory import javax.management.remote.JMXServiceURL as JmxUrl def serverUrl = 'service:jmx:rmi:///jndi/rmi://localhost:9003/jmxrmi' String beanName = "com.webwars.gameplatform.data:type=udmdataloadsystem,id=0" def server = JmxFactory.connect(new JmxUrl(serverUrl)).MBeanServerConnection def dataSystem = new GroovyMBean(server, beanName) println "Connected to:\n$dataSystem\n" println "Executing jmxForceRefresh()" dataSystem.jmxForceRefresh();
cmdline-jmxclient示例:
如果您有
使用一个称为:
然后,您可以编写一个简单的bash脚本(假设您下载了cmdline- jmxclient-0.10.3.jar并放置在与脚本相同的目录中):
#!/bin/bash cmdLineJMXJar=./cmdline-jmxclient-0.10.3.jar user=yourUser password=yourPassword jmxHost=localhost port=9003 #No User and password so pass '-' echo "Available Operations for com.company.data:type=datasystem,id=0" java -jar ${cmdLineJMXJar} ${user}:${password} ${jmxHost}:${port} com.company.data:type=datasystem,id=0 echo "Executing XML update..." java -jar ${cmdLineJMXJar} - ${jmxHost}:${port} com.company.data:type=datasystem,id=0 jmxForceRefresh