Weblogic监控jndi泄露连接的方法
- 格式:doc
- 大小:17.50 KB
- 文档页数:2
JavaJNDI案例详解JNDI的理解JNDI是 Java 命名与⽂件夹接⼝(Java Naming and Directory Interface),在J2EE规范中是重要的规范之中的⼀个,不少专家觉得,没有透彻理解JNDI的意义和作⽤,就没有真正掌握J2EE特别是EJB的知识。
那么,JNDI究竟起什么作⽤?//带着问题看⽂章是最有效的要了解JNDI的作⽤,我们能够从“假设不⽤JNDI我们如何做?⽤了JNDI后我们⼜将如何做?”这个问题来探讨。
没有JNDI的做法:程序猿开发时,知道要开发訪问MySQL数据库的应⽤,于是将⼀个对 MySQL JDBC 驱动程序类的引⽤进⾏了编码,并通过使⽤适当的 JDBC URL 连接到数据库。
就像以下代码这样:Connection conn=null;try {Class.forName("com.mysql.jdbc.Driver",true, Thread.currentThread().getContextClassLoader());conn=DriverManager.getConnection("jdbc:mysql://MyDBServer?user=qingfeng&password=mingyue");......conn.close();} catch(Exception e) {e.printStackTrace();} finally {if(conn!=null) {try {conn.close();} catch(SQLException e) {}}}这是传统的做法,也是曾经⾮Java程序猿(如Delphi、VB等)常见的做法。
这种做法⼀般在⼩规模的开发过程中不会产⽣问题,仅仅要程序猿熟悉Java语⾔、了解JDBC技术和MySQL,能够⾮常快开发出对应的应⽤程序。
没有JNDI的做法存在的问题:1. 数据库server名称MyDBServer 、username和⼝令都可能须要改变,由此引发JDBC URL须要改动;2. 数据库可能改⽤别的产品,如改⽤DB2或者Oracle,引发JDBC驱动程序包和类名须要改动;3. 随着实际使⽤终端的添加,原配置的连接池參数可能须要调整;4. ......解决的⽅法:程序猿应该不须要关⼼“详细的数据库后台是什么?JDBC驱动程序是什么?JDBC URL格式是什么?訪问数据库的username 和⼝令是什么?”等等这些问题。
jndi lookup方法使用在 Java 中,`JNDI`(Java Naming and Directory Interface)是一种用于访问命名和目录服务的 API。
它允许 Java 应用程序通过统一的接口来查找和访问各种不同类型的命名和目录服务,例如 LDAP、DNS、RMI 等。
使用`JNDI`进行查找的步骤通常如下:1. 初始化`JNDI`上下文:通过创建一个`InitialContext`对象来初始化`JNDI`上下文。
这将建立与命名服务的连接。
2. 查找对象:使用`lookup()`方法在初始化的上下文中查找指定的对象。
该方法接受一个名称作为参数,并返回找到的对象。
3. 绑定对象:如果需要将对象绑定到命名服务中,可以使用`bind()`方法。
4. 解除绑定对象:使用`unbind()`方法来解除已绑定的对象。
5. 关闭`JNDI`上下文:在使用完`JNDI`后,应该关闭上下文以释放资源。
下面是一个简单的示例代码,演示了如何使用`JNDI`来查找一个对象:```javaimport javax.naming.Context;import javax.naming.InitialContext;import javax.naming.NamingException;public class JNDIExample {public static void main(String[] args) {try {// 初始化 JNDI 上下文Context context = new InitialContext();// 查找对象Object obj = context.lookup("example");// 打印找到的对象System.out.println("Found object: " + obj);} catch (NamingException e) {e.printStackTrace();}}}```在上面的示例中,我们使用`InitialContext`类来初始化`JNDI`上下文。
帆软报表FineReport中数据连接之Weblogic配置JNDI连接1.制作报表的原理在帆软报表FineReport设计器中先用JDBC连接到数据库,建立数据库连接,然后用SQL或者其他方法创建数据集,使用数据集制作报表,然后把建立的数据库连接从JDBC 连接改成JNDI连接,然后配置好服务器中的JNDI连接,然后按照我们下面章节所说的部署weblogic服务器,这样就可以通过WEB浏览器浏览到使用JNDI连接的报表了。
WEBLOGIC的JNDI资源应用与WEBSPHERE的JNDI资源应用很类似,所以本章就简略地进行讲解,具体的步骤可以参照上一章WEBSPHERE的JNDI资源应用。
1.1制作使用JNDI连接的报表首先使用jdbc方式连接数据库orcl10g,然后使用orcl10g数据库中的一个表制作一个模板jndi.cpt保存可以预览,如下图:然后,回到定义数据库连接,新建一个JNDI方式的数据连接orcl10g,将jndi的名字命名为oracle,删掉前面建的连接方式JDBC的orcl10g数据连接,确定保存。
如下图:保存模板,此时的模板jndi.cpt是无法预览的,因为我们只定义了jndi的名字,没有建立jndi连接。
注:只需要设置JNDI的名字,上下文里面的可以省略。
1.2WEBLOGIC中的JNDI连接配置登陆WEBLOGIC的管理平台,在域结构里面选择服务Services,选择Jdbc数据源,新建Connectionpool设置数据源JNDI名称为oracle(即制作模板时定义的JNDI名字),选择数据库类型及驱动点击“下一步”,如下图:事务选项根据自己要求定义,在此默认,点击“下一步”配置连接属性:数据库名,URL,用户名及密码,建立weblogic10至oracle10g的物理连接。
测试刚定义的数据源连接:点击下一步,weblogic服务器会呈现之前我们配置好的数据源,并提供连接测试,点击“测试配置”,弹出消息“连接测试成功”说明配置正确,点击“下一步”,部署新建的JDBC数据源:选中需要部署该数据源的服务器,点击“完成”。
数据库连接池中的连接泄漏问题分析与解决数据库连接池是应用程序与数据库之间的桥梁,通过连接池,应用程序可以高效地管理数据库连接,提高系统性能。
然而,在数据库连接池中,连接泄漏是一种常见的问题,它会导致系统资源的浪费,甚至可能导致系统崩溃。
本文将对数据库连接池中的连接泄漏问题进行分析,并介绍相应的解决方法,以帮助读者更好地理解和解决这一问题。
首先,我们来解释一下数据库连接泄漏的概念。
数据库连接泄漏指的是在应用程序使用完数据库连接后没有正确地关闭连接,导致数据库连接未被释放,从而造成连接资源的浪费。
当连接泄漏发生时,连接池中的空闲连接数量逐渐减少,最终导致无法获取可用的数据库连接。
如果系统中连接泄漏的数量过多,系统资源将会被耗尽,从而引发性能问题甚至系统崩溃。
接下来,我们将分析数据库连接泄漏的原因。
常见的连接泄漏原因包括以下几点:1. 程序代码错误:连接泄漏可能是由于程序代码中的错误导致的,例如忘记关闭数据库连接或者错误地关闭了连接。
2. 异常情况:当应用程序遇到异常情况时,可能导致数据库连接没有正确地关闭。
例如,在使用连接的过程中发生异常,未能通过异常处理机制来关闭连接。
3. 多线程并发访问:在多线程环境下,如果没有正确地管理和同步数据库连接的使用,可能会导致连接泄漏问题。
针对以上问题,我们可以采取一些解决方法来避免连接泄漏:1. 编写稳健的代码:编写良好的程序代码是避免连接泄漏的基础。
在使用完数据库连接后,务必在finally块中关闭连接,确保无论是否发生异常,连接都能被正确关闭。
2. 使用连接池配置参数:连接池通常提供了一些配置参数,如最大连接数、最小空闲连接数等。
合理配置这些参数可以避免连接泄漏问题。
例如,设置合理的最大连接数,确保不会产生过多的连接资源,减少泄漏的可能性。
3. 追踪和监控连接的使用:在大型系统中,追踪和监控数据库连接的使用情况非常重要。
通过使用连接池的监控工具,可以及时检测到连接泄漏的情况。
一、weblogic数据源配置进入到weblogic控制台,找到服务→jdbc→数据源,锁定并编辑后,新增数据源. 进入到新建页面,如下图:修改配置如下:注意,上面的jndi名称,需要在torConfig.xml配置文件中用到点击下一步(此步不需修改配置),再点击下一步,进入到如下页面:修改配置:数据库名称,即oracle的实例名(SID),主机名:可以输入oracle数据库所在机器的IP,端口号,根据实际情况输入,用户名和密码都根据实际生产环境的配置输入.以上配置,可以根据目前的torConfig.xml文件中的数据库连接方式把相应的配置录入到页面即可.如下图为我本机的配置:再点击下一步,出现如下图的页面:根据前一步数据库配置不同,有些数据与下图会不一样,点击测试配置,如果显示连接测试成功即表示配置成功:测试成功后,点击下一步,选择对应的运用服务,点击完成即可.(备注:每次修改保存后、都需要激活更改)激活以后,再次选择jdbc数据源,并点击“锁定并编辑”,选择对应的数据源,点击进入,会到以下页面:选择连接缓冲池,进入以下页面:调整初始容量为:30,最大容量为250(目前客户生产环境,设置的oracle最大连接数为300),容量增长为:15;语句缓存大小为:30;根据上面的配置调整后,激活更改,weblogic的数据源配置即可完成二、tor数据库连接配置把TORConfig.xml文件中目前的配置,如下代码,注释掉:<bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource" destroy-method="close"><property name="driver"><value>oracle.jdbc.driver.OracleDriver</value></property><property name="driverUrl"><value>jdbc:oracle:thin:gzldoa/*************.1.201:1521:ldoadev</value></property><property name="user"><value>gzldoa</value></property><property name="password"><value>toone</value></property><property name="alias"><value>gzwork</value></property><property name="houseKeepingSleepTime"><value>90000</value></property><property name="prototypeCount"><value>5</value></property><property name="maximumConnectionCount"><value>100</value></property><property name="minimumConnectionCount"><value>10</value></property><property name="trace"><value>true</value></property><property name="verbose"><value>true</value></property></bean>新增jndi连接方式配置:<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName"><value>ldjoa</value></property></bean>后保存即可,注意需要把上面的ldjoa改成刚在weblogic控制台配置的jndi名称。
jndiexploit用法JNDIExploit用法简介JNDIExploit是一种利用Java命名和目录接口(Java Naming and Directory Interface)漏洞的工具。
它可以帮助攻击者利用这些漏洞来执行远程代码,导致服务器的远程命令执行或拒绝服务攻击。
JNDI是Java中用于访问命名和目录服务的API。
它允许Java应用程序通过命名和目录服务查找和访问各种资源,如数据库连接、消息队列、网络服务等。
然而,由于JNDI的设计缺陷,攻击者可以构造恶意输入,导致服务器执行任意代码。
JNDIExploit利用这些漏洞的原理是通过构造恶意的JNDI引用,使得服务器在查询和加载资源时,会尝试执行恶意代码或连接到攻击者控制的恶意服务。
这种攻击通常利用了JNDI的远程资源加载功能。
为了成功利用JNDIExploit,攻击者需要了解目标服务器上正在使用的JNDI资源,并构造相应的恶意JNDI引用。
攻击者可以使用不同的技术和工具来获取目标服务器上的JNDI资源信息,如查看配置文件、使用漏洞扫描器等。
一旦攻击者成功构造了恶意JNDI引用,他们可以将其发送到目标服务器,并等待服务器查询和加载该资源。
当服务器尝试加载恶意JNDI引用时,攻击者控制的恶意服务或恶意代码将被执行,从而实现远程命令执行或拒绝服务攻击。
为了保护服务器免受JNDIExploit攻击,管理员可以采取以下措施:1. 及时更新和修补软件:及时应用相关安全补丁,以修复已知的JNDI漏洞和其他安全问题。
2. 最小化JNDI资源的使用:只使用必要的JNDI资源,并限制其访问权限。
3. 审查代码和配置文件:审查服务器代码和配置文件,确保没有明显的安全漏洞和不安全的JNDI引用。
4. 网络隔离和访问控制:使用防火墙和访问控制策略来限制对JNDI服务的访问,并阻止来自未信任来源的恶意请求。
总之,JNDIExploit是一种利用Java命名和目录接口漏洞的工具,攻击者可以通过构造恶意的JNDI引用来执行远程代码。
性能测试工作室,专注于性能测试技术研究(JMSRuntimeJMSServersCurrentCount 返回当前JMS服务的连接数ConnectionsCurrentCount 返回本JMS服务器上当前的连接数JMSServersHighCount 返回自服务器启动后JMS服务的最大连接数ConnectionsHighCount 返回本JMS服务器自上次重置后的最大连接数JVMRuntimeHeapSizeCurrent 返回当前JVM堆中内存数,单位时字节HeapFreeCurrent 返回当前JVM堆中空闲内存数,单位时字节ExecuteQueueRuntimeExecuteThreadCurrentIdleCount 返回队列中当前空闲线程数PendingRequestOldestTime 返回队列中最长的等待时间PendingRequestCurrentCount 返回队列中等待的请求数Queue Length 队列长度JDBCConnectionPoolRuntimeWaitingForConnectionHighCount返回本JDBCConnectionPoolRuntimeMBean 上最大等待连接数WaitingForConnectionCurrentCount 返回当前等待连接的总数MaxCapacity 返回JDBC池的最大能力WaitSecondsHighCount 返回等待连接中的最长时间等待者的秒数ActiveConnectionsCurrentCount 返回当前活动连接总数ActiveConnectionsHighCount 返回本JDBCConnectionPoolRuntimeMBean 上最大活动连接数注:weblogic通常监控JVM和执行队列,JDBC连接池,其中执行队列最关键的指标是Queue Length 队列长度weblogic一般来说监控jvm的使用、执行线程队列情况、和连接池的变化情况,还有一个很重要的检查weblogic的console日志这里经常能反映一些很重要到情况。
weblogic主管服务日志
1. 登录WebLogic管理控制台并选择要查看服务日志的运行中的WebLogic服务器示例。
2. 在WebLogic Server控制台的左侧导航中,展开“诊断”部分,然后单击“日志文件”。
3. 在“日志文件”页面上,选择“服务器日志文件”选项卡。
4. 在“服务器日志文件”选项卡下,您可以选择服务器日志文件的级别和存储位置。
默认情况下,WebLogic Server会将服务日志存储在域目录下的logs目录中。
5. 在手动刷新之前,您可以在此页面上选择查看服务器日志的时间范围。
6. 单击“刷新”按钮以查看最新的服务日志条目。
您也可以使用WebLogic Server命令行工具来查看服务日志。
以下是命令行工具中查看服务日志文件的示例:
1. 以管理员模式登录WebLogic Server。
2. 进入WebLogic Server域的bin目录。
3. 运行以下命令:
```
tail -f ../logs/server.log
```
此命令将显示服务器日志文件的最新条目,并将持续监视日志文件并显示新条目。
要停止此命令,请使用“Ctrl + C”组合键。
请注意,这只是查看WebLogic Server服务日志的基本步骤,具体步骤可能因WebLogic版本和配置而异。
jndi-injection-exploit 参数JNDI 注入攻击参数漏洞随着互联网的普及和发展,越来越多的服务和应用程序依赖于各种远程资源。
Java 命名和目录接口(Java Naming and Directory Interface,JNDI)是 Java 平台提供的一种标准 API,用于访问命名和目录服务,如 DNS、LDAP、RMI 等。
然而,由于一些安全配置不当,JNDI 参数注入漏洞成为攻击者利用的突破口。
本文将介绍 JNDI 注入攻击参数漏洞的原理、影响和防御方法。
一、原理JNDI 注入攻击参数漏洞是一种攻击方式,攻击者利用受害者在解析 JNDI 参数时没有进行充分过滤和验证的漏洞来执行恶意代码。
攻击者通常使用以下方式进行攻击:1.构造恶意输入数据:攻击者通过构造特殊的输入数据,将恶意代码嵌入参数中,例如将JNDI 参数设置为“ldap://恶意服务器/恶意代码”。
2.传递恶意参数:攻击者将构造好的恶意参数传递给目标应用程序,触发解析过程。
3.执行恶意代码:受害者应用程序在解析恶意参数时,将尝试连接到攻击者指定的恶意服务器,并执行恶意代码。
二、影响JNDI 注入攻击参数漏洞能够对受影响的应用程序造成严重的安全威胁,可能导致以下问题:1.远程代码执行:攻击者能够在受害者的服务器上执行任意代码,进而控制服务器进行恶意活动。
2.信息泄露:攻击者可以通过恶意代码访问目标系统中的敏感信息,包括用户凭据和数据库连接信息等。
3.拒绝服务:攻击者可能通过恶意代码导致目标系统崩溃或无法正常运行,造成拒绝服务攻击。
三、防御方法为了防止 JNDI 注入攻击参数漏洞的利用,以下是一些有效的防御方法:1.输入验证:对于接收 JNDI 参数的应用程序,应该对用户输入进行充分验证和过滤,确保参数的合法性和安全性。
2.最小权限原则:将应用程序的权限限制到最低级别,避免攻击者通过受感染的应用程序获取到系统权限。
通过JMX获取weblogic的监控指标通过JMX获取weblogic的监控数据,包括JDBC,SESSION,SERVERLET,JVM等信息。
主要⽤到weblogic⾃⼰的t3协议,所以要⽤到weblogic的jar包:wlfullclient.jar和wlclient.jar。
这两个jar包怎么获取我专门在另外⼀篇⽂章中讲。
下⾯贴⼀些获取监控指标的代码,做个备份只⽤吧。
1、对JDBC的监控,只取了最重要的监控指标:package test;import java.io.IOException;import .MalformedURLException;import java.util.Hashtable;import javax.management.MBeanServerConnection;import javax.management.MalformedObjectNameException;import javax.management.ObjectName;import javax.management.remote.JMXConnector;import javax.management.remote.JMXConnectorFactory;import javax.management.remote.JMXServiceURL;import javax.naming.Context;public class PrintJdbc {private static MBeanServerConnection connection;private static JMXConnector connector;private static final ObjectName service;static {try {service = new ObjectName("com.bea:Name=RuntimeService,Type=weblogic.management.mbeanservers.runtime.RuntimeServiceMBean");}catch (MalformedObjectNameException e) {throw new AssertionError(e.getMessage());}}public static void initConnection(String hostname, String portString,String username, String password) throws IOException,MalformedURLException {String protocol = "t3";Integer portInteger = Integer.valueOf(portString);int port = portInteger.intValue();String jndiroot = "/jndi/";String mserver = "weblogic.management.mbeanservers.runtime";JMXServiceURL serviceURL = new JMXServiceURL(protocol, hostname,port, jndiroot + mserver);Hashtable h = new Hashtable();h.put(Context.SECURITY_PRINCIPAL, username);h.put(Context.SECURITY_CREDENTIALS, password);h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES,"weblogic.management.remote");connector = JMXConnectorFactory.connect(serviceURL, h);connection = connector.getMBeanServerConnection();}public static ObjectName getServerRuntimes() throws Exception {return (ObjectName)connection.getAttribute(service,"ServerRuntime");}public ObjectName getJDBCServer() throws Exception {ObjectName dc = getServerRuntimes();ObjectName jdbcService = (ObjectName)connection.getAttribute(dc,"JDBCServiceRuntime");return jdbcService;}public ObjectName[] getJDBCDataSourceRuntime() throws Exception {ObjectName[] jdbcDataSourceRTMB = (ObjectName[]) connection.getAttribute(getJDBCServer(), "JDBCDataSourceRuntimeMBeans");return jdbcDataSourceRTMB;}public void printJdbc() throws Exception {ObjectName[] objectList = getJDBCDataSourceRuntime();if(objectList != null && objectList.length > 0){for(ObjectName obj : objectList){int connectionsTotalCount = (int) connection.getAttribute(obj, "ConnectionsTotalCount");int activeConnectionsCurrentCount = (int)connection.getAttribute(obj, "ActiveConnectionsCurrentCount");int activeConnectionsAverageCount = (int)connection.getAttribute(obj,"ActiveConnectionsAverageCount");int failuresToReconnectCount = (int)connection.getAttribute(obj, "FailuresToReconnectCount");String name = (String)connection.getAttribute(obj, "Name");int waitingForConnectionCurrentCount = (int)connection.getAttribute(obj, "WaitingForConnectionCurrentCount");long waitingForConnectionFailureTotal = (long)connection.getAttribute(obj, "WaitingForConnectionFailureTotal");int waitSecondsHighCount = (int)connection.getAttribute(obj, "WaitSecondsHighCount");int connectionDelayTime = (int)connection.getAttribute(obj, "ConnectionDelayTime");int activeConnectionsHighCount = (int)connection.getAttribute(obj, "ActiveConnectionsHighCount");int currCapacity = (int)connection.getAttribute(obj, "CurrCapacity");System.out.println("currCapacity==" + currCapacity);System.out.println("activeConnectionsHighCount==" + activeConnectionsHighCount);System.out.println("connectionDelayTime==" + connectionDelayTime);System.out.println("connectionsTotalCount==" + connectionsTotalCount);System.out.println("activeConnectionsCurrentCount==" + activeConnectionsCurrentCount);System.out.println("activeConnectionsAverageCount==" + activeConnectionsAverageCount);System.out.println("failuresToReconnectCount==" + failuresToReconnectCount);System.out.println("name==" + name);System.out.println("waitingForConnectionCurrentCount==" +waitingForConnectionCurrentCount );System.out.println("waitingForConnectionFailureTotal==" + waitingForConnectionFailureTotal);System.out.println("waitSecondsHighCount==" + waitSecondsHighCount);}}}public static void main(String[] args) throws Exception {String hostname = "10.111.131.50";String portString = "7001";String username = "weblogic";String password = "weblogic123";PrintJdbc s = new PrintJdbc();initConnection(hostname, portString, username, password);s.printJdbc();connector.close();}} 2、有关thread的监控:package test;import java.io.IOException;import .MalformedURLException;import java.util.Hashtable;import javax.management.MBeanServerConnection;import javax.management.MalformedObjectNameException;import javax.management.ObjectName;import javax.management.remote.JMXConnector;import javax.management.remote.JMXConnectorFactory;import javax.management.remote.JMXServiceURL;import javax.naming.Context;public class PrintThread {private static MBeanServerConnection connection;private static JMXConnector connector;private static final ObjectName service;static {try {service = new ObjectName("com.bea:Name=RuntimeService,Type=weblogic.management.mbeanservers.runtime.RuntimeServiceMBean"); }catch (MalformedObjectNameException e) {throw new AssertionError(e.getMessage());}}public static void initConnection(String hostname, String portString,String username, String password) throws IOException,MalformedURLException {String protocol = "t3";Integer portInteger = Integer.valueOf(portString);int port = portInteger.intValue();String jndiroot = "/jndi/";String mserver = "weblogic.management.mbeanservers.runtime";JMXServiceURL serviceURL = new JMXServiceURL(protocol, hostname,port, jndiroot + mserver);Hashtable h = new Hashtable();h.put(Context.SECURITY_PRINCIPAL, username);h.put(Context.SECURITY_CREDENTIALS, password);h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES,"weblogic.management.remote");connector = JMXConnectorFactory.connect(serviceURL, h);connection = connector.getMBeanServerConnection();}public static ObjectName getServerRuntimes() throws Exception {return (ObjectName)connection.getAttribute(service,"ServerRuntime");}public void printInfo() throws Exception{ObjectName objThreadPool = null;ObjectName serverRT = getServerRuntimes();objThreadPool = (ObjectName) connection.getAttribute(serverRT, "ThreadPoolRuntime");double throughput = Double.parseDouble(String.valueOf(connection.getAttribute(objThreadPool,"Throughput")));System.out.println("throughput:" + throughput);int executeThreadTotalCount = Integer.parseInt(String.valueOf(connection.getAttribute(objThreadPool, "ExecuteThreadTotalCount"))); System.out.println("executeThreadTotalCount:" + executeThreadTotalCount);int executeThreadIdleCount = Integer.parseInt(String.valueOf(connection.getAttribute(objThreadPool, "ExecuteThreadIdleCount"))); System.out.println("executeThreadIdleCount:" + executeThreadIdleCount);int StandbyThreadCount = Integer.parseInt(String.valueOf(connection.getAttribute(objThreadPool, "StandbyThreadCount")));System.out.println("StandbyThreadCount:" + StandbyThreadCount);long timestamp = System.currentTimeMillis() / 1000;String metricJson = "";String jsonFormat = "{\"name\": \"weblogic_threadpool_metric_demo\", " +"\"command\":\"weblogic_threadpool_metric\"," +"\"type\": \"metric\"," +"\"handlers\": [\"influxdb\"],"+"\"output\": \"%s %.5f %d\\n%s %d %d\\n%s %d %d\\n\"," +"\"status\": 0}";metricJson = String.format(jsonFormat,".weblogic.threadpool.throughput", (double)throughput, timestamp,".weblogic.threadpool.executeThreadTotalCount", (int)executeThreadTotalCount, timestamp,".weblogic.threadpool.executeThreadIdleCount", (int)executeThreadIdleCount, timestamp);System.out.println("metricJson==" + metricJson);}public static void main(String args[]) throws Exception {String hostname = "10.111.131.50";String portString = "7001";String username = "weblogic";String password = "weblogic123";PrintThread s = new PrintThread();initConnection(hostname, portString, username, password);s.printInfo();connector.close();}} 3、有关session的监控:package test;import java.io.IOException;import .MalformedURLException;import java.util.Hashtable;import javax.management.MBeanServerConnection;import javax.management.MalformedObjectNameException;import javax.management.ObjectName;import javax.management.remote.JMXConnector;import javax.management.remote.JMXConnectorFactory;import javax.management.remote.JMXServiceURL;import javax.naming.Context;public class PrintServerState {private static MBeanServerConnection connection;private static JMXConnector connector;private static final ObjectName service;static {try {service = new ObjectName("com.bea:Name=DomainRuntimeService,Type=weblogic.management.mbeanservers.domainruntime.DomainRuntimeServiceMBean"); }catch (MalformedObjectNameException e) {throw new AssertionError(e.getMessage());}}public static void initConnection(String hostname, String portString,String username, String password) throws IOException,MalformedURLException {String protocol = "t3";Integer portInteger = Integer.valueOf(portString);int port = portInteger.intValue();String jndiroot = "/jndi/";String mserver = "weblogic.management.mbeanservers.domainruntime";JMXServiceURL serviceURL = new JMXServiceURL(protocol, hostname,port, jndiroot + mserver);Hashtable h = new Hashtable();h.put(Context.SECURITY_PRINCIPAL, username);h.put(Context.SECURITY_CREDENTIALS, password);h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES,"weblogic.management.remote");connector = JMXConnectorFactory.connect(serviceURL, h);connection = connector.getMBeanServerConnection();}public static ObjectName[] getServerRuntimes() throws Exception {return (ObjectName[]) connection.getAttribute(service,"ServerRuntimes");}public static ObjectName[] getApplicationRuntimes(ObjectName serverRuntime) throws Exception {return (ObjectName[])connection.getAttribute(serverRuntime, "ApplicationRuntimes");}public static ObjectName[] getComponentRuntimes(ObjectName componentRutime) throws Exception {return (ObjectName[]) connection.getAttribute(componentRutime, "ComponentRuntimes");}public void printSessionState() throws Exception {ObjectName[] serverRT = getServerRuntimes();int length = (int) serverRT.length;for(int i = 0;i<length;i++){ObjectName[] applicationRT = getApplicationRuntimes(serverRT[i]);int length_app = (int)applicationRT.length;for(int y = 0;y<length_app;y++){String applicationName = (String)connection.getAttribute(applicationRT[y], "ApplicationName");if(applicationName.equals("helloworld")){ObjectName[] componentRuntime = getComponentRuntimes(applicationRT[y]);int length_component = (int)componentRuntime.length;System.out.println("length_component==" + length_component);for(int z = 0;z<length_component;z++){String type = (String)connection.getAttribute(componentRuntime[z], "Type");if(type.equals("WebAppComponentRuntime")){int SessionsOpenedTotalCount = (int)connection.getAttribute(componentRuntime[z], "SessionsOpenedTotalCount");System.out.println("SessionsOpenedTotalCount==" + SessionsOpenedTotalCount);int openSessionsHighCount = (int)connection.getAttribute(componentRuntime[z], "OpenSessionsHighCount");System.out.println("openSessionsHighCount==" + openSessionsHighCount);int openSessionsCurrentCount = (int)connection.getAttribute(componentRuntime[z], "OpenSessionsCurrentCount");System.out.println("openSessionsCurrentCount==" + openSessionsCurrentCount);break;}}break;}}}}public void printNameAndState() throws Exception {ObjectName[] serverRT = getServerRuntimes();System.out.println("got server runtimes");int length = (int) serverRT.length;for (int i = 0; i < length; i++) {String name = (String) connection.getAttribute(serverRT[i],"Name");String state = (String) connection.getAttribute(serverRT[i],"State");long activationTime = (Long)connection.getAttribute(serverRT[i], "ActivationTime");System.out.println("Server name: " + name);System.out.println("Server state:" + state);System.out.println("Server activationTime:" + activationTime);String weblogicVersion = (String) connection.getAttribute(serverRT[i], "WeblogicVersion");System.out.println("Server weblogicVersion:" + weblogicVersion);String weblogicHome = (String) connection.getAttribute(serverRT[i], "WeblogicHome");System.out.println("Server WeblogicHome:" +weblogicHome );}}public static void main(String[] args) throws Exception {String hostname = "10.111.131.50";String portString = "7001";String username = "weblogic";String password = "weblogic123";PrintServerState s = new PrintServerState();initConnection(hostname, portString, username, password);s.printNameAndState();s.printSessionState();connector.close();}} 4、有关serverlet的监控:package test;import java.io.IOException;import .MalformedURLException;import java.util.Hashtable;import javax.management.MBeanServerConnection;import javax.management.MalformedObjectNameException;import javax.management.ObjectName;import javax.management.remote.JMXConnector;import javax.management.remote.JMXConnectorFactory;import javax.management.remote.JMXServiceURL;import javax.naming.Context;public class PrintServerlet {private static MBeanServerConnection connection;private static JMXConnector connector;private static final ObjectName service;private final String applicationName_app="helloworld";private final String serverletName1="index.jsp";private final String serverletName2="helloworld.jsp";static {try {service = new ObjectName("com.bea:Name=DomainRuntimeService,Type=weblogic.management.mbeanservers.domainruntime.DomainRuntimeServiceMBean"); }catch (MalformedObjectNameException e) {throw new AssertionError(e.getMessage());}}public static void initConnection(String hostname, String portString,String username, String password) throws IOException,MalformedURLException {String protocol = "t3";Integer portInteger = Integer.valueOf(portString);int port = portInteger.intValue();String jndiroot = "/jndi/";String mserver = "weblogic.management.mbeanservers.domainruntime";JMXServiceURL serviceURL = new JMXServiceURL(protocol, hostname,port, jndiroot + mserver);Hashtable h = new Hashtable();h.put(Context.SECURITY_PRINCIPAL, username);h.put(Context.SECURITY_CREDENTIALS, password);h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES,"weblogic.management.remote");connector = JMXConnectorFactory.connect(serviceURL, h);connection = connector.getMBeanServerConnection();}public static ObjectName[] getServerRuntimes() throws Exception {return (ObjectName[]) connection.getAttribute(service,"ServerRuntimes");}public String getApplicationName(ObjectName objectName) throws Exception {return (String)connection.getAttribute(objectName, "Name");}public String getServletName(ObjectName objectName) throws Exception {return (String)connection.getAttribute(objectName,"Name");}public void getServletData() throws Exception {ObjectName[] serverRT = getServerRuntimes();int length = (int) serverRT.length;for (int i = 0; i < length; i++) {ObjectName[] appRT =(ObjectName[]) connection.getAttribute(serverRT[i],"ApplicationRuntimes");int appLength = (int) appRT.length;for (int x = 0; x < appLength; x++) {String applicationName = getApplicationName(appRT[x]);if(applicationName_app.equals(applicationName)){System.out.println("Application name: " + applicationName);ObjectName[] compRT =(ObjectName[]) connection.getAttribute(appRT[x],"ComponentRuntimes");int compLength = (int) compRT.length;for (int y = 0; y < compLength; y++) {printValue("Component name",(String)connection.getAttribute(compRT[y], "Name"));String componentType =(String) connection.getAttribute(compRT[y], "Type");System.out.println(componentType.toString());if (componentType.toString().equals("WebAppComponentRuntime")){ObjectName[] servletRTs = (ObjectName[])connection.getAttribute(compRT[y], "Servlets");int servletLength = (int) servletRTs.length;for (int z = 0; z < servletLength; z++) {String servletName = getServletName(servletRTs[z]);if (servletName.contains(serverletName1) || servletName.contains(serverletName2)){ printValue("Servlet name",servletName);printValue("Servlet context path" ,(String)connection.getAttribute(servletRTs[z],"ContextPath"));printValue("Invocation Total Count" ,(Object)connection.getAttribute(servletRTs[z],"InvocationTotalCount"));printValue("ExecutionTimeHigh",(Object)connection.getAttribute(servletRTs[z],"ExecutionTimeHigh"));printValue("ExecutionTimeTotal",(Object)connection.getAttribute(servletRTs[z],"ExecutionTimeTotal"));printValue("ExecutionTimeAverage",(Object)connection.getAttribute(servletRTs[z], "ExecutionTimeAverage"));}}}}}}}}private void printValue(String key,Object value){System.out.println(" " + key + " : " + value);}public static void main(String[] args) throws Exception {String hostname = "10.111.131.50";String portString = "7001";String username = "weblogic";String password = "weblogic123";PrintServerlet s = new PrintServerlet();initConnection(hostname, portString, username, password); s.getServletData();connector.close();}}。
精心整理
精心整理
Weblogic 监控jndi 泄露连接的方法
1 说明
如果后台出现“Noresourcescurrentlyavailableinpooliscjnditoallocatetoapplications ”类似这个错误,说明weblogic 的数据库连接池满了:
可以考虑通过此方法排查一下具体是程序中的什么方法导致。
查看数据源监控页面,如果“当前活动连接数”超过系统配置的最大数字(系统默认是15,优化之后为150),2 123443 12344 查看日志
在系统中操作,重现一下相关问题,同时监控weblogic 日志,日志中会出现类似以下格式的异常信息,提供研发找到具体class 进一步排查泄露原因。
注意:操作之后等待一段时间,weblogic 自动回收连接时才产生日志,不是操作的时候立即产生。
日志文件位置:
/app/weblogic/user_projects/domains/【domain 名称】/servers/【server 名称】/logs/【server 名称】.log 比如总部测试环境的日志文件位置:
/app/weblogic /user_projects/domains/mpsm_domain /servers/mpsmServer /logs/mpsmServer .log
精心整理。