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

来源:互联网 发布:c 会员管理系统源码 编辑:程序博客网 时间:2024/05/22 01:40

  对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文件如下:
??? ??????? Targets="myserver" TemporaryTemplate="MyJMSTemplate">
??????? ??????????? Name="CenterQueue" Template="MyJMSTemplate"/>
??????? ??????????? Name="que00001" Template="MyJMSTemplate"/>
??????? ??????????? Name="que00002" Template="MyJMSTemplate"/>
???????
???

代码如下:
package messagecenter;

/**
?*

Title: 消息中心


?*

Description: 对消息队列进行维护


?* @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

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){

??? }
? }

}

原创粉丝点击