服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

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


  对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;
  
  /**
  * <p>title: 消息中心</p>
  * <p>description: 对消息队列进行维护</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){
  
  }
  }
  
  }

扫描关注微信公众号