JAVA JMX协议监控

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

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

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

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

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 "终止";
    }
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏张善友的专栏

Mix 10 上的asp.net mvc 2的相关Session

Beyond File | New Company: From Cheesy Sample to Social Platform Scott Hansel...

2787
来自专栏飞扬的花生

jsencrypt参数前端加密c#解密

      写程序时一般是通过form表单或者ajax方式将参数提交到服务器进行验证,如何防止提交的请求不被抓包后串改,虽然无法说绝对安全却给非法提交提高了难度...

4269
来自专栏闻道于事

js登录滑动验证,不滑动无法登陆

js的判断这里是根据滑块的位置进行判断,应该是用一个flag判断 <%@ page language="java" contentType="text/html...

8908
来自专栏Ceph对象存储方案

Luminous版本PG 分布调优

Luminous版本开始新增的balancer模块在PG分布优化方面效果非常明显,操作也非常简便,强烈推荐各位在集群上线之前进行这一操作,能够极大的提升整个集群...

3705
来自专栏芋道源码1024

熔断器 Hystrix 源码解析 —— 断路器 HystrixCircuitBreaker

本文主要基于 Hystrix 1.5.X 版本 1. 概述 2. HystrixCircuitBreaker 3. HystrixCircuitBreaker....

5847
来自专栏张善友的专栏

LINQ via C# 系列文章

LINQ via C# Recently I am giving a series of talk on LINQ. the name “LINQ via C...

3025
来自专栏一个爱瞎折腾的程序猿

sqlserver使用存储过程跟踪SQL

USE [master] GO /****** Object: StoredProcedure [dbo].[sp_perfworkload_trace_s...

3020
来自专栏我和未来有约会

Kit 3D 更新

Kit3D is a 3D graphics engine written for Microsoft Silverlight. Kit3D was inita...

2946
来自专栏pangguoming

Spring Boot集成JasperReports生成PDF文档

由于工作需要,要实现后端根据模板动态填充数据生成PDF文档,通过技术选型,使用Ireport5.6来设计模板,结合JasperReports5.6工具库来调用渲...

1.4K7
来自专栏大内老A

The .NET of Tomorrow

Ed Charbeneau(http://developer.telerik.com/featured/the-net-of-tomorrow/) Exciti...

39410

扫码关注云+社区