如何将监听器添加到JMX进行更新?

时间:2014-10-14 04:04:19

标签: java arraylist listener jmx spring-jmx

考虑以下JMX代码:

public class UserJMXBeanExpose {
private UserJMXBeanImpl userJMXBean;

@Required
public void setUserJMXBean(UserJMXBeanImpl userJMXBean) {
    this.userJMXBean = userJMXBean;
}



public void init() throws MBeanRegistrationException, InstanceAlreadyExistsException, NotCompliantMBeanException, MalformedObjectNameException {

    MBeanServer server = ManagementFactory.getPlatformMBeanServer();
    ObjectName objectName = new ObjectName("MineStar:type=UserJMXBeanExpose,name=Statistics");
    MineStarMBean impl = new MineStarMBean();

    impl.addProperty(new PropertyAdapter("User", "User List", "java.lang.String") {
        public Object getValue() {
            if (userJMXBean == null) return 0;
            return userJMXBean.getUserAttributes();
        }
    });

    server.registerMBean(impl, objectName);
}
}

这段代码:

public class UserJMXBeanImpl implements UserJMXBean {

private  List<User> userList = new ArrayList<>();

public void addUser(User user){
    userList.add(user);

}

public List<User> getUserList() {
      return userList;
}

public void setUserList(List<User> userList) {
    this.userList = userList;
}

public String[] getUserAttributes(){
    for(User user : this.getUserList()){
            return new String[]{user.getUserId(),user.getRoles()};
}
    return null;
}

}

如何向JMX添加侦听器,以便使用getUserAttributes()类新添加的用户更新UserJMXBeanImpl的值?

我需要在每个新用户添加到impl类时更新我的​​jmx信息。或者我们如何添加侦听器,以便每当将新用户添加到impl类时它都可以通知UserJMXBeanExpose公开其数据?

0 个答案:

没有答案