有使用过JMX实时修改wblogic连接池密码的么?

taber taber 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
系统使用jndi通过weblogic81连接池访问数据库。
===========
碰到的问题
===========
由于安全原因,数据库密码会定期修改,这样导致weblogic服务重新启动后无法连接到数据库。
由于部署了weblogic的机器众多,不希望一台台连上去通过console修改密码,而是通过部署在weblogic中的
一个jar包自动获得正确的密码,然后更新weblogic的config.xml文件,同时刷新连接池,使其能够正常工作。
=====
现状
=====
已经能够使用JMX修改连接池密码了(见modifyAdmin方法),但连接池仍无法正常使用。请高手指教,帮顶有分:

package weblogic;

import java.util.Properties;

import javax.management.InstanceNotFoundException;
import javax.management.InvalidAttributeValueException;
import javax.naming.Context;
import javax.naming.NamingException;

import weblogic.jndi.Environment;
import weblogic.management.MBeanHome;
import weblogic.management.configuration.JDBCConnectionPoolMBean;

/**
* 测试调用weblogic api直接修改config.xml
*
*
*/
public class ConfigOperator {

/**
* @param args
*/
public static void main(String[] args) {
//modifyAdmin("user", "pass");
modifyConfig("user", "pass");
System.out.println("test finished");
}

public static void modifyConfig(String userName, String password) {
try {
String poolName = "MyJDBC Connection Pool";
MBeanHome home = getMBeanHome();
JDBCConnectionPoolMBean mConfigBean = (JDBCConnectionPoolMBean) home
.getConfigurationMBean(poolName, "JDBCConnectionPoolConfig");
Properties nameProperty = new Properties();
nameProperty.setProperty("name", userName);
mConfigBean.setProperties(nameProperty);
mConfigBean.setPassword(password);
System.out.println(mConfigBean.getPassword());
//mConfigBean.setDriverName("helloworld1");
} catch (InvalidAttributeValueException iave) {
System.out.println("Invalid attribute while configuring tracing "
+ iave);
} catch (InstanceNotFoundException infe) {
System.out.println("Instance not found while configuring tracing "
+ infe);
} catch (Exception e) {
System.out.println(e);
}
}

public static void modifyAdmin(String userName, String password) {
try {
String poolName = "MyJDBC Connection Pool";
MBeanHome home = getMBeanHome();
JDBCConnectionPoolMBean mConfigBean = (JDBCConnectionPoolMBean) home
.getAdminMBean(poolName, "JDBCConnectionPool");
Properties nameProperty = new Properties();
nameProperty.setProperty("name", userName);
mConfigBean.setProperties(nameProperty);
mConfigBean.setPassword(password);
//mConfigBean.setDriverName("helloworld1");
} catch (InvalidAttributeValueException iave) {
System.out.println("Invalid attribute while configuring tracing "
+ iave);
} catch (InstanceNotFoundException infe) {
System.out.println("Instance not found while configuring tracing "
+ infe);
} catch (Exception e) {
System.out.println(e);
}
}

private static MBeanHome getMBeanHome() {
String url = "t3://localhost:7001";
String username = "weblogic";
String password = "weblogic";
MBeanHome home = null;

try {
Environment env = new Environment();
env.setProviderUrl(url);
env.setSecurityPrincipal(username);
env.setSecurityCredentials(password);
Context ctx = env.getInitialContext();
home = (MBeanHome) ctx.lookup(MBeanHome.LOCAL_JNDI_NAME);
} catch (NamingException ne) {
System.out.println("Error getting MBeanHome " + ne);
}
return home;
}

}

...全文
评论 8
收藏 已收藏
分享
返回顶部
taber
Java SE 62,621 成员 307,251 内容