如何实现JMX身份验证?

时间:2018-03-21 18:52:33

标签: java jmx mbeans

当我运行SystemConfigClient.java时出现此错误:

**

  

线程“main”中的异常   java.lang.reflect.UndeclaredThrowableException at   com.sun.proxy。$ Proxy0.doConfig(未知来源)at   com.jmx.SystemConfigClient.main(SystemConfigClient.java:37)引起:   javax.management.InstanceNotFoundException:com.jmx:type = SystemConfig     在   com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getMBean(未知   来源)at   com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getClassLoaderFor(未知   来源)at   com.sun.jmx.mbeanserver.JmxMBeanServer.getClassLoaderFor(未知   来源)at   javax.management.remote.rmi.RMIConnectionImpl $ 6.run(未知来源)     在java.security.AccessController.doPrivileged(Native Method)at   javax.management.remote.rmi.RMIConnectionImpl.getClassLoaderFor(未知   来源)at   javax.management.remote.rmi.RMIConnectionImpl.invoke(未知来源)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)at   sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源)at   java.lang.reflect.Method.invoke(未知来源)at   sun.rmi.server.UnicastServerRef.dispatch(未知来源)at   sun.rmi.transport.Transport $ 1.run(未知来源)at   sun.rmi.transport.Transport $ 1.run(未知来源)at   java.security.AccessController.doPrivileged(Native Method)at   sun.rmi.transport.Transport.serviceCall(未知来源)at   sun.rmi.transport.tcp.TCPTransport.handleMessages(未知来源)at   sun.rmi.transport.tcp.TCPTransport $ ConnectionHandler.run0(未知   来源)at   sun.rmi.transport.tcp.TCPTransport $ ConnectionHandler.lambda $运行$ 0(未知   来自java.security.AccessController.doPrivileged(Native Method)     at sun.rmi.transport.tcp.TCPTransport $ ConnectionHandler.run(未知   来自)java.util.concurrent.ThreadPoolExecutor.runWorker(未知   来自java.util.concurrent.ThreadPoolExecutor $ Worker.run(未知   来自)java.lang.Thread.run(未知来源)at   sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(未知   来源)在sun.rmi.transport.StreamRemoteCall.executeCall(未知   来源)在sun.rmi.server.UnicastRef.invoke(未知来源)at   com.sun.jmx.remote.internal.PRef.invoke(未知来源)at   javax.management.remote.rmi.RMIConnectionImpl_Stub.invoke(未知   来源)at   javax.management.remote.rmi.RMIConnector $ RemoteMBeanServerConnection.invoke(未知   来源)at   javax.management.MBeanServerInvocationHandler.invoke(未知来源)     ......还有2个

**

我的Mbean:

package com.jmx;
public interface SystemConfigMBean {
    public void setThreadCount (int noOfThreads);
    public int getThreadCount();
    public void setSchemaName (String schemaName);
    public String getSchemaName();
    public String doConfig();
}

这是我的SystemConfig.java:

package com.jmx;
public class SystemConfig implements SystemConfigMBean{ 
    private int threadCount;
    private String schemaName;  
    public SystemConfig(int numThreads, String schema) {
        this.threadCount = numThreads;
        this.schemaName=schema;
    }
    @Override
    public void setThreadCount(int noOfThreads) {
        // TODO Auto-generated method stub
        this.threadCount=noOfThreads;       
    }
    @Override
    public int getThreadCount() {
        // TODO Auto-generated method stub
        return this.threadCount;
    }
    @Override
    public void setSchemaName(String schemaName) {
        // TODO Auto-generated method stub
        this.schemaName= schemaName;        
    }
    @Override
    public String getSchemaName() {
        // TODO Auto-generated method stub
        return this.schemaName;
    }
    @Override
    public String doConfig() {
        // TODO Auto-generated method stub
        return "No.of Threads="+this.threadCount+" and DB SchemaName"+ this.schemaName;
    }
}

这是我的SystemConfigManagement.java文件

package com.jmx;
import java.lang.management.ManagementFactory;
import javax.management.InstanceAlreadyExistsException;
import javax.management.MBeanRegistrationException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.NotCompliantMBeanException;
import javax.management.ObjectName;

public class SystemConfigManagement {
    private static final int DEFAULT_NO_THREADS=10;
    private static final String DEFAULT_SCHEMA="default";
    public static void main(String[] args) throws MalformedObjectNameException, InterruptedException, InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException {
        //Get the MBean server
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
        //register the MBean
        SystemConfig mBean = new SystemConfig(DEFAULT_NO_THREADS, DEFAULT_SCHEMA);
        ObjectName name = new ObjectName("com.jmx:type=SystemConfig");
        mbs.registerMBean(mBean, name);
        do{
            Thread.sleep(3000);
            System.out.println("Thread Count="+mBean.getThreadCount()+":::Schema Name="+mBean.getSchemaName());
        }while(mBean.getThreadCount() !=0);
    }
}

这是我的SystemConfigClient.java:

package com.jmx;
import java.io.IOException;
import javax.management.MBeanServerConnection;
import javax.management.MBeanServerInvocationHandler;
import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

public class SystemConfigClient {   
    //public static final String HOST = "10.61.3.171";
    //public static final String PORT = "1099"; 
    public static void main (String[] args) throws IOException, MalformedObjectNameException {      
        JMXServiceURL url = new JMXServiceURL ("service:jmx:rmi:///jndi/rmi://:1099/jmxrmi");       
        JMXConnector jmxConnector = JMXConnectorFactory.connect(url);   
        MBeanServerConnection mbeanServerConnection = jmxConnector.getMBeanServerConnection();      
        //ObjectName should be same as your MBean nam       
        ObjectName mbeanName = new ObjectName ("com.jmx:type=SystemConfig");        
        //Get MBean proxy instance that will be used to make calls to registered MBean      
        SystemConfigMBean mbeanProxy = (SystemConfigMBean) MBeanServerInvocationHandler.newProxyInstance(mbeanServerConnection,  mbeanName,  SystemConfigMBean.class, true);        
          //let's make some calls to mbean through proxy and see the results.       
        System.out.println("current systemConfig::" + mbeanProxy.doConfig());       
        mbeanProxy.setSchemaName("NEwSchema");
        mbeanProxy.setThreadCount(5);       
        System.out.println("New SystemConfig::" + mbeanProxy.doConfig());       
         //let's terminate the mbean by making thread count as 0    
        mbeanProxy.setThreadCount(0);       
        //close the connection      
        jmxConnector.close();
    }
}

0 个答案:

没有答案