使用jmx对weblogic进行动态的配置(源代码)

王朝other·作者佚名  2008-05-31
窄屏简体版  字體: |||超大  

对weblogic进行配置一般是通过console控制台来进行配置的,但有的时候,需要自己在程序中需要进行动态的配置,比如增加队列,显示队列,或者配置数据源;改写写config.XML,是可以达到动态配置的效果的,但bea不推荐这样做,而且这样做需要重新启动服务器。

怎么样既动态的配置,又不重新启动服务器呢?

笔者查询了weblogic的网站,了解到有两种方法动态的配置(1)可以使用weblogic.Admin命令(文档地址:http://e-docs.bea.com/wls/docs81/pdf/adminguide.pdf),(2)使用weblogic是用jmx编程来进行治理,通过jmx来对weblogic中的组件进行动态的配置。jmx的文档地址:http://e-docs.bea.com/wls/docs81/pdf/jmx.pdf,假如使用这种方法,要将weblogic.jar配置到CLASSPATH环境变量中(因为weblogic的jmx类是放在weblogic.jar中的)

本人写了一份代码,对Queue进行治理,包括JMSQueue的增加,删除,和显示,我的config.xml文件如下:

<JMSServer Name="MessageCenterServer" Store="MyJmsSave"

Targets="myserver" TemporaryTemplate="MyJMSTemplate"

<JMSQueue CreationTime="1092359207895" JNDIName="CenterQueue"

Name="CenterQueue" Template="MyJMSTemplate"/

<JMSQueue CreationTime="1092372641842" JNDIName="que00001"

Name="que00001" Template="MyJMSTemplate"/

<JMSQueue CreationTime="1092372701067" JNDIName="que00002"

Name="que00002" Template="MyJMSTemplate"/

<JMSQueue CreationTime="1093353883216" JNDIName="queue0003" Name="queue0003"/

</JMSServer

代码如下:

package messagecenter;

/**

* <pTitle: 消息中心</p

* <pDescription: 对消息队列进行维护</p

* @author 张荣斌

* @version 1.0

*/

import Java.util.*;

import java.util.regex.Pattern;

import javax.naming.Context;

import weblogic.jndi.Environment;

import weblogic.management.MBeanHome;

import weblogic.management.runtime.ServletRuntimeMBean;

import weblogic.management.runtime.ApplicationRuntimeMBean;

import weblogic.management.runtime.WebAppComponentRuntimeMBean;

import weblogic.management.runtime.ComponentRuntimeMBean;

import weblogic.jms.extensions.*;

import weblogic.management.RemoteMBeanServer;

import javax.management.ObjectName;

import javax.management.QueryEXP;

public class JMSQueueMaintain {

public static final String WEBLOGIC_URL = "t3://localhost:7001";

public static final String WEBLOGIC_USER="system";

public static final String WEBLOGIC_PASSWord = "12345678";

public static final String WEBLOGIC_JMSSERVER = "MessageCenterServer";//JMS服务器的名字,可以看到我的config.xml<JMSServer

Name="MessageCenterServer" Store="MyJmsSave"这一行

public JMSQueueMaintain() {

}

/**

* 得到initial context

*/

private static Context getCtx(String url,String username, String password) throws Exception{

Environment env = new Environment();

env.setProviderUrl(url);

env.setSecurityPrincipal(username);

env.setSecurityCredentials(password);

return env.getInitialContext();

}

/**

* 得到the Admin MBean Home

*/

private static MBeanHome getMBeanHome(String url,String username, String password) throws Exception

{

return (MBeanHome) getCtx(url,username,password).lookup(MBeanHome.ADMIN_JNDI_NAME);

}

/**

* 增加队列

*/

public static void addQueue(String queuename) throws Exception{

Context ctx = getCtx(WEBLOGIC_URL,WEBLOGIC_USER,WEBLOGIC_PASSWORD);

JMSHelper.createPermanentQueueAsync(ctx,WEBLOGIC_JMSSERVER,queuename,queuename);

}

/**

* 删除队列

*/

public static void deleteQueue(String queuename) throws Exception{

Context ctx = getCtx(WEBLOGIC_URL,WEBLOGIC_USER,WEBLOGIC_PASSWORD);

JMSHelper.deletePermanentQueue(ctx,WEBLOGIC_JMSSERVER,queuename);

}

/**

* 得到所有的队列名

*/

public static Vector getQueuenames() throws Exception{

Vector vect = new Vector();

MBeanHome home = getMBeanHome(WEBLOGIC_URL,WEBLOGIC_USER,WEBLOGIC_PASSWORD);

RemoteMBeanServer homeServer = null;

QueryExp query = null;

homeServer = home.getMBeanServer();

Set JMSMBeans = homeServer.queryNames(new ObjectName("mydomain:JMSServer="+WEBLOGIC_JMSSERVER+",Type=JMSQueue,*"),

query);

//where "query" could be any object that implements the JMX

//javax.managementQueryExp

for (Iterator itr = JMSMBeans.iterator(); itr.hasNext(); ) {

ObjectName mbean = (ObjectName)itr.next();

if(!mbean.getKeyProperty("Name").equals("CenterQueue")){

vect.addElement(mbean.getKeyProperty("Name"));

}

}

return vect;

}

public static void main(String[] args) {

JMSQueueMaintain JMSQueueMaintain1 = new JMSQueueMaintain();

try{

System.out.println(JMSQueueMaintain1.getQueuenames());

JMSQueueMaintain1.addQueue("queue0005");

JMSQueueMaintain1.deleteQueue("queue0003");

System.out.println(JMSQueueMaintain1.getQueuenames());

}catch(Exception e){

}

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航