java监控weblogic
代码
1 package com.spring.earlywarning.util; 2 3 import java.io.IOException; 4 import java.net.MalformedURLException; 5 import java.util.Hashtable; 6 7 import javax.management.MBeanServerConnection; 8 import javax.management.MalformedObjectNameException; 9 import javax.management.ObjectName; 10 import javax.management.remote.JMXConnector; 11 import javax.management.remote.JMXConnectorFactory; 12 import javax.management.remote.JMXServiceURL; 13 import javax.naming.Context; 14 15 /** 16 * @author liuwenlong 17 * @create 2022-03-15 21:00:59 18 */ 19 @SuppressWarnings("all") 20 public class PrintThread { 21 private static MBeanServerConnection connection; 22 private static JMXConnector connector; 23 private static final ObjectName service; 24 private static MBeanServerConnection ThreadPoolRuntimeconn; 25 26 27 static { 28 try { 29 service = new ObjectName("com.bea:Name=RuntimeService,Type=weblogic.management.mbeanservers.runtime.RuntimeServiceMBean"); 30 } catch (MalformedObjectNameException e) { 31 throw new AssertionError(e.getMessage()); 32 } 33 } 34 35 /** 36 * * 打印一组 ServerRuntimeMBeans。 * 此 MBean 是运行时 MBean 层次的根, * 37 * 此域中的每个服务器承载自己的实例。 38 */ 39 // public static ObjectName[] getServerRuntimes() throws Exception { 40 // return (ObjectName[]) connection.getAttribute(service, "ServerRuntimes"); 41 // } 42 43 public static ObjectName getThreadPoolRuntimeconn(ObjectName ObjectName) throws Exception { 44 return (javax.management.ObjectName) connection.getAttribute(ObjectName, "ThreadPoolRuntime"); 45 } 46 47 public static void initConnection(String hostname, String portString, String username, String password) throws IOException, 48 MalformedURLException { 49 String protocol = "t3"; 50 Integer portInteger = Integer.valueOf(portString); 51 int port = portInteger.intValue(); 52 String jndiroot = "/jndi/"; 53 String mserver = "weblogic.management.mbeanservers.runtime"; 54 JMXServiceURL serviceURL = new JMXServiceURL(protocol, hostname, 55 port, jndiroot + mserver); 56 Hashtable h = new Hashtable(); 57 h.put(Context.SECURITY_PRINCIPAL, username); 58 h.put(Context.SECURITY_CREDENTIALS, password); 59 h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES, 60 "weblogic.management.remote"); 61 connector = JMXConnectorFactory.connect(serviceURL, h); 62 connection = connector.getMBeanServerConnection(); 63 } 64 65 public static ObjectName getServerRuntimes() throws Exception { 66 return (ObjectName) connection.getAttribute(service, "ServerRuntime"); 67 } 68 69 public void printInfo() throws Exception { 70 ObjectName objThreadPool = null; 71 ObjectName serverRT = getServerRuntimes(); 72 73 String name = (String) connection.getAttribute(serverRT, "Name"); //服务器名 74 System.out.println("服务器名:" + name); 75 76 objThreadPool = (ObjectName) connection.getAttribute(serverRT, "ThreadPoolRuntime"); 77 double throughput = Double.parseDouble(String.valueOf(connection.getAttribute(objThreadPool, "Throughput"))); 78 System.out.println("吞吐量:" + throughput); 79 80 ObjectName ThreadPoolRuntimeconn = getThreadPoolRuntimeconn(serverRT); 81 int attribute = (int) connection.getAttribute(ThreadPoolRuntimeconn, "HoggingThreadCount"); //线程数 82 System.out.println("线程数:" + attribute); 83 84 int ListenPort = (int) connection.getAttribute(serverRT, "ListenPort");//监听端口 85 System.out.println("监听端口:" + ListenPort); 86 87 int executeThreadTotalCount = Integer.parseInt(String.valueOf(connection.getAttribute(objThreadPool, "ExecuteThreadTotalCount"))); 88 System.out.println("执行线程总数:" + executeThreadTotalCount); 89 90 91 int executeThreadIdleCount = Integer.parseInt(String.valueOf(connection.getAttribute(objThreadPool, "ExecuteThreadIdleCount"))); 92 System.out.println("执行线程空闲计数:" + executeThreadIdleCount); 93 94 int StandbyThreadCount = Integer.parseInt(String.valueOf(connection.getAttribute(objThreadPool, "StandbyThreadCount"))); 95 System.out.println("备用线程数:" + StandbyThreadCount); 96 97 98 long timestamp = System.currentTimeMillis() / 1000; 99 String metricJson = ""; 100 String jsonFormat = "{\"name\": \"weblogic_threadpool_metric_demo\", " + 101 "\"command\":\"weblogic_threadpool_metric\"," + 102 "\"type\": \"metric\"," + 103 "\"handlers\": [\"influxdb\"]," + 104 "\"output\": \"%s %.5f %d\\n%s %d %d\\n%s %d %d\\n\"," + 105 "\"status\": 0}"; 106 metricJson = String.format(jsonFormat, 107 ".weblogic.threadpool.throughput", (double) throughput, timestamp, 108 ".weblogic.threadpool.executeThreadTotalCount", (int) executeThreadTotalCount, timestamp, 109 ".weblogic.threadpool.executeThreadIdleCount", (int) executeThreadIdleCount, timestamp); 110 System.out.println("metricJson==" + metricJson); 111 112 113 } 114 115 /** 116 * 117 * @param args 118 * @throws Exception 119 */ 120 public static void main(String args[]) throws Exception { 121 String hostname = "localhost"; 122 String portString = "7003"; 123 String username = "weblogic"; 124 String password = "123456"; 125 PrintThread s = new PrintThread(); 126 initConnection(hostname, portString, username, password); 127 s.printInfo(); 128 connector.close(); 129 } 130 }
运行结果
报错情况一、
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包存放的位置,然后导入即可
原创文章,转载请说明出处,谢谢合作
【推荐】中国电信天翼云云端翼购节,2核2G云服务器一口价38元/年
【推荐】博客园携手 AI 驱动开发工具商 Chat2DB 推出联合终身会员
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 时间轮在 Netty , Kafka 中的设计与实现
· MySQL 优化利器 SHOW PROFILE 的实现原理
· 在.NET Core中使用异步多线程高效率的处理大量数据
· 聊一聊 C#前台线程 如何阻塞程序退出
· 几种数据库优化技巧
· 跟着 8.6k Star 的开源数据库,搞 RAG!
· .NET 9 中的 多级缓存 HybridCache
· 夜莺 v8 第一个版本来了,开始做有意思的功能了
· .NET 9 增强 OpenAPI 规范,不再内置swagger
· 推荐一个C#轻量级矢量图形库