WebLogic Server 9.0 應用配置管理接口

 

WebLogic Server 9.0 應用配置管理接口

  WLS 9可以說是自WLS 6.0以來最重大的一次改革,不僅增加了許多功能,而且對許多舊有的子系統重新進行了設計。爲了集成第三方管理系統,WLS提供了與JMX完全兼容的標準接口來執行所有的管理操作。

  在WLS中,每個Domain中使用一系列Mbean負責維護該Domain的配置。這些MBean對內部的各個子系統是樹狀組織形式。對外則提供JMX作爲訪問接口。此外還有Console,WLST,WLConfig等管理工具利用Mbean來管理Domain。在WLS 9中完全重寫了JMX子系統,從而代替了從6以來的這部分框架。新的JMX框架支持可靠的批量操作,提高性能,並增加對更多J2EE標準的支持。包括 JSR 77,JMX 1.2以及JSR 160等。

  此外WLS 9中包含了另一套和JMX類似的API,稱爲J2EE Management API,用於實現J2EE Management data model。後面我會通過一個例子對J2EE Management API進行更深入的討論。

  本文所有代碼通過WLS 9.0測試,使用Eclipse 3.1.0開發。

  本文沒有包括用JMX管理安全Realm和使用自定義MBean。自定義MBean將在另一篇文章中討論。

  一.JMX功能介紹

  WLS 9中JMX的新特性主要包括如下六方面:

  1。由於JMX新版本中遠程API的發佈(JSR 160),遠程JMX客戶端可以使用標準的JMX remote API 1.0來訪問WLS上的JMX代理。以前可以通過weblogic.management.MBeanHome接口訪問WLS Mbean的類型安全存根接口。在9中這個接口是不贊成的。如果你的JMX客戶端應用中使用了類型安全接口,建議升級到標準的JMX模型上。但如果使用舊的MBeanHome 接口,仍然可以被新的JMX兼容。

  2。新JMX框架中修改Domain配置的過程類似二階段事務提交機制。所有“修改”操作會先臨時保存到Administration Server的Edit MBeans中。然後這些“修改”會發布到各個Server上。如果任何Server不能執行這個修改操作,整個修改過程將回滾。Mbean的發佈使用了 WLS的部署框架,因此部署和配置使用相同的渠道。

  3。新的MBean數據模型。由於整個Domain配置是由一個XML文檔來描述,因此Mbean是以等級結構來表示這個文檔的。每個 Domain有一個類型爲DomainMBean的MBean來表示這個Domain,該Mbean中又有屬性來訪問Domain的Server和 Cluster。當調用一個MBean是通嗨芆bjectName on =

  javax.management.MBeanServerConnection.getAttribute (object-name, attribute);的方法。

  4。新的MBean Server結構。Administration Server必須維護三個MBean server,每個Server用於訪問特定種類的Mbean。Edit MBean Server用於訪問Domain中可編輯配置的Mbean;Domain Runtime MBean Server用於訪問該Domain的所有運行時Mbean和只讀配置的Mbean;Runtime MBean Server用於訪問Administration Server的運行時Mbean和只讀配置的Mbean。對於每個授管Server只維護各自的Runtime MBean Server用於訪問該Server上的運行時Mbean和只讀配置的Mbean。JMX客戶端使用J2EE的標準接口

  javax.remote.access來訪問在上述MBean servers上註冊的Mbean。此外還有第四種作爲可選的:JVM's platform MBean Sever,用於監控JVM的信息,可存在於Administration Server或被管Server。

  5。在WLS 9中,開發者可以把描述應用服務的描述符文件打包到應用的EAR文件中去。因此當部署這個應用時WLS會按照部署描述符文件創建一個實例代表這個服務。因此WLS中許多子系統已經不贊成使用舊的JMX接口轉而升級到新的Mbean。

  6。註冊自定義MBean。以前如果需要註冊自定義Mbean,則需要使用自己的Mbean Server或通過weblogic.management.RemoteMBeanServer接口把MBean註冊到WLS的MBean Server上。對於9c除了可以創建自己的MBean Server外,還可以將自定義Mbean註冊到Runtime MBean server上並通過JNDI訪問,或者將自定義Mbean註冊到JVM's platform MBean server上。

  二.理解WLS Mbean

  按照Mbean是用於監控或配置,分爲Runtime MBeans,Configuration MBeans和Configuration MBeans for system modules。Runtime MBeans只包括Server和其資源的運行時狀況,因此只在Server運行期內存活。Configuration MBeans包括Server和其資源的配置,因此保存在Domain的XML配置文檔中。

  Configuration MBeans for system modules包括各種系統級Service的配置信息。Domain中每個Server有自己的一份Domain配置文件。當該Server啓動後或者做任何改動時會和Administration Server連絡來更新其配置文件。即使啓動時無法和Administration Server連絡,仍舊可以成功啓動。此外對Administration Server,在Domain的config/pending路經下還有一份可編輯的Domain配置文檔拷貝用於保存JMX client端所作的中間修改狀態。

  在WLS中MBean是以樹狀組織的,對應到Domain的XML配置文檔結構。每個Mbean用一個唯一的ObjectName來註冊到 MBean Server上。按照約定,子MBean的ObjectName要包含父Mbean的ObjectName中的一部分:

 

  1. com.bea:Name=name,Type=type[,TypeOfParentMBean=NameOfParentMBean]  
  2. [,TypeOfParentMBean1=NameOfParentMBean1]...  

 

  其中com.bea是這個Mbean所屬於的JMX Domain名。對於非自定義的MBean,這個Domain Name總是com.bea。此後這些屬性名值對的次序無所謂。JMX允許從本地或遠程訪問MBean Server。如果從本地訪問,JMX客戶端可以通過JNDI獲得javax.management.MBeanServer接口,並還可以訪問創建註冊自定義的MBean。如果遠程訪問需要包含WL_HOME/lib/wljmxclient.jar包,獲得 javax.management.MBeanServerConnection接口,但不能操作自定義Mbean。

  三.使用JMX的例子

  下面用一個例子來說明如何使用JMX訪問Mbean來修改Domain配置。

  1. import java.io.IOException;  
  2. import java.net.MalformedURLException;  
  3. import java.util.Hashtable;  
  4. import java.util.Map;  
  5. import javax.management.Attribute;  
  6. import javax.management.MBeanServerConnection;  
  7. import javax.management.ObjectName;  
  8. import javax.management.remote.JMXConnector;  
  9. import javax.management.remote.JMXConnectorFactory;  
  10. import javax.management.remote.JMXServiceURL;  
  11. import javax.naming.Context;  
  12. public class JMXSample {  
  13. // JMXSample class definition - do not copy this line   
  14. private static String USERNAME = "weblogic";  
  15. private static String PASSWORD = "weblogic";  
  16. private static String PROTOCOL = "t3";  
  17. private static String HOSTNAME = "localhost";  
  18. private static int PORT = 7001;  
  19. private static String JNDI = "/jndi/";  
  20. private static String RUNTIME_URI = "weblogic.management.mbeanservers.runtime";  
  21. private static String EDIT_URI = "weblogic.management.mbeanservers.edit";  
  22. private static String RUNTIME_SERVICE = "com.bea:Name=RuntimeService,Type=weblogic.management.mbeanservers.runtime  
  23. .RuntimeServiceMBean";  
  24. private static String EDIT_SERVICE = "com.bea:Name=EditService,Type=weblogic.management.mbeanservers.edit  
  25. .EditServiceMBean";  
  26. private MBeanServerConnection runtimeServiceConnection = null;  
  27. private MBeanServerConnection editServiceConnection = null;  
  28. public static void main(String[] args) {  
  29. JMXSample jmx = new JMXSample();  
  30. jmx.runtests();  
  31. }  
  32. //連到特定Server的特定MBean Server上。   
  33. //WLS9支持JDK 1.5,所以我這裏用上了Tiger的新特性,泛型   
  34. public MBeanServerConnection getConnection(String URI) throws IOException,  
  35. MalformedURLException {  
  36. //描述MBean Server的地址   
  37. JMXServiceURL serviceURL = new JMXServiceURL(PROTOCOL, HOSTNAME, PORT, JNDI + URI);  
  38. Hashtable h = new Hashtable();  
  39. h.put(Context.SECURITY_PRINCIPAL, USERNAME);  
  40. h.put(Context.SECURITY_CREDENTIALS, PASSWORD);  
  41. h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES, "weblogic.management.remote");  
  42. //構造JMXConnector對象。   
  43. JMXConnector connector = JMXConnectorFactory.connect(serviceURL, (Map)h);  
  44. //連接到MBean Server   
  45. MBeanServerConnection connection = connector.getMBeanServerConnection();  
  46. return connection;  
  47. }  
  48. //包括兩類操作,前面一部分是讀取Domain配置,後面一部分是修改Domain配置。   
  49. public void runtests() {  
  50. try {  
  51. runtimeServiceConnection = getConnection(RUNTIME_URI);  
  52. editServiceConnection = getConnection(EDIT_URI);  
  53. ObjectName runtimeON = new ObjectName(RUNTIME_SERVICE);  
  54. ObjectName editON = new ObjectName(EDIT_SERVICE);  
  55. // 獲得 server   
  56. ObjectName server = (ObjectName) runtimeServiceConnection  
  57. .getAttribute(runtimeON, "ServerConfiguration");  
  58. // 獲得並顯示當前 server 名   
  59. System.out.println("SERVER NAME "  
  60. + runtimeServiceConnection.getAttribute(runtimeON,  
  61. "ServerName"));  
  62. // 獲得並顯示 domain 名   
  63. ObjectName domain = (ObjectName) runtimeServiceConnection  
  64. .getAttribute(runtimeON, "DomainConfiguration");  
  65. System.out.println("DOMAIN NAME "  
  66. + runtimeServiceConnection.getAttribute(domain, "Name"));  
  67. // Since we have the server already we will just reuse it to   
  68. // 獲得並顯示當前監聽端口   
  69. System.out.println("LISTEN PORT "  
  70. + runtimeServiceConnection.getAttribute(server,  
  71. "ListenPort").toString());  
  72. // 獲得並顯示SSL端口   
  73. ObjectName ssl = (ObjectName) runtimeServiceConnection  
  74. .getAttribute(server, "SSL");  
  75. System.out.println("SSL LISTEN PORT "  
  76. + runtimeServiceConnection.getAttribute(ssl, "ListenPort")  
  77. .toString());  
  78. // 獲得並顯示生產模式   
  79. System.out.println("PRODUCTION MODE ENABLED "  
  80. + runtimeServiceConnection.getAttribute(domain,  
  81. "ProductionModeEnabled").toString());  
  82. //獲得並顯示當前部署的所有應用   
  83. ObjectName[] apps = (ObjectName[]) runtimeServiceConnection  
  84. .getAttribute(domain, "AppDeployments");  
  85. for (ObjectName app : apps) {  
  86. System.out.println("App Deployment : "  
  87. + runtimeServiceConnection.getAttribute(app, "Name")  
  88. .toString());  
  89. }  
  90. ObjectName mgrOn = (ObjectName) editServiceConnection.getAttribute(  
  91. editON, "ConfigurationManager");  
  92. //對Edit MBean的事務控制包括startEdit,save,activate等。這些方法都必須通過類反射來執行。   
  93. //第一個參數表示如果調用startEdit時候等待獲得鎖的時間。第二個參數表示如果2分鐘之內沒完成所有操作,則自動失去鎖。   
  94. Object[] params = new Object[] { new Integer(60000),  
  95. new Integer(120000) };  
  96. String[] paramTypes = new String[] { "java.lang.Integer",  
  97. "java.lang.Integer" };  
  98. //start edit將返回一個domain MBean的句柄,可認爲是整個配置樹的根。   
  99. ObjectName domainMgr = (ObjectName) editServiceConnection.invoke(mgrOn,  
  100. "startEdit", params, paramTypes);  
  101. if (domainMgr == null) {  
  102. // 無法在規定時間內獲得鎖   
  103. throw new Exception("Somebody else is editing already");  
  104. }  
  105. //對domain MBean的notes屬性做一個修改   
  106. Attribute notes = new Attribute("Notes""Blah Blah Blah");  
  107. editServiceConnection.setAttribute(domainMgr, notes);  
  108. //列出未保存的修改   
  109. Object[] unsavedList = (Object[])editServiceConnection.getAttribute(mgrOn, "Changes");  
  110. for(Object o:unsavedList)  
  111. System.out.println("Unsaved change: " + o.toString());  
  112. //如果不保存,則可執行"undo"操作   
  113. editServiceConnection.invoke(mgrOn, "save"nullnull);  
  114. //列出未激活的修改   
  115. Object[] unactivatedList = (Object[])editServiceConnection.getAttribute(mgrOn, "Changes");  
  116. for(Object o:unactivatedList)  
  117. System.out.println("Unactivated change: " + o.toString());  
  118. //激活這個修改   
  119. params = new Object[]{new Long(120000)};  
  120. paramTypes = new String[]{"java.lang.Long"};  
  121. ObjectName taskOn =  
  122. (ObjectName) editServiceConnection.invoke(mgrOn, "activate", params, paramTypes);  
  123. //列出已經激活的修改   
  124. Object[] activatedList=(Object[])editServiceConnection.getAttribute(taskOn, "Changes");  
  125. for(Object o:activatedList)  
  126. System.out.println("Activated change: " + o.toString());  
  127. //最近激活的任務。WLS默認保存最近10筆激活的任務歷史   
  128. for(Object o:activatedList)  
  129. System.out.println("Activated change: " + o.toString());  
  130. ObjectName[] completedObjects=(ObjectName[])editServiceConnection.getAttribute(mgrOn,  
  131. "CompletedActivationTasks");  
  132. for(ObjectName on:completedObjects){  
  133. System.out.println("User who started activation: " +editServiceConnection.getAttribute(on, "User"));  
  134. System.out.println("Task state:" +editServiceConnection.getAttribute(on, "State"));  
  135. System.out.println("Start time:" +editServiceConnection.getAttribute(on, "StartTime"));  
  136. Object[] completedList=(Object[])editServiceConnection.getAttribute(on, "Changes");  
  137. for(Object o:completedList)  
  138. System.out.println("Changes activated: " + o.toString());  
  139. }  
  140. //清除已經完成的激活的任務   
  141. editServiceConnection.invoke(mgrOn, "purgeCompletedActivationTasks"nullnull);  
  142. // 由於Edit屬性是異步的,在此我們等待操作完成。   
  143. params = new Object[]{new Long(120000)};  
  144. paramTypes = new String[]{"java.lang.Long"};  
  145. editServiceConnection.invoke(taskOn, "waitForTaskCompletion", params, paramTypes);  
  146. catch (Throwable t) {  
  147. t.printStackTrace();  
  148. }  
  149. }  
  150. }   

 

  四.使用J2EE Management API

  在Domain中所有資源可以用J2EE Managed Object (JMO)來表示。所有這些JMO被數據模型組織成樹狀結構,其中根JMO稱爲J2EEDomain。每個JMO對象通過一個 javax.management.ObjectName實例來表示它的唯一對象名,形如:

  domain:j2eeType=value,name=value,parent-j2eeType[,property=value]*。

  JAVA應用可以通過Management Enterprise Java Bean (MEJB)的遠程接口javax.management.j2ee.Management來訪問Administration Server上的JMO。事實上JMO只是MBean的另一種包裝,因此任何MBean的變化會及時反映到對應的JMO上。這組接口實現了J2EE Management Specification 1.0(JSR-77)的必選功能,而諸如性能統計,通知服務,用於集成SNMP的Management Information Base(MIB),向Common Information Model (CIM)的映射等可選功能在目前版本中沒有實現。通過J2EE Management API,開發者可以流覽當前Domain的所有資源。下面這個例子通過J2EE Management API遍歷當前Domain所有資源,並把他們的ObjectName打印出來。

  1. import java.io.IOException;  
  2. import java.net.MalformedURLException;  
  3. import java.util.Iterator;  
  4. import java.util.Set;  
  5. import java.util.Properties;  
  6. import javax.management.j2ee.Management;  
  7. import javax.management.j2ee.ManagementHome;  
  8. import javax.management.AttributeNotFoundException;  
  9. import javax.management.InstanceNotFoundException;  
  10. import javax.management.ObjectName;  
  11. import javax.management.QueryExp;  
  12. import javax.naming.Context;  
  13. import javax.naming.InitialContext;  
  14. import javax.naming.NamingException;  
  15. import javax.ejb.CreateException;  
  16. public class GetJMONames {  
  17. static String url = "t3://localhost:8001";  
  18. static String user = "weblogic";  
  19. static String password = "weblogic";  
  20. public static void main(String[] args) {  
  21. try {  
  22. getAllJMONames();  
  23. catch (Exception e) {  
  24. System.out.println(e);  
  25. }  
  26. }  
  27. public static Context getInitialContext() throws NamingException {  
  28. Properties p = new Properties();  
  29. p.put(Context.INITIAL_CONTEXT_FACTORY,  
  30. "weblogic.jndi.WLInitialContextFactory");  
  31. p.put(Context.PROVIDER_URL, url);  
  32. if (user != null) {  
  33. p.put(Context.SECURITY_PRINCIPAL, user);  
  34. if (password == null)  
  35. password = "";  
  36. p.put(Context.SECURITY_CREDENTIALS, password);  
  37. }  
  38. return new InitialContext(p);  
  39. }  
  40. //通過JNDI獲得javax.management.j2ee.ManagementHome接口,並構造MEJB的遠程接口實例。   
  41. public static Management getMEJBRemote() throws IOException,  
  42. MalformedURLException, NamingException, CreateException {  
  43. Context context = getInitialContext();  
  44. ManagementHome home = (ManagementHome) context.lookup("ejb.mgmt.MEJB");  
  45. Management bean = home.create();  
  46. return bean;  
  47. }  
  48. public static void getAllJMONames() {  
  49. try {  
  50. Management rhome = getMEJBRemote();  
  51. String string = "";  
  52. ObjectName name = new ObjectName(string);  
  53. QueryExp query = null;  
  54. Set allNames = rhome.queryNames(name, query);  
  55. Iterator nameIterator = allNames.iterator();  
  56. while (nameIterator.hasNext()) {  
  57. ObjectName on = (ObjectName) nameIterator.next();  
  58. System.out.println(on.getCanonicalName() + "/n");  
  59. }  
  60. catch (Exception ex) {  
  61. ex.printStackTrace();  
  62. }  
  63. }  
  64. }   

  五.常用參數參考

  和上述三個MBean Server對應的是訪問他們分別訪問各自MBean的URI路經:

  Runtime MBean Server:

  "/jndi/" + "weblogic.management.mbeanservers.runtime"

  Edit MBean Server:

  "/jndi/" + "weblogic.management.mbeanservers.edit"

  Domain Runtime MBean Server:

  "/jndi/" + "weblogic.management.mbeanservers.domainruntime"

  以及分別獲得三個MBean Server上根服務的名稱:

  weblogic:Name=RuntimeService

  weblogic:Name=EditService

  weblogic:Name=DomainRuntimeService

  此外還有一個服務用於獲得配置管理 器:

  weblogic:Name=ConfigurationManager


  六.相關文檔

  JMX: http://java.sun .com/products/JavaManagement/.

  JMX 1.2 specification: http://jcp.org/aboutJava/communityprocess/final/jsr003/index3.html

  JMX Remote API 1.0 specification: http://jcp.org/aboutJava/communityprocess/final/jsr160/index.html

  javax.management* packages: http://java.sun.com/j2se/1.5.0/docs/api/overview- summary.html

  Developing Applications with JMX:http://e-docs.bea.com/wls/docs90/jmx/index.html

  JAVA Management API:http://e-docs.bea.com/wls/docs90/j2eemanage/index.html

  The Web Logic Server MBean Reference:http://e- docs.bea.com/wls/docs90/wlsmbeanref/core/index.html

  關於作者

  goblinize, 來自BEA, Matrix Bea產品版版主( http://www.matrix.org.cn/topic.shtml?forumId=28 );歡迎訪問作者的Blog: http://www.matrix.org.cn/blog/goblinize

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章