用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

基于jmxtools的JMX服务端构建

2012-11-27 作者: 大漠孤烟举报

[java]代码库

package cn.rtdata.jmx;

import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.rmi.registry.LocateRegistry;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.management.InstanceAlreadyExistsException;
import javax.management.MBeanRegistrationException;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.management.NotCompliantMBeanException;
import javax.management.ObjectName;
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.rtdata.Config;

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

public class JMXService {
    private static Log logger = LogFactory.getLog(JMXService.class);
    private MBeanServer mbs = null;
    private HtmlAdaptorServer htmlAdaptor = null;
    private JMXConnectorServer cs = null;
    private static JMXService jmx = null;

    public boolean init() {
        Config config = Config.getInstance();
        // 取得JMX配置信息
        Properties jmxPro = config.getJmxPrams();
        try {
            mbs = ManagementFactory.getPlatformMBeanServer();
            // htmlAdaptor
            htmlAdaptor = new HtmlAdaptorServer();
            final ObjectName htmlAdaptorON = new ObjectName(
                    "com.example.mbeans:name=HtmlAdaptor");
            int port = Integer.parseInt(jmxPro.getProperty("htmlAdaptorPort",
                    "9876"));
            htmlAdaptor.setPort(port);
            // htmlAdaptor验证信息
            AuthInfo login = new AuthInfo();
            login.setLogin(jmxPro.getProperty("htmlUser"));
            login.setPassword(jmxPro.getProperty("htmlPasswd"));
            // 添加用户验证
            htmlAdaptor.addUserAuthenticationInfo(login);
            // 注册该模块
            mbs.registerMBean(htmlAdaptor, htmlAdaptorON);
            System.out.println("注册应用程序启动入口");
            Launcher_jmxMBean launcher = new Launcher_jmx();
            mbs.registerMBean(launcher, new ObjectName(
                    "cn.rtdata:launcher=ILauncherMBean"));
            htmlAdaptor.start();
            System.out.println("启动JMX的HtmlAdaptor,端口号:" + port);
            int jmxServiceURIPort = Integer.parseInt(jmxPro.getProperty(
                    "jmxServiceURIPort", "9999"));
            // if(LocateRegistry.getRegistry(jmxServiceURIPort)==null)s
            LocateRegistry.createRegistry(jmxServiceURIPort);
            String jmxServiceURI_ = jmxPro.getProperty("jmxServiceURI");
            String jmxServiceURI = jmxServiceURI_.substring(1,
                    jmxServiceURI_.length() - 1);
            // 密码文件
            /**
             * 内容为键值对 admin 111111 user 111111
             */
            String userFile = "config//jmx//passwd.txt";
            Map<String, String> env = new HashMap<String, String>();
            env.put("jmx.remote.x.password.file", userFile);

            JMXServiceURL url = new JMXServiceURL(jmxServiceURI);
            cs = JMXConnectorServerFactory.newJMXConnectorServer(url, env,
                    mbs);

            cs.start();

        } catch (MalformedObjectNameException e) {
            logger.error("JMX服务启动异常", e);
            e.printStackTrace();
            return false;
        } catch (InstanceAlreadyExistsException e) {
            logger.error("JMX服务启动异常", e);
            e.printStackTrace();
            return false;
        } catch (MBeanRegistrationException e) {
            logger.error("JMX服务启动异常", e);
            e.printStackTrace();
            return false;
        } catch (NotCompliantMBeanException e) {
            logger.error("JMX服务启动异常", e);
            e.printStackTrace();
            return false;
        } catch (NullPointerException e) {
            logger.error("JMX服务启动异常", e);
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            logger.error("JMX服务启动异常", e);
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public static JMXService getInstance() {
        if (jmx == null) {
            jmx = new JMXService();
        }
        return jmx;
    }

    private JMXService() {
    }

    public void stopService() {
        if (htmlAdaptor != null)
            htmlAdaptor.stop();
        if (cs != null) {
            try {
                cs.stop();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...