基于JMX管理站点资源
一.How
1.创建配置文件的类,然后使用spring配置导出成MBean。
(1)创建配置类(基于spring管理的话其实就是POJO,不用实现MBean接口)
public class ParseHandlerMap{private Logger logger =Logger.getLogger(this.getClass());private Map<String, IUrlParseHandler> parseMap;private Map<String, String> loginURLMap;private String defaultLoginPath="http://www.test.com/Clients/login.html";public void setParseMap(Map<String, IUrlParseHandler> parseMap) {this.parseMap = parseMap;}public void setLoginURLMap(Map<String, String> loginURLMap) {this.loginURLMap = loginURLMap;}public void setDefaultLoginPath(String defaultLoginPath) {this.defaultLoginPath = defaultLoginPath;}public String getDefaultLoginPath() {return defaultLoginPath;}public Map<String, IUrlParseHandler> getParseMap() {return parseMap;}public Map<String, String> getLoginURLMap() {return loginURLMap;}//动态添加游戏解析器public String addHandler(String gameId) {if(!ParameterUtil.isPosiIntegerNum(gameId)){return "gameId is not number";}String className="com.test.Game"+gameId+"URLParseHandler";try {Class<?> clazz = Class.forName(className);IUrlParseHandler handler =(IUrlParseHandler)clazz.newInstance();parseMap.put(gameId, handler);return "add handler OK";} catch (ClassNotFoundException e) {logger.error("add handler fail",e);return "class not found";} catch (InstantiationException e) {logger.error("add handler fail",e);return "instance create fail";} catch (IllegalAccessException e) {logger.error("add handler fail",e);return "add handler fail";}}//动态删除解析器public String deleteHandler(String gameId) {parseMap.remove(gameId);return "removed";}public IUrlParseHandler getHandler(String gameId) {return parseMap.get(gameId);}//查看当前的解析器数量public int getHandlerNum(){return parseMap.size();}//添加游戏的登陆页面地址public String addLoginURL(String gameId, String url) {loginURLMap.put(gameId, url);return "OK";}//删除游戏的登陆页面地址public String deleteLoginURL(String gameId) {loginURLMap.remove(gameId);return "removed ok";}//查看游戏的登陆页面地址public String getLoginURL(String gameId) {if(loginURLMap==null || loginURLMap.isEmpty()){return defaultLoginPath;}else{String path = loginURLMap.get(gameId);if(path==null){return defaultLoginPath;}else{return path;}}}}
?
2.spring导出MBean配置
<bean id="exporter" value-ref="parseHandlerMap"/> </map> </property> <property name="registrationBehaviorName" value="REGISTRATION_REPLACE_EXISTING"/> </bean> <bean id="parseHandlerMap" //远程管理端口
export CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote.authenticate=true" //权限认证
export CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote.ssl=false"
(2)tomcat的lib目录增加jmxri.jar,jmxtools.jar包;
(3)创建管理权限文件
由模板文件$JAVA_HOME/jre/lib/management/jmxremote.password.template创建密码文件$JAVA_HOME/jre/lib/management/jmxremote.password;打开其中monitorRole和controlRole的角色并修改密码。monitorRole只有只读权限,controlRole可读可写。二.使用JMX进行管理1.客户端命令运行JConsole,打开java自带的管理工具,输入远程地址端口,以及账号密码
??
2.连接后选MBean标签,找到自定义的MBean的类,即可调用里面的方法更新配置,或者查看当前的配置