我就用Swing写了一个GUI界面,整合了一些配置信息(结构有些复杂,但是比较容易操作,比直接通过console要方便。主要当时为公司项目写的,便于以后项目中操作)。
仅针对connection pool配置这一块。主要代码大致如下:
public void deployServerDataSource(){
try {
ctx = getInitialContext();
if(ctx!=null){
this.txtareaDeployStatus.append(\r\n+_INFO : 连接
服务器,并登陆成功);
this.txtareaDeployStatus.append(\r\n+_INFO : 获取管理对象);
//getting the Administration MBeanHome
mbeanHome = (MBeanHome)ctx.lookup(MBeanHome.ADMIN_JNDI_NAME);
this.txtareaDeployStatus.append(\r\n+_INFO : 获取Admin Server);
serverMBean = (ServerMBean)mbeanHome.getAdminMBean(serverName, Server);
this.txtareaDeployStatus.append(\r\n+_INFO : 获取domain);
domainName = mbeanHome.getDomainName();
}else{
this.txtareaDeployStatus.append(\r\n+_INFO : 连接
服务器失败,请服务器正确启动,并且登陆正确);
return;
}
}
catch (Exception ex) {
this.txtareaDeployStatus.append(\r\n+_ERROR: 初始化管理服务器信息失败,配置终止);
return;
}
try{
this.txtareaDeployStatus.append(\r\n+_INFO : 开始配置WebLogic DataSource of RiseNet);
config();
this.txtareaDeployStatus.append(\r\n+_INFO : 配置WebLogic DataSource of RiseNet成功 );
JOptionPane.showMessageDialog(this, 配置WebLogic DataSource of RiseNet成功, null, JOptionPane.INFORMATION_MESSAGE);
}catch(Exception ex){
this.txtareaDeployStatus.append(\r\n+_ERROR: 配置WebLogic DataSource of RiseNet失败 );
JOptionPane.showMessageDialog(this, 配置WebLogic DataSource of RiseNet失败, 错误, JOptionPane.ERROR_MESSAGE);
}
//需要在配置完以后,进行一次连接测试。
try{
javax.sql.DataSource ds = null;
ds = (javax.sql.DataSource)ctx.lookup(cpDataSourceJNDIName);
java.sql.Connection c = ds.getConnection();
c.close();
}catch(Exception ex){
this.txtareaDeployStatus.append(\r\n+_ERROR: 测试失败 );
this.txtareaDeployStatus.append(\r\n+_ERROR: 请手动进行Apply一次,否则配置DataSource在服务器重新启动后失效 );
JOptionPane.showMessageDialog(this, 测试失败, 错误, JOptionPane.ERROR_MESSAGE);
}
}
public void config() throws Exception{
cpPoolName = this.txtPoolName.getText();
cpDataSourceName = this.txtDataSourceName.getText();
cpDataSourceJNDIName = this.txtDataSourceJNDIName.getText();
//将原有的连接DataSource信息删除
deleteDataSource();
try {
createDataSource();
}
catch (Exception ex) {
ex.printStackTrace();
throw ex;
}
}
public void createDataSource() throws Exception {
/**
* WebLogicObjectName的构造起的参数如下:
* java.lang.String name 名称
* java.lang.String type 类型
* java.lang.String domain 域
* java.lang.String location
* WebLogicObjectName parent 父节点
*/
/*
WebLogicObjectName pname = new WebLogicObjectName(server1, ServerRuntime, domainName,server1);
WebLogicObjectName oname = new WebLogicObjectName(cpName, JDBCConnectionPoolRuntime, domainName,server1, pname);
JDBCConnectionPoolRuntimeMBean cprmb = (JDBCConnectionPoolRuntimeMBean)mbeanHome.getMBean(oname);
*/
Vector vct = RiseNet.getDefaultDataSource();
String jdbc_user = (String)vct.get(RiseContent.HASHMAP_JDBC_USERNAME);
String jdbc_url = (String)vct.get(RiseContent.HASHMAP_JDBC_URL);
String jdbc_pass = (String)vct.get(RiseContent.HASHMAP_JDBC_PASSWROD);
String jdbc_driver = (String)vct.get(RiseContent.HASHMAP_JDBC_DRIVER_NAME);
String t_Properties = (String)vct.get(RiseContent.HASHMAP_JDBC_PROPERTIES);
t_Properties = ConfigUtil.convertEnter(t_Properties);
Properties pros = new Properties();
pros.put(user, jdbc_user);
String[] t_PropertiesArr = null;
if(!(t_Properties==null || t_Properties.equals())){
t_PropertiesArr = StrUtil.separateDateStr(t_Properties,;);
}
if(t_PropertiesArr!=null && t_PropertiesArr.length>0){
for(int i=0;i<t_PropertiesArr.length;i++){
String[] tmpArr = StrUtil.separateDateStr(t_PropertiesArr[i],=);
pros.put(tmpArr[0],tmpArr[1]);
}
}
// Create ConnectionPool MBean
JDBCConnectionPoolMBean cpMBean = (JDBCConnectionPoolMBean)mbeanHome.createAdminMBean(cpPoolName, JDBCConnectionPool, domainName);
cpMBean.setProperties(pros);
cpMBean.setURL(jdbc_url);
cpMBean.setDriverName(jdbc_driver);
cpMBean.setPassword(jdbc_pass);
cpMBean.setLoginDelaySeconds(1);
cpMBean.setInitialCapacity(0);
cpMBean.setMaxCapacity(5);
cpMBean.setCapacityIncrement(5);
cpMBean.setShrinkingEnabled(true);
cpMBean.setShrinkPeriodMinutes(10);
//cpMBean.setRefreshMinutes(10); //空闲10分钟后,即测试连接
//cpMBean.setTestTableName(dual);
//cpMBean.setACLName(dynapool);
cpMBean.addTarget(serverMBean);
cpMBean.setPersistenceEnabled(true);
JDBCDataSourceMBean dsMBeans = (JDBCDataSourceMBean)mbeanHome.createAdminMBean(cpDataSourceName, JDBCDataSource, domainName);
dsMBeans.setJNDIName(cpDataSourceJNDIName);
dsMBeans.setPoolName(cpPoolName);
dsMBeans.addTarget(serverMBean);
dsMBeans.setPersistenceEnabled(true);
}