JMX -- JDMK实现HTML页面触发后台方法

JMX是一种JAVA的正式规范,它主要目的是让程序有被管理的功能,那么怎么理解所谓的“被管理”呢?试想你开发了一个软件(如WEB网站),它是在24小时不间断运行的,那么你可能会想要“监控”这个软件的运行情况,比如收到了多少数据,有多少人登录等等。或者你又想“配置”这个软件,比如现在访问人数比较多,你想把数据连接池设置得大一些。

  当然,你也许会专门为这些管理来开发软件,但如果你借助JMX,则会发现创建这样的管理程序是如此简单。因为你无需为管理程序来开发界面,已经有通用的JMX管理软件,如MC4J,或者是用一般都附带提供的HTML网页来管理,你要做的仅仅是将自己要被管理和监控类的按照JMX规范修改一下即可。

  中间件软件WebLogic的管理页面就是基于JMX开发的,而JBoss则整个系统都基于JMX构架。

1、下载JAR包,jdmkrt.jar
2、核心JDMK代码

package com.redhorse.jmx;

import java.lang.management.ManagementFactory;
import java.util.Map;
import java.util.Set;

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

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Controller;

import com.sun.jdmk.comm.AuthInfo;
import com.sun.jdmk.comm.HtmlAdaptorServer;

@Controller
public abstract class JmxAgent implements InitializingBean, DisposableBean {

    private final Logger log = Logger.getLogger(getClass());

    private HtmlAdaptorServer adapter = null;

    public abstract Integer getJmxHtmlAdapterPort();

    public abstract String getJmxHtmlAdapterName();

    public abstract String getUserName();

    public abstract String getPassword();

    public abstract Map<String, Object> getMBean();

    private void create() throws Exception {
        MBeanServer server = ManagementFactory.getPlatformMBeanServer();
        Map<String, Object> mbean = getMBean();
        if (null != mbean && !mbean.isEmpty()) {
            Set<Map.Entry<String, Object>> set = mbean.entrySet();
            for (Map.Entry<String, Object> en : set) {
                ObjectName constantsName = new ObjectName(en.getKey() + ":name=" + en.getKey());
                server.registerMBean(en.getValue(), constantsName);
            }
        }
        ObjectName adapterName = new ObjectName(
                getJmxHtmlAdapterName() + ":name=htmladapter,port=" + getJmxHtmlAdapterPort());
        HtmlAdaptorServer adapter = new HtmlAdaptorServer();
        adapter.setPort(getJmxHtmlAdapterPort());
        AuthInfo login = new AuthInfo();
        login.setLogin(StringUtils.isBlank(getUserName()) ? "" : getUserName());
        login.setPassword(StringUtils.isBlank(getPassword()) ? "" : getPassword());
        adapter.addUserAuthenticationInfo(login);
        server.registerMBean(adapter, adapterName);
        adapter.start();
        this.adapter = adapter;
        log.info("Starting JMX Management in port " + getJmxHtmlAdapterPort() + ".");
    }

    public void afterPropertiesSet() throws Exception {
        create();
    }

    public void destroy() throws Exception {
        if (adapter != null) {
            adapter.stop();
        }
        log.info("Stoped JMX Management in port " + getJmxHtmlAdapterPort() + ".");
    }

}

核心代码2,配置HTML访问端口、用户、密码

package com.redhorse.jmx;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.redhorse.service.RuleService;

@Controller
public class CoreJmxAgent extends JmxAgent {

    private final Logger logger = Logger.getLogger(getClass());

    @Autowired
    private JobMBean jobMBean;

    public JobMBean getJobMBean() {
        return jobMBean;
    }

    public void setJobMBean(JobMBean jobMBean) {
        this.jobMBean = jobMBean;
    }

    @Override
    public String getJmxHtmlAdapterName() {
        return "com.redhorse.jmx";
    }

    @Override
    public Map<String, Object> getMBean() {
        Map<String, Object> map = new HashMap<String, Object>();

        map.put("com.redhorse.jmx.dojob", getJobMBean());

        return map;
    }

    @Override
    public String getUserName() {
        return getRuleByName("jmx.username");
    }

    @Override
    public String getPassword() {
        return getRuleByName("jmx.password");
    }

    @Override
    public Integer getJmxHtmlAdapterPort() {
        return Integer.parseInt(getRuleByName("jmx.port"));
    }

    /** * 读取properties资源文件 * * @author: Gao Peng * @date: 2015年11月27日 下午4:45:29 * @param: @param * name * @param: @return * @return: String */
    private String getRuleByName(String name) {
        String rule = "";
        Properties prop = new Properties();
        InputStream in = RuleService.class.getResourceAsStream("/rule.properties");
        try {
            prop.load(in);
            rule = prop.getProperty(name);
        } catch (Exception e) {
            logger.error("警告!!!警告!!!【JMX初始化】,读取到properties配置文件出错!!!!!!", e);
            return "";
        }
        return rule;
    }

}

3、业务代码

package com.redhorse.jmx;

public interface JobMBean {
    public void test();

    public void updateMobileRule();
}
package com.redhorse.jmx;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.redhorse.service.RuleService;

@Controller
public class Job implements JobMBean {
    private final static Logger log = Logger.getLogger(Job.class.getName());

    @Autowired
    private RuleService ruleService;

    @Override
    public void test() {
        log.info("【JMX任务执行】 test Success");
    }

    @Override
    public void updateMobileRule() {
        log.info("【JMX任务执行】updateMobileRule begin ...... ");
        ruleService.putMobileToEncache();
        log.info("【JMX任务执行】 updateMobileRule end ");
    }

}

配置文件


jmx.username = daxiang
jmx.password = daxiang!@#
jmx.port = 18887

目录结构图
JMX -- JDMK实现HTML页面触发后台方法_第1张图片

你可能感兴趣的:(html,jmx)