JMX-RMI

最近发现JMX是个好东西,很适用于分布式系统的控制信息传递。

/*
 * 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;

/**
 *
 * @author Bearice
 */
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 prop = new HashMap();
        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.....");
    }
}

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

import java.util.HashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.management.JMX;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

/**
 *
 * @author Bearice
 */
public class Client {

    static HelloWorldMBean hello;

    public static void main(String[] args) throws Exception {
        final AtomicInteger count = new AtomicInteger();
        HashMap prop = new HashMap();
        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);
    }
}

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

import java.io.Serializable;

/**
 *
 * @author Bearice
 */
public interface HelloWorldMBean extends Serializable {

    void setName(String name);

    String getName();

    void sayHello();

    String getHelloString();

    int getId();

    @Override
    String toString();

    @Override
    int hashCode();

    @Override
    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;
    }
}
>_<|| 我手贱!!这个有问题!绝对有问题!!一般般啦,真的很一般般。还不错哦~小表扬一下!GJ!乃就是新世界的神様了,快去拯救世界吧! (No Ratings Yet)
Loading...

Comments are closed.

春菜 对话 相声
双击调戏
双击调戏