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

}

  

  相关连接

  http://www.iteye.com/topic/1117196

      https://www.ibm.com/developerworks/cn/java/j-rtm1/  

    http://1985wanggang.blog.163.com/blog/static/7763833201258112117110/

      http://www.blogjava.net/japper/archive/2012/09/05/387092.html

  http://www.aichengxu.com/xitong/944435.htm

      http://www.programcreek.com/java-api-examples/index.php?class=javax.management.MBeanServerConnection&method=queryMBeans

  http://www.voidcn.com/blog/xuechongyang/article/p-1837832.html

 

posted @ 2017-03-06 11:18  杉枫  阅读(843)  评论(0编辑  收藏  举报