Created
October 14, 2016 08:31
-
-
Save alex-1q84/838e67c6b628e5539a39aaf5b30e2ed7 to your computer and use it in GitHub Desktop.
call jmx use groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.management.* | |
import javax.management.ObjectName | |
import javax.management.remote.JMXConnector | |
import javax.management.remote.JMXConnectorFactory as JmxFactory | |
import javax.management.remote.JMXServiceURL as JmxUrl | |
def callJmx(String jmxUrl, String beanName, String operator, Object args, String userName=null, String password=null) { | |
def jmxEnv = null | |
if (userName != null && password != null) { | |
jmxEnv = [(JMXConnector.CREDENTIALS): (String[])[userName, password]] | |
} | |
def server = JmxFactory.connect(new JmxUrl(jmxUrl), jmxEnv).MBeanServerConnection | |
def dataSystem = new GroovyMBean(server, beanName) | |
println "Connected to:\n$dataSystem\n" | |
println "Executing ${operator}(${args})" | |
//dataSystem.stopAll() | |
try { | |
dataSystem."${operator}"(args) | |
} catch (MissingMethodException) { | |
if ( args == null) { | |
dataSystem."${operator}"() | |
} | |
} | |
} | |
callJmx('service:jmx:rmi:///jndi/rmi://127.0.0.1:9977/jmxrmi', 'xxx:name=MQxxxService', 'stopAll', null, 'jmxUser', 'jmxPassword') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment