前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >JAVA JMX协议监控

JAVA JMX协议监控

作者头像
杉枫
发布2018-01-03 11:12:09
1.6K0
发布2018-01-03 11:12:09
举报
文章被收录于专栏:互联网研发闲思录

  JMX协议监控,可通过JMX协议远程监控,实时监控线上jvm情况,并通过平台管理界面进行

展示,可以通过监控实时获得线上服务器运行情况。

  可以监控内存、实时线程、共享内存等各种信息。

  获取实时线程信息并显示:

代码语言:txt
复制
import javax.management.MBeanServerConnection;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

 /**
 * Created by liubaofeng on 2017/3/10.
 * 根据jmx配置
 * 将线程信息返回
 */
public class JvmRuntimeServiceImpl implements JvmRuntimeService {
    protected static final Logger logger = LoggerFactory.getLogger("file_logger");
    private Map<String,MBeanServerConnection> map = new HashMap<>();
    

        @Override
    public List<JvmRuntimeModel> threadRuntime(String url)  {
        if(logger.isDebugEnabled())
            logger.debug("url:"+url);
        String jmxURL = "service:jmx:rmi:///jndi/rmi://"+url+"/jmxrmi";
        List<JvmRuntimeModel> resultList = null;
        if(!map.containsKey(jmxURL)) {
            MBeanServerConnection mBeanServerConnection = null;
            try {
                mBeanServerConnection = connection(jmxURL);
            } catch (IOException e) {
                if (logger.isErrorEnabled())
                    logger.error(e.getMessage(), e);
            }
            map.put(jmxURL,mBeanServerConnection);
            try {
                ThreadInfo[] threadInfos = thread(mBeanServerConnection);
                resultList = getJvmRuntimeModelList(threadInfos);
            } catch (IOException e) {
                if (logger.isErrorEnabled())
                    logger.error(e.getMessage(), e);
            }
        }else{
            MBeanServerConnection mBeanServerConnection = map.get(jmxURL);
            try {
                ThreadInfo[] threadInfos = thread(mBeanServerConnection);
                resultList = getJvmRuntimeModelList(threadInfos);
            } catch (IOException e) {
                if (logger.isErrorEnabled())
                    logger.error(e.getMessage(), e);
            }
        }
        return resultList;
    }
    

        /**
     * 建立jmx连接
     * @param jmxURL
     * @return
     * @throws IOException
     */
    private MBeanServerConnection connection(String jmxURL)throws IOException{
        JMXServiceURL serviceURL = new JMXServiceURL(jmxURL);
        JMXConnector connector = JMXConnectorFactory.connect(serviceURL, null);
        MBeanServerConnection mBeanServerConnection = connector.getMBeanServerConnection();
        return mBeanServerConnection;
    }
            

    /**
     * 取threadinfo信息.
     * @param mBeanServerConnection
     * @throws IOException
     */
    private ThreadInfo[] thread(MBeanServerConnection mBeanServerConnection) throws IOException {
        ThreadMXBean threadMXBean = ManagementFactory
                .newPlatformMXBeanProxy(mBeanServerConnection,
                        ManagementFactory.THREAD_MXBEAN_NAME,
                        ThreadMXBean.class);
        long[] ids = threadMXBean.getAllThreadIds();
        ThreadInfo[] threadInfos = threadMXBean.getThreadInfo(ids);
        return threadInfos;
    }
            

                /**
     * 取jvm运行时信息.
     * @param threadInfos
     * @return
     */
    private List<JvmRuntimeModel> getJvmRuntimeModelList(ThreadInfo[] threadInfos){
        if(threadInfos!=null)
            return null;
        
        List<JvmRuntimeModel> list = new ArrayList<JvmRuntimeModel>(threadInfos.length);
        for(int i=0;i<threadInfos.length;i++){
            ThreadInfo threadInfo = threadInfos[i];
            String threadName = threadInfo.getThreadName();
            Thread.State threadState = threadInfo.getThreadState();
            JvmRuntimeModel jvmRuntimeModel = new JvmRuntimeModel();
            jvmRuntimeModel.id = i;
            jvmRuntimeModel.name = threadName;
            jvmRuntimeModel.state = getState(threadState);
            list.add(jvmRuntimeModel);
        }
    

            return list;
    }
    

        /**
     * 获得状态.
     * @param state
     * @return
     */
    private String getState(Thread.State state){
        if(state.equals(Thread.State.NEW))
            return "新建";
        else if(state.equals(Thread.State.RUNNABLE))
            return "执行中";
        else if(state.equals(Thread.State.BLOCKED))
            return "阻塞";
        else if(state.equals(Thread.State.WAITING))
            return "等待";
        else if(state.equals(Thread.State.TIMED_WAITING))
            return "定时等待";
        else
            return "终止";
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-03-06 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档