作者:书友60061807 | 来源:互联网 | 2023-09-18 18:49
具体代码1packagecom.spring.earlywarning.util;2importjava.io.IOException;3importjava.net.Malfor
具体代码
1 package com.spring.earlywarning.util;
2 import java.io.IOException;
3 import java.net.MalformedURLException;
4 import java.util.Hashtable;
5
6 import javax.management.MBeanServerConnection;
7 import javax.management.MalformedObjectNameException;
8 import javax.management.ObjectName;
9 import javax.management.remote.JMXConnector;
10 import javax.management.remote.JMXConnectorFactory;
11 import javax.management.remote.JMXServiceURL;
12 import javax.naming.Context;
13 /**
14 * @author liuwenlong
15 * @create 2022-03-15 21:00:59
16 */
17 public class PrintThread {
18 private static MBeanServerConnection connection;
19 private static JMXConnector connector;
20 private static final ObjectName service;
21
22
23 static {
24 try {
25 service = new ObjectName("com.bea:Name=RuntimeService,Type=weblogic.management.mbeanservers.runtime.RuntimeServiceMBean");
26 }catch (MalformedObjectNameException e) {
27 throw new AssertionError(e.getMessage());
28 }
29 }
30
31 public static void initConnection(String hostname, String portString,
32 String username, String password) throws IOException,
33 MalformedURLException {
34 String protocol = "t3";
35 Integer portInteger = Integer.valueOf(portString);
36 int port = portInteger.intValue();
37 String jndiroot = "/jndi/";
38 String mserver = "weblogic.management.mbeanservers.runtime";
39 JMXServiceURL serviceURL = new JMXServiceURL(protocol, hostname,
40 port, jndiroot + mserver);
41 Hashtable h = new Hashtable();
42 h.put(Context.SECURITY_PRINCIPAL, username);
43 h.put(Context.SECURITY_CREDENTIALS, password);
44 h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES,
45 "weblogic.management.remote");
46 cOnnector= JMXConnectorFactory.connect(serviceURL, h);
47 cOnnection= connector.getMBeanServerConnection();
48 }
49
50 public static ObjectName getServerRuntimes() throws Exception {
51 return (ObjectName)connection.getAttribute(service,
52 "ServerRuntime");
53 }
54
55 public void printInfo() throws Exception{
56 ObjectName objThreadPool = null;
57 ObjectName serverRT = getServerRuntimes();
58
59 objThreadPool = (ObjectName) connection.getAttribute(serverRT, "ThreadPoolRuntime");
60 double throughput = Double.parseDouble(String.valueOf(connection.getAttribute(
61 objThreadPool,"Throughput")));
62 System.out.println("throughput:" + throughput);
63
64 int executeThreadTotalCount = Integer.parseInt(String.valueOf(connection.getAttribute(objThreadPool, "ExecuteThreadTotalCount")));
65 System.out.println("executeThreadTotalCount:" + executeThreadTotalCount);
66
67
68 int executeThreadIdleCount = Integer.parseInt(String.valueOf(connection.getAttribute(objThreadPool, "ExecuteThreadIdleCount")));
69 System.out.println("executeThreadIdleCount:" + executeThreadIdleCount);
70
71 int StandbyThreadCount = Integer.parseInt(String.valueOf(connection.getAttribute(objThreadPool, "StandbyThreadCount")));
72 System.out.println("StandbyThreadCount:" + StandbyThreadCount);
73
74
75 long timestamp = System.currentTimeMillis() / 1000;
76 String metricJson = "";
77 String jsOnFormat= "{\"name\": \"weblogic_threadpool_metric_demo\", " +
78 "\"command\":\"weblogic_threadpool_metric\"," +
79 "\"type\": \"metric\"," +
80 "\"handlers\": [\"influxdb\"],"+
81 "\"output\": \"%s %.5f %d\\n%s %d %d\\n%s %d %d\\n\"," +
82 "\"status\": 0}";
83 metricJson = String.format(jsonFormat,
84 ".weblogic.threadpool.throughput", (double)throughput, timestamp,
85 ".weblogic.threadpool.executeThreadTotalCount", (int)executeThreadTotalCount, timestamp,
86 ".weblogic.threadpool.executeThreadIdleCount", (int)executeThreadIdleCount, timestamp);
87 System.out.println("metricJson==" + metricJson);
88
89
90
91 }
92
93 /**
94 * @param args
95 * @throws Exception
96 */
97 public static void main(String args[]) throws Exception {
98 String hostname = "127.0.0.1";
99 String portString = "7101";
100 String username = "weblogic";
101 String password = "weblogic123";
102 PrintThread s = new PrintThread();
103 initConnection(hostname, portString, username, password);
104 s.printInfo();
105 connector.close();
106 }
107 }
运行结果
报错情况一、
1 java.net.MalformedURLException: Unsupported protocol: t3
2 at javax.management.remote.JMXConnectorFactory.newJMXConnector(JMXConnectorFactory.java:357)
3 at javax.management.remote.JMXConnectorFactory.connect(JMXConnectorFactory.java:267)
4 at com.whm.test.weblogic.WeblogicProbeJava.initConnection(WeblogicProbeJava.java:63)
5 at com.whm.test.weblogic.WeblogicProbeJava.main(WeblogicProbeJava.java:418)
报错情况二、
1 Exception in thread "main" java.lang.NoClassDefFoundError: weblogic/rmi/extensions/DisconnectListener
2 at java.lang.ClassLoader.defineClass1(Native Method)
3 at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
4 at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
5 at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
6 at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
7 at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
8 at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
9 at java.security.AccessController.doPrivileged(Native Method)
10 at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
11 at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
12 at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
13 at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
14 at java.lang.Class.forName0(Native Method)
15 at java.lang.Class.forName(Class.java:274)
以上两种报错,解决方法:
引入两个jar包即可:wlclient.jar,wljmxclient.jar(这两个包在我网盘里。链接:链接:https://pan.baidu.com/s/1nrdcVHIyucq5U4TRxwCZQw 提取码:olhb --来自百度网盘超级会员V3的分享)
找到jar包存放的位置,然后导入即可
原创文章,转载请说明出处,谢谢合作