Use JMX to dynamically configure it for WebLogic (source code)

xiaoxiao2021-03-06  58

Configuring WebLogic is generally configured by the Console console, but sometimes you need to make dynamic configurations in the program, such as adding queues, display queues, or configuring data sources; rewrote writing config.xml, is The effect of dynamic configuration can be reached, but the BEA does not recommend this, and doing so need to restart the server. How is it dynamic configuration, don't restart the server? The author queries WebLogic website and learned that there are two ways to dynamically configure (1) can use the weblogic.admin command (document address: http://e-docs.bea.com/wls/docs81/pdf/adminguide.pdf ), (2) Use WebLogic to be programmed with JMX, and dynamically configure the components in WebLogic by JMX. JMX document address: http://e-docs.bea.com/wls/docs81/pdf/jmx.pdf, if you use this method, you should configure WebLogic.jar into the ClassPath environment variable (because WebLogic JMX class Is put in WebLogic.jar)

I wrote a code, manage Queue, including JMSQueue, delete, and display, my config.xml file is as follows:???

??????? Targets = "myserver" TempoRyTemplate = "MyjmsTemplate">

???????

??????????? name = "centerqueue" template = "myjmstemplate" />

???????

??????????? name = "que00001" template = "myjmstemplate" />

???????

??????????? name = "que00002" Template = "myjmstemplate" />

???????

???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

The code is as follows: package messagecenter;

/ **? *

Title: Message Center

? *

Description: Maintaining the message queue

? * @Author Zhang Rongbin

? * @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.Runtime.ApplicationRuntimeMbean;

Import Weblogic.Management.Runtime.WebappcomponenTruntimembe

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 server name, you can see my config.xml

Name = "MessageCenterServer" store = "myjmssave" this line?

? Public JMSQueueMaintain () {?}? / ** ?? * to obtain 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 ();?}? / **? * get it 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) ;? }? / ** ?? * Add queue ?? * /? Public static void address {????? context ctx = getctX (WebLogic_URL, WebLogic_User, WebLogic_Password); ????? jmshelper. createPermanentQueueAsync (ctx, WEBLOGIC_JMSSERVER, queuename, queuename) ;?}? / ** ?? * delete queue ?? * /? public static void deleteQueue (String queuename) throws Exception {??? Context ctx = getCtx (WEBLOGIC_URL, WEBLOGIC_USER, Weblogic_password); ??? jmshelper.deleteper ManentQueue (CTX, WebLogic_JMSSERVER, Queuename);?}? / ** ?? * Get all queues ?? * /? 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 ("Mydomain: jmsserver =" WebLogic_JMSSERVER ", TYPE = JMSQUE, *"), Query); ??? // where "query" could be any Object what imports 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 ) {

???}?}

}

转载请注明原文地址:https://www.9cbs.com/read-118615.html

New Post(0)