public void export() {
    try {
      // 创建一个MBeanServer
      MBeanServer mbs = MBeanServerFactory.createMBeanServer(DOMAIN);
      // MBeanServer mbs = MBeanServerFactory.createMBeanServer();//不能在jconsole中使用
      // MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();//可在jconsole中使用
      // 用MBeanServer注册LoginStatsMBean
      // MBeanServer.registerMBean(Object,ObjectName)方法使用的参数有两个:一个是MBean实现的一个实例;另一个是类型ObjectName的一个对象-它用于唯一地标识该MBean
      mbs.registerMBean(new Status(), new ObjectName(MBeanName));

      // 存取该JMX服务的URL:
      JMXServiceURL url =
          new JMXServiceURL("rmi", HOST, JMX_PORT, "/jndi/rmi://" + HOST + ":" + 1099 + "/app");
      // start()和stop()来启动和停止 JMXConnectorServer
      JMXConnectorServer jmxServer =
          JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
      serviceUrl = url.toString();
      // 在RMI上注册
      LocateRegistry.createRegistry(JMX_PORT);
      jmxServer.start();

      // 创建适配器,用于能够通过浏览器访问MBean
      //            HtmlAdaptorServer adapter = new HtmlAdaptorServer();
      //            adapter.setPort(9797);
      //            mbs.registerMBean(adapter, new ObjectName(
      //                    "MyappMBean:name=htmladapter,port=9797"));
      //            adapter.start();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
示例#2
0
public class DeadLockTest {
  private static final String[] protocols = {"rmi", "iiop", "jmxmp"};
  private static final MBeanServer mbs = MBeanServerFactory.createMBeanServer();

  public static void main(String[] args) {
    System.out.println(">>> test on a client notification deadlock.");

    boolean ok = true;
    for (int i = 0; i < protocols.length; i++) {
      try {
        test(protocols[i]);
      } catch (Exception e) {
        System.out.println(">>> Test failed for " + protocols[i]);
        e.printStackTrace(System.out);
      }
    }

    System.out.println(">>> Test passed");
  }

  private static void test(String proto) throws Exception {
    System.out.println(">>> Test for protocol " + proto);

    JMXServiceURL u = null;
    JMXConnectorServer server = null;

    HashMap env = new HashMap(2);
    // server will close a client connection after 1 second
    env.put("jmx.remote.x.server.connection.timeout", "1000");

    // disable the client ping
    env.put("jmx.remote.x.client.connection.check.period", "0");

    try {
      u = new JMXServiceURL(proto, null, 0);
      server = JMXConnectorServerFactory.newJMXConnectorServer(u, env, mbs);
    } catch (MalformedURLException e) {
      System.out.println(">>> Skipping unsupported URL " + proto);
    }

    server.start();

    JMXServiceURL addr = server.getAddress();

    long st = 2000;
    MyListener myListener;

    // a cycle to make sure that we test the blocking problem.
    do {
      JMXConnector client = JMXConnectorFactory.connect(addr, env);
      MBeanServerConnection conn = client.getMBeanServerConnection();
      myListener = new MyListener(conn);
      client.addConnectionNotificationListener(myListener, null, null);

      // wait the server to close the client connection
      Thread.sleep(st);

      // makes the listener to do a remote request via the connection
      // which should be closed by the server.
      conn.getDefaultDomain();

      // allow the listner to have time to work
      Thread.sleep(100);

      // get a closed notif, should no block.
      client.close();
      Thread.sleep(100);

      st += 2000;

    } while (!myListener.isDone());

    server.stop();
  }

  private static class MyListener implements NotificationListener {
    public MyListener(MBeanServerConnection conn) {
      this.conn = conn;
    }

    public void handleNotification(Notification n, Object h) {
      if (n instanceof JMXConnectionNotification) {
        JMXConnectionNotification jcn = (JMXConnectionNotification) n;
        final String type = jcn.getType();
        System.out.println(">>> The listener receives notif with the type:" + type);

        if (JMXConnectionNotification.CLOSED.equals(type)
            || JMXConnectionNotification.FAILED.equals(type)) {

          synchronized (this) {
            done = false;
          }

          try {
            conn.getDefaultDomain();
          } catch (IOException ioe) {
            // Greate !
          }

          synchronized (this) {
            done = true;
          }

          System.out.println(">>> The listener is not blocked!");
        }
      }
    }

    public boolean isDone() {
      synchronized (this) {
        return done;
      }
    }

    private boolean done = false;
    private MBeanServerConnection conn;
  }
}
示例#3
0
 /** Constructs a Server object. Creates a new MBeanServer. */
 public Server() {
   mbs = MBeanServerFactory.createMBeanServer();
 }