java中的等效GroovyMBean

时间:2012-01-31 15:54:17

标签: java groovy jmx

我的服务器中有一些JMX配置。现在,当我想要它时,我从groovy中调用它:

GroovyMBean mbean = new GroovyMBean(server, application)
mbean.invokeMethod(function,parameter) 

java中的GroovyMBean相当于什么,因为现在我想从java代码中调用此配置?

2 个答案:

答案 0 :(得分:0)

如果我正确理解您的问题,此代码段将在Java中用于连接到JMXServer。

     JMXConnector jmxConnector = null;
            MBeanServerConnection mbeanConn = null;
            try {
                String url = "service:jmx:rmi:///jndi/rmi://" + this.server + ":" + this.port + "/jmxrmi";
                JMXServiceURL serviceUrl;

                serviceUrl = new JMXServiceURL(url);
                jmxConnector = JMXConnectorFactory.connect(serviceUrl, null);
                mbeanConn = jmxConnector.getMBeanServerConnection();
}catch(Exception e)
{
}
finally
{
//close jmx connector.
}

答案 1 :(得分:0)

抱歉这么晚了。我刚遇到类似的问题。没有mBean类,但是有通用对象类。您的服务网址也缺少部分。 ObjectName是mbean名称,methodName是要调用的函数。

MBeanServerConnection connection = null;
HashMap<String,String[]> jmxEnv = new HashMap<String, String[]>();
String[] credentials = new String[2];
credentials[0] = JMX_User;
credentials[1] = JMX_Password;
jmxEnv.put(JMXConnector.CREDENTIALS,credentials);

JMXServiceURL serverUrl = null;

serverUrl = new JMXServiceURL("service:jmx:rmi://"+JMX_Server+":"+JMX_Port+"/jndi/rmi://"+JMX_Server+":"+JMX_Registry_Port+"/server");
connection = JMXConnectorFactory.connect(serverUrl, jmxEnv).getMBeanServerConnection();
ObjectName beanName = null;
beanName = new ObjectName("ObjectName");
Object[] params = new Object[1];
params[0]=fileName;
String[] signature = new String[0];
connection.invoke(beanName, "methodName", params, signature);