亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術(shù)文章
文章詳情頁

使用JMX監(jiān)控Tomcat示例代碼

瀏覽:189日期:2023-06-12 15:17:15
目錄
  • 示例代碼
  • 完整的示例代碼文件

示例代碼

String jmxURL = "service:jmx:rmi:///jndi/rmi://192.168.10.93:8999/jmxrmi";
JMXServiceURL serviceURL = new JMXServiceURL(jmxURL);

Map map = new HashMap();
// 用戶名密碼,在jmxremote.password文件中查看
String[] credentials = new String[] { "monitorRole", "tomcat" };
map.put("jmx.remote.credentials", credentials);
JMXConnector connector = JMXConnectorFactory.connect(serviceURL, map);
MBeanServerConnection mbsc = connector.getMBeanServerConnection();

// 端口最好是動態(tài)取得
ObjectName threadObjName = new ObjectName("Catalina:type=ThreadPool,name=http-8080");
MBeanInfo mbInfo = mbsc.getMBeanInfo(threadObjName);

// tomcat的線程數(shù)對應(yīng)的屬性值
String attrName = "currentThreadCount";
MBeanAttributeInfo[] mbAttributes = mbInfo.getAttributes();
System.out.println("currentThreadCount:" + mbsc.getAttribute(threadObjName, attrName));

完整的示例代碼文件

import java.lang.management.MemoryUsage;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Formatter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.management.MBeanAttributeInfo;
import javax.management.MBeanInfo;
import javax.management.MBeanServerConnection;
import javax.management.ObjectInstance;
import javax.management.ObjectName;
import javax.management.openmbean.CompositeDataSupport;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
/**
 * @author liuyazhuang
 * @date 2016-05-31 
 */
public class JMXTest {

    /**
     * main方法
     * @param args
     */
    public static void main(String[] args) {
try {

    String jmxURL = "service:jmx:rmi:///jndi/rmi://127.0.0.1:8999/jmxrmi";

    JMXServiceURL serviceURL = new JMXServiceURL(jmxURL);

    Map map = new HashMap();
    String[] credentials = new String[] { "monitorRole", "tomcat" };
    map.put("jmx.remote.credentials", credentials);
    JMXConnector connector = JMXConnectorFactory.connect(serviceURL,
    map);
    MBeanServerConnection mbsc = connector.getMBeanServerConnection();

    // 端口最好是動態(tài)取得
    ObjectName threadObjName = new ObjectName(
    "Catalina:type=ThreadPool,name=http-8080");
    MBeanInfo mbInfo = mbsc.getMBeanInfo(threadObjName);

    String attrName = "currentThreadCount";// tomcat的線程數(shù)對應(yīng)的屬性值
    MBeanAttributeInfo[] mbAttributes = mbInfo.getAttributes();
    System.out.println("currentThreadCount:"
    + mbsc.getAttribute(threadObjName, attrName));

    // heap
    for (int j = 0; j < mbsc.getDomains().length; j++) {
System.out.println("###########" + mbsc.getDomains()[j]);
    }
    Set MBeanset = mbsc.queryMBeans(null, null);
    System.out.println("MBeanset.size() : " + MBeanset.size());
    Iterator MBeansetIterator = MBeanset.iterator();
    while (MBeansetIterator.hasNext()) {
ObjectInstance objectInstance = (ObjectInstance) MBeansetIterator
.next();
ObjectName objectName = objectInstance.getObjectName();
String canonicalName = objectName.getCanonicalName();
System.out.println("canonicalName : " + canonicalName);
if (canonicalName
.equals("Catalina:host=localhost,type=Cluster")) {
    // Get details of cluster MBeans
    System.out.println("Cluster MBeans Details:");
    System.out
    .println("=========================================");
    // getMBeansDetails(canonicalName);
    String canonicalKeyPropList = objectName
    .getCanonicalKeyPropertyListString();
}
    }
    // ------------------------- system ----------------------
    ObjectName runtimeObjName = new ObjectName("java.lang:type=Runtime");
    System.out.println("廠商:"
    + (String) mbsc.getAttribute(runtimeObjName, "VmVendor"));
    System.out.println("程序:"
    + (String) mbsc.getAttribute(runtimeObjName, "VmName"));
    System.out.println("版本:"
    + (String) mbsc.getAttribute(runtimeObjName, "VmVersion"));
    Date starttime = new Date((Long) mbsc.getAttribute(runtimeObjName,
    "StartTime"));
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    System.out.println("啟動時間:" + df.format(starttime));

    Long timespan = (Long) mbsc.getAttribute(runtimeObjName, "Uptime");
    System.out.println("連續(xù)工作時間:" + JMXTest.formatTimeSpan(timespan));
    // ------------------------ JVM -------------------------
    // 堆使用率
    ObjectName heapObjName = new ObjectName("java.lang:type=Memory");
    MemoryUsage heapMemoryUsage = MemoryUsage
    .from((CompositeDataSupport) mbsc.getAttribute(heapObjName,
    "HeapMemoryUsage"));
    long maxMemory = heapMemoryUsage.getMax();// 堆最大
    long commitMemory = heapMemoryUsage.getCommitted();// 堆當(dāng)前分配
    long usedMemory = heapMemoryUsage.getUsed();
    System.out.println("heap:" + (double) usedMemory * 100
    / commitMemory + "%");// 堆使用率

    MemoryUsage nonheapMemoryUsage = MemoryUsage
    .from((CompositeDataSupport) mbsc.getAttribute(heapObjName,
    "NonHeapMemoryUsage"));
    long noncommitMemory = nonheapMemoryUsage.getCommitted();
    long nonusedMemory = heapMemoryUsage.getUsed();
    System.out.println("nonheap:" + (double) nonusedMemory * 100
    / noncommitMemory + "%");

    ObjectName permObjName = new ObjectName(
    "java.lang:type=MemoryPool,name=Perm Gen");
    MemoryUsage permGenUsage = MemoryUsage
    .from((CompositeDataSupport) mbsc.getAttribute(permObjName,
    "Usage"));
    long committed = permGenUsage.getCommitted();// 持久堆大小
    long used = heapMemoryUsage.getUsed();//
    System.out.println("perm gen:" + (double) used * 100 / committed
    + "%");// 持久堆使用率

    // -------------------- Session ---------------
    ObjectName managerObjName = new ObjectName(
    "Catalina:type=Manager,*");
    Set<ObjectName> s = mbsc.queryNames(managerObjName, null);
    for (ObjectName obj : s) {
System.out.println("應(yīng)用名:" + obj.getKeyProperty("path"));
ObjectName objname = new ObjectName(obj.getCanonicalName());
System.out.println("最大會話數(shù):"
+ mbsc.getAttribute(objname, "maxActiveSessions"));
System.out.println("會話數(shù):"
+ mbsc.getAttribute(objname, "activeSessions"));
System.out.println("活動會話數(shù):"
+ mbsc.getAttribute(objname, "sessionCounter"));
    }

    // ----------------- Thread Pool ----------------
    ObjectName threadpoolObjName = new ObjectName(
    "Catalina:type=ThreadPool,*");
    Set<ObjectName> s2 = mbsc.queryNames(threadpoolObjName, null);
    for (ObjectName obj : s2) {
System.out.println("端口名:" + obj.getKeyProperty("name"));
ObjectName objname = new ObjectName(obj.getCanonicalName());
System.out.println("最大線程數(shù):"
+ mbsc.getAttribute(objname, "maxThreads"));
System.out.println("當(dāng)前線程數(shù):"
+ mbsc.getAttribute(objname, "currentThreadCount"));
System.out.println("繁忙線程數(shù):"
+ mbsc.getAttribute(objname, "currentThreadsBusy"));
    }

} catch (Exception e) {
    e.printStackTrace();
}
    }

