Username: Password:

使用jmx对weblogic进行动态的配置(源代码)
来源:linux宝库作者:linux宝库 发布时间:2007-09-30 00:00:00


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

  }

  }

  }

  

喜欢本文,那就收藏到:

    Del.icio.us Google书签 Digg Live Bookmark Technorati Furl Yahoo书签 Facebook 百度搜藏 新浪ViVi 365Key网摘 天极网摘 和讯网摘 博拉网 POCO网摘 添加到饭否 QQ书签 Digbuzz我挖网
相关评论  我也要评论
还没有关于此文章的相关评论!
  • 昵称: (为空则显示guest)
  • 评论分数: ★ ★ ★★★ ★★★★ ★★★★★
  • 评论内容:(不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
  • 导航
    赞助商
    文章类别
    订阅