Advertisement
Guest User

JMX Invoke Script Working Example

a guest
Jul 19th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 1.60 KB | None | 0 0
  1. import javax.management.remote.JMXConnectorFactory as JmxFactory
  2. import javax.management.remote.JMXServiceURL as JmxUrl
  3.  
  4. def props = new Properties()
  5. def file = new File('./jmx.properties')
  6. if (file.exists()){
  7.     new File('jmx.properties').withInputStream {
  8.         stream -> props.load(stream)
  9.     }
  10. } else{
  11.     props.putAll([host : 'localhost', port : 0000])
  12. }
  13.  
  14. def serverUrl = "service:jmx:rmi:///jndi/rmi://${props.host}:${props.port}/jmxrmi"
  15.  
  16. String beanName = "analytics:name=Groovy executor"
  17. def server = JmxFactory.connect(new JmxUrl(serverUrl)).MBeanServerConnection
  18. def dataSystem = new GroovyMBean(server, beanName)
  19.  
  20. String result
  21. String[] args = new String[1]
  22. args[0] = "classpath_script_filename"
  23. if (args.length == 0){
  24.     result = 'required parameter (script name) is absent!'
  25. } else if (args.length == 1) {
  26.     String scriptName = args[0]
  27.     println "invoke script '$scriptName' on $serverUrl"
  28.     result = dataSystem.executeScriptInContext(scriptName);
  29. } else {
  30.     /** аргументов больше одного, интерпретируем их как выражения key=value **/
  31.     String scriptName = args[0]
  32.  
  33.     Map<String, String> map = [:]
  34.     for (int i = 1; i < args.length; i++) {
  35.         String argument = args[i]
  36.         if (argument ==~ /\p{Alnum}+=\p{Alnum}+/){
  37.             String[] arr = argument.split('=')
  38.             map[arr[0]] = arr[1]
  39.             println argument
  40.         }
  41.     }
  42.     println "invoke script '$scriptName' on $serverUrl with args:${map}"
  43.     result = dataSystem.executeScriptInContextWithArgs(scriptName,map)
  44. }
  45. println result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement