筆者查詢了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){
}
}
}