版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/inforstack/article/details/54250510
定义所需接口
public interface HomeDataMBean {
public void getDataCount();
}
实现接口
public class HomeData implements HomeDataMBean {
private static ConcurrentHashMap<String, Integer> dataMap = new ConcurrentHashMap<String, Integer>();
public static ConcurrentHashMap<String, Integer> getDataMap() {
return dataMap;
}
@Override
public void getDataCount() {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
//每隔4小时更新Map中的数据
}
}, 1000, 14400000);
}
}
初始化
try {
HomeData homeData = new HomeData();
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
ObjectName name = new ObjectName("com.inforstack.clinicalsense.jmx:type=HomeData");
mbs.registerMBean(homeData, name);
// ObjectName, 调用getDataCount,,调用方法所需参数,参数类型
//我写的是无参方法所以传递两个null
mbs.invoke(name, "getDataCount", null, null);
} catch (Exception e) {
e.printStackTrace();
}
终端命令
jconsole