    public static String formatTimeSpan(long span) {
long minseconds = span % 1000;

span = span / 1000;
long seconds = span % 60;

span = span / 60;
long mins = span % 60;

span = span / 60;
long hours = span % 24;

span = span / 24;
long days = span;
return (new Formatter()).format("%1$d天 %2$02d:%3$02d:%4$02d.%5$03d",
days, hours, mins, seconds, minseconds).toString();
    }
}

到此這篇關(guān)于使用JMX監(jiān)控Tomcat示例代碼的文章就介紹到這了,更多相關(guān)JMX監(jiān)控Tomcat內(nèi)容請搜索以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持!

標(biāo)簽: Tomcat
相關(guān)文章:
主站蜘蛛池模板: 亚洲欧美人成人让影院 | 国产乱码精品一区二区三区中 | 东京不太热在线视频免费 | 欧美在线观看一区二区 | 免费看一级欧美毛片视频 | 一级色| 国产人成| 久久综合综合久久 | 永久免费在线视频 | 五月天丁香花婷婷视频网 | 欧美ol丝袜高跟秘书在线观看 | 亚洲精品一区二区ai换脸 | 久久免费国产精品一区二区 | 天天影视欧美综合在线观看 | 亚洲综合精品香蕉久久网 | 国产欧美日韩在线观看 | 人操人摸| 国产毛片在线看 | 久久大香萑太香蕉综合网 | heyzo北条麻妃久久 | 国产免费私拍一区二区三区 | 91视频聊天网 | 色五夜婷婷 | 91插插插插 | 高清女主播一区二区三区 | 久久久久日韩精品免费观看网 | 啪视频| 亚洲精品综合 | 免费大黄网站在线观看 | 草草免费 | 成人免费久久精品国产片久久影院 | 欧美草逼 | 亚洲国产精品午夜电影 | 国产福利在线免费观看 | 日本韩国一级片 | 日本久久网 | 欧美一级色片 | 一本一本大道香蕉久在线精品 | 欧美日韩亚洲国产一区二区综合 | 日本高清视频一区二区 | 精品国产人成亚洲区 |