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

你可能感兴趣的:(配置管理,Weblogic,应用服务器,Bean,JVM)