JMX-RMI实现

标准Mbean
package jmxtest;

import java.io.Serializable;  
import java.util.concurrent.atomic.AtomicInteger;
  
/** 
 * 
 * @author fengzl 
 */  
public interface HelloWorldMBean extends Serializable {  
  
    void setName(String name);  
  
    String getName();  
  
    void sayHello();  
  
    String getHelloString();  
  
    int getId();  
  
    String toString();  
  
    int hashCode();  
  
    boolean equals( Object obj);  
      
    HelloWorldMBean getThis();  
}  

class HelloWorld implements HelloWorldMBean, Serializable {  
	  
    static AtomicInteger count = new AtomicInteger();  
    private static final long serialVersionUID = 1627976932729278650L;  
    int id = count.incrementAndGet();  
    String name;  
  
    public synchronized void setName(String name) {  
        this.name = name;  
    }  
  
    public synchronized String getName() {  
        return name;  
    }  
  
    public synchronized void sayHello() {  
        System.out.println(getHelloString());  
    }  
  
    public synchronized String getHelloString() {  
        return "Hello, " + name;  
    }  
  
    public synchronized int getId() {  
        return id;  
    }  
  
    public HelloWorldMBean getThis() {  
        return this;  
    }  
}  



服务端注册
/*
 * Copyrights (C) 2008 Bearice ([email protected])
 * Release under GNU/GPL Version 2.
 */
package jmxtest;

import java.rmi.registry.LocateRegistry;
import java.util.HashMap;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.ObjectInstance;
import javax.management.ObjectName;
import javax.management.remote.JMXAuthenticator;
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;
import javax.security.auth.Subject;

public class Main {  
	  
	    /** 
	     * @param args the command line arguments 
	     */  
	    public static void main(String[] args) throws Exception {  
	        LocateRegistry.createRegistry(8877);  
	        MBeanServer server = MBeanServerFactory.createMBeanServer();  
	        ObjectName helloName = new ObjectName("hello:name=hello");  
	        HelloWorld hello = new HelloWorld();  
	        HashMap<String, Object> prop = new HashMap<String, Object>();  
	        prop.put(JMXConnectorServer.AUTHENTICATOR, new JMXAuthenticator() {  
	  
	            public Subject authenticate(Object credentials) {  
	                System.out.println(credentials);  
	                if (credentials instanceof String) {  
	                    if (credentials.equals("hello")) {  
	                        return new Subject();  
	                    }  
	                }  
	                throw new SecurityException("not authicated");  
	            }  
	        });  
	        JMXConnectorServer cserver =  
	                JMXConnectorServerFactory.newJMXConnectorServer(  
	                new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:8877/hello"), prop, server);  
	        cserver.start();  
	        server.registerMBean(hello, helloName);  
	        for (ObjectInstance object : server.queryMBeans(null, null)) {  
	            System.out.println(object.getObjectName());  
	        }  
	        System.out.println(hello);  
	        System.out.println("start.....");  
	    }  
	}  




客户端验证
package jmxtest;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.HashMap;

import javax.management.JMX;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

public class Client {  
	  
	    static HelloWorldMBean hello;  
	  
	    public static void main(String[] args) throws Exception {  
	        final AtomicInteger count = new AtomicInteger();  
	        HashMap<String,Object> prop = new HashMap<String, Object>();  
	        prop.put(JMXConnector.CREDENTIALS, "hello");  
	        final JMXConnector conn = JMXConnectorFactory.connect(new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:8877/hello"), prop);  
	        conn.connect();  
	        Runnable r = new Runnable() {  
	  
	            public void run() {  
	                for (int i = 0; i < 10; i++) {  
	                    try {  
	                        final HelloWorldMBean hello = JMX.newMBeanProxy(conn.getMBeanServerConnection(), new ObjectName("hello:name=hello"), HelloWorldMBean.class);  
	                        hello.setName("bearice");  
	                        hello.sayHello();  
	                        hello.getHelloString();  
	                        System.out.println(count.incrementAndGet());  
	                        Client.hello = hello;  
	                        System.out.println(hello.equals(hello.getThis()));  
	                    } catch (Exception ex) {  
	                        Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);  
	                    }  
	                }  
	            }  
	        };  
	        Thread[] ts = new Thread[5];  
	        for (int i = 0; i < ts.length; i++) {  
	            ts[i] = new Thread(r);  
	            ts[i].start();  
	        }  
	        for (Thread t : ts) {  
	            t.join();  
	        }  
	        System.out.println(hello.equals(hello.getThis()));  
	        System.out.println(hello.getId());  
	        System.out.println(hello);  
	    }  
	}  

你可能感兴趣的:(java,thread,Security,Gmail)