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中的一部分:
- com.bea:Name=name,Type=type[,TypeOfParentMBean=NameOfParentMBean]
- [,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配置。
- import java.io.IOException;
- import java.net.MalformedURLException;
- import java.util.Hashtable;
- import java.util.Map;
- import javax.management.Attribute;
- import javax.management.MBeanServerConnection;
- import javax.management.ObjectName;
- import javax.management.remote.JMXConnector;
- import javax.management.remote.JMXConnectorFactory;
- import javax.management.remote.JMXServiceURL;
- import javax.naming.Context;
- public class JMXSample {
- // JMXSample class definition - do not copy this line
- private static String USERNAME = "weblogic";
- private static String PASSWORD = "weblogic";
- private static String PROTOCOL = "t3";
- private static String HOSTNAME = "localhost";
- private static int PORT = 7001;
- private static String JNDI = "/jndi/";
- private static String RUNTIME_URI = "weblogic.management.mbeanservers.runtime";
- private static String EDIT_URI = "weblogic.management.mbeanservers.edit";
- private static String RUNTIME_SERVICE = "com.bea:Name=RuntimeService,Type=weblogic.management.mbeanservers.runtime
- .RuntimeServiceMBean";
- private static String EDIT_SERVICE = "com.bea:Name=EditService,Type=weblogic.management.mbeanservers.edit
- .EditServiceMBean";
- private MBeanServerConnection runtimeServiceConnection = null;
- private MBeanServerConnection editServiceConnection = null;
- public static void main(String[] args) {
- JMXSample jmx = new JMXSample();
- jmx.runtests();
- }
- //連到特定Server的特定MBean Server上。
- //WLS9支持JDK 1.5,所以我這裏用上了Tiger的新特性,泛型
- public MBeanServerConnection getConnection(String URI) throws IOException,
- MalformedURLException {
- //描述MBean Server的地址
- JMXServiceURL serviceURL = new JMXServiceURL(PROTOCOL, HOSTNAME, PORT, JNDI + URI);
- Hashtable h = new Hashtable();
- h.put(Context.SECURITY_PRINCIPAL, USERNAME);
- h.put(Context.SECURITY_CREDENTIALS, PASSWORD);
- h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES, "weblogic.management.remote");
- //構造JMXConnector對象。
- JMXConnector connector = JMXConnectorFactory.connect(serviceURL, (Map)h);
- //連接到MBean Server
- MBeanServerConnection connection = connector.getMBeanServerConnection();
- return connection;
- }
- //包括兩類操作,前面一部分是讀取Domain配置,後面一部分是修改Domain配置。
- public void runtests() {
- try {
- runtimeServiceConnection = getConnection(RUNTIME_URI);
- editServiceConnection = getConnection(EDIT_URI);
- ObjectName runtimeON = new ObjectName(RUNTIME_SERVICE);
- ObjectName editON = new ObjectName(EDIT_SERVICE);
- // 獲得 server
- ObjectName server = (ObjectName) runtimeServiceConnection
- .getAttribute(runtimeON, "ServerConfiguration");
- // 獲得並顯示當前 server 名
- System.out.println("SERVER NAME "
- + runtimeServiceConnection.getAttribute(runtimeON,
- "ServerName"));
- // 獲得並顯示 domain 名
- ObjectName domain = (ObjectName) runtimeServiceConnection
- .getAttribute(runtimeON, "DomainConfiguration");
- System.out.println("DOMAIN NAME "
- + runtimeServiceConnection.getAttribute(domain, "Name"));
- // Since we have the server already we will just reuse it to
- // 獲得並顯示當前監聽端口
- System.out.println("LISTEN PORT "
- + runtimeServiceConnection.getAttribute(server,
- "ListenPort").toString());
- // 獲得並顯示SSL端口
- ObjectName ssl = (ObjectName) runtimeServiceConnection
- .getAttribute(server, "SSL");
- System.out.println("SSL LISTEN PORT "
- + runtimeServiceConnection.getAttribute(ssl, "ListenPort")
- .toString());
- // 獲得並顯示生產模式
- System.out.println("PRODUCTION MODE ENABLED "
- + runtimeServiceConnection.getAttribute(domain,
- "ProductionModeEnabled").toString());
- //獲得並顯示當前部署的所有應用
- ObjectName[] apps = (ObjectName[]) runtimeServiceConnection
- .getAttribute(domain, "AppDeployments");
- for (ObjectName app : apps) {
- System.out.println("App Deployment : "
- + runtimeServiceConnection.getAttribute(app, "Name")
- .toString());
- }
- ObjectName mgrOn = (ObjectName) editServiceConnection.getAttribute(
- editON, "ConfigurationManager");
- //對Edit MBean的事務控制包括startEdit,save,activate等。這些方法都必須通過類反射來執行。
- //第一個參數表示如果調用startEdit時候等待獲得鎖的時間。第二個參數表示如果2分鐘之內沒完成所有操作,則自動失去鎖。
- Object[] params = new Object[] { new Integer(60000),
- new Integer(120000) };
- String[] paramTypes = new String[] { "java.lang.Integer",
- "java.lang.Integer" };
- //start edit將返回一個domain MBean的句柄,可認爲是整個配置樹的根。
- ObjectName domainMgr = (ObjectName) editServiceConnection.invoke(mgrOn,
- "startEdit", params, paramTypes);
- if (domainMgr == null) {
- // 無法在規定時間內獲得鎖
- throw new Exception("Somebody else is editing already");
- }
- //對domain MBean的notes屬性做一個修改
- Attribute notes = new Attribute("Notes", "Blah Blah Blah");
- editServiceConnection.setAttribute(domainMgr, notes);
- //列出未保存的修改
- Object[] unsavedList = (Object[])editServiceConnection.getAttribute(mgrOn, "Changes");
- for(Object o:unsavedList)
- System.out.println("Unsaved change: " + o.toString());
- //如果不保存,則可執行"undo"操作
- editServiceConnection.invoke(mgrOn, "save", null, null);
- //列出未激活的修改
- Object[] unactivatedList = (Object[])editServiceConnection.getAttribute(mgrOn, "Changes");
- for(Object o:unactivatedList)
- System.out.println("Unactivated change: " + o.toString());
- //激活這個修改
- params = new Object[]{new Long(120000)};
- paramTypes = new String[]{"java.lang.Long"};
- ObjectName taskOn =
- (ObjectName) editServiceConnection.invoke(mgrOn, "activate", params, paramTypes);
- //列出已經激活的修改
- Object[] activatedList=(Object[])editServiceConnection.getAttribute(taskOn, "Changes");
- for(Object o:activatedList)
- System.out.println("Activated change: " + o.toString());
- //最近激活的任務。WLS默認保存最近10筆激活的任務歷史
- for(Object o:activatedList)
- System.out.println("Activated change: " + o.toString());
- ObjectName[] completedObjects=(ObjectName[])editServiceConnection.getAttribute(mgrOn,
- "CompletedActivationTasks");
- for(ObjectName on:completedObjects){
- System.out.println("User who started activation: " +editServiceConnection.getAttribute(on, "User"));
- System.out.println("Task state:" +editServiceConnection.getAttribute(on, "State"));
- System.out.println("Start time:" +editServiceConnection.getAttribute(on, "StartTime"));
- Object[] completedList=(Object[])editServiceConnection.getAttribute(on, "Changes");
- for(Object o:completedList)
- System.out.println("Changes activated: " + o.toString());
- }
- //清除已經完成的激活的任務
- editServiceConnection.invoke(mgrOn, "purgeCompletedActivationTasks", null, null);
- // 由於Edit屬性是異步的,在此我們等待操作完成。
- params = new Object[]{new Long(120000)};
- paramTypes = new String[]{"java.lang.Long"};
- editServiceConnection.invoke(taskOn, "waitForTaskCompletion", params, paramTypes);
- } catch (Throwable t) {
- t.printStackTrace();
- }
- }
- }
四.使用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打印出來。
- import java.io.IOException;
- import java.net.MalformedURLException;
- import java.util.Iterator;
- import java.util.Set;
- import java.util.Properties;
- import javax.management.j2ee.Management;
- import javax.management.j2ee.ManagementHome;
- import javax.management.AttributeNotFoundException;
- import javax.management.InstanceNotFoundException;
- import javax.management.ObjectName;
- import javax.management.QueryExp;
- import javax.naming.Context;
- import javax.naming.InitialContext;
- import javax.naming.NamingException;
- import javax.ejb.CreateException;
- public class GetJMONames {
- static String url = "t3://localhost:8001";
- static String user = "weblogic";
- static String password = "weblogic";
- public static void main(String[] args) {
- try {
- getAllJMONames();
- } catch (Exception e) {
- System.out.println(e);
- }
- }
- public static Context getInitialContext() throws NamingException {
- Properties p = new Properties();
- p.put(Context.INITIAL_CONTEXT_FACTORY,
- "weblogic.jndi.WLInitialContextFactory");
- p.put(Context.PROVIDER_URL, url);
- if (user != null) {
- p.put(Context.SECURITY_PRINCIPAL, user);
- if (password == null)
- password = "";
- p.put(Context.SECURITY_CREDENTIALS, password);
- }
- return new InitialContext(p);
- }
- //通過JNDI獲得javax.management.j2ee.ManagementHome接口,並構造MEJB的遠程接口實例。
- public static Management getMEJBRemote() throws IOException,
- MalformedURLException, NamingException, CreateException {
- Context context = getInitialContext();
- ManagementHome home = (ManagementHome) context.lookup("ejb.mgmt.MEJB");
- Management bean = home.create();
- return bean;
- }
- public static void getAllJMONames() {
- try {
- Management rhome = getMEJBRemote();
- String string = "";
- ObjectName name = new ObjectName(string);
- QueryExp query = null;
- Set allNames = rhome.queryNames(name, query);
- Iterator nameIterator = allNames.iterator();
- while (nameIterator.hasNext()) {
- ObjectName on = (ObjectName) nameIterator.next();
- System.out.println(on.getCanonicalName() + "/n");
- }
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- }
五.常用參數參考
和上述三個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