Git Product home page Git Product logo

jmx-annotations's Introduction

Introduction

Create and register JMX MBeans by adding some simple annotations to your classes. The project was inspired by the JSR-255 (JMX 2.0) specification, 
which provides annotations to create MBeans, but it is currently inactive. However, 
it goes further by allowing you to automatically or programmatically create and register the MBeans. Currently, it only works with CDI (JEE6) as a portable extension,
but I plan to support Seam as well as Spring.

Automatic creation and registration

An MBean is created and registered automatically when your class is instantiated.

Programatic creation and registration

If you don't want to register your MBean automatically, you can deactivate the auto register feature and the use the API to programatically create and register an MBean within the MBeanServer.
Status

Example

import org.wstone.jmx.*;

@MBean(description = "访问计数类")
public class VisitorCounter {

  @ManagedAttribute(description = "访问次数", writable = false, readable = true)
  private int visits;

  @ManagedOperation(description = "重置访问次数", impact = Impact.WRITE)
  public void resetVisits() {
    this.visits = 0;
  }

  @ManagedOperation(description = "带参数的重置访问次数", impact = Impact.WRITE)
  public void resetVisits(@Description("次数") int visits) {
    this.visits = visits;
  }

  @ManagedOperation(description = "累加访问次数", impact = Impact.READ_WRITE)
  public int incVisits() {
    return ++this.visits;
  }

  @ManagedOperation(description = "设置访问次数", impact = Impact.WRITE)
  public void setVisits(@Description("次数") int visits) {
    this.visits = visits;
  }

  public int getVisits() {
    return visits;
  }

}

You can also use the API to programmatically create and register your MBeans:

import javax.management.DynamicMBean;
import javax.management.MBeanServer;
import javax.management.ObjectName;

import org.wstone.jmx.MBeanFactory;
import org.wstone.jmx.std.StandardMBeanFactory;
import org.wstone.jmx.util.MBeanServerLocator;

public class TestMain {
  public static void main(String[] args) {
    try {
      MBeanFactory mBeanFactory = new StandardMBeanFactory();

      // create the MBean
      VisitorCounter visitorCounterA = new VisitorCounter();
      DynamicMBean mBean = mBeanFactory.createMBean(visitorCounterA);

      // find MBeanServer
      MBeanServer ms = MBeanServerLocator.instance().getmBeanServer();

      // register the MBean
      ms.registerMBean(mBean, new ObjectName("org.test:type=VisitorCounter"));
      ms.registerMBean(mBean, null);  //由mBean自动提供ObjectName
      
      System.out.println("started MBeanServer...");

      for (;;) {
        try {
          java.util.concurrent.TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException ex) {
          Thread.currentThread().interrupt();
          System.exit(0);
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

jmx-annotations's People

Contributors

wjw465150 avatar

Watchers

James Cloos avatar 踏空 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.