WebLogic's Classloading Framework
Running any application on a JVM or an application server (e.g., WebLogic Server), the main question a designer faces is:- Which class is getting loaded from which source
Classloader Hierarchy and Visibility
Classloader hierarchy plays an important role when locating and loading classes into memory. There are two important points to remember:- Classloaders follow the delegation model when loading classes (i.e., current classloader asks its parent for the class if a class is not found in the cache).
- A child classloader loads a class only if its parent fails to load it (i.e., even if both parent and child classloaders have access to a particular class, and the child classloader receives a request to load the class, it is the parent that actually loads the class).
- See customization section for an exception that is supported by WebLogic Server to override this default behavior by setting the prefer-web-inf-classes element to true in the weblogic.xmldescriptor file.
- See customization section for an exception that is supported by WebLogic Server to override this default behavior by setting the prefer-web-inf-classes element to true in the weblogic.xmldescriptor file.
The visibility of a class definition is determined by the following rules:
- Any classes loaded by a classloader are visible directly or indirectly, to all its descendants.
- A parent classloader cannot see any class loaded by any of its child classloaders
- A classloader cannot access any classes loaded by a sibling classloader.
Java Classloader Hierarchy
The bootstrap classloader is the root of the Java classloader hierarchy. The details of Java classloader hierarchy is described below:- Bootstrap classloader (root)
- Created by the JVM for loading its internal classes and the java.* packages (i.e., core Java libraries under
/lib directory) included within the JVM - Written in native code
- Created by the JVM for loading its internal classes and the java.* packages (i.e., core Java libraries under
- Extensions classloader (child of bootstrap classloader)
- Loads any JARs placed in the extensions directory (
/lib/ext or any other directory specified by thejava.ext.dirs system property) of the JDK - Implemented by the sun.misc.Launcher$ExtClassLoader class
- Loads any JARs placed in the extensions directory (
- System classloader (child of extensions classloader)
- Loads code found on java.class.path, which maps to the system CLASSPATH variable.
- Implemented by the sun.misc.Launcher$AppClassLoader class
- Any custom classloader created by an application, including WebLogic's classloaders, are all descendants of this system classpath classloader.
WebLogic's Classloading Framework
WebLogic's standard classloading framework needs to achieve two main goals:- Maintain application independence
- Classes used by application A must never come in conflict with any classes used by application B
- Redeploying application A must have no effect on classes used by application B
- Hot-deploy or hot-redeploy
- Within an application, it must allow you to redeploy web applications without having to redeploy the EJBs
- It is more common to change JSP files and servlets than to change the EJB tier. With proper design, a separate classloader can be created for each servlet and JSP page. This allows you to reload individual servlets and JSPs easily, without the need for redeploying the web application or affecting any of the EJBs.
Java classloaders do not have any standard mechanism to undeploy or unload a set of classes, nor can they load new versions of classes. To achieve the second goal, each application in WebLogic Server has a hierarchy of classloaders (see the Figure below) that are offspring of the system classloader. These hierarchies allow applications or parts of applications to be individually reloaded without affecting the rest of the system. To find out more details on this, read WebLogic Server Application Classloading.
Customization
Even with good support from either Java classloading framework or WebLogic's application classloading framework, it often comes times that you need to have better control over which modules are reloadable, which classes are visible between modules, etc.There are multiple solutions to your customization needs:
- You can configure a web application classloader so that it doesn't use the default parent delegation scheme by setting the prefer-web-inf-classes element to true in the weblogic.xml descriptor file. See details here.
- The FilteringClassLoader provides a mechanism for you to configure deployment descriptors to explicitly specify that certain packages should always be loaded from the application, rather than being loaded by the system classloader. See details here.
- You can create custom classloader hierarchies for an application allowing for better control over class visibility and reloadability. You achieve this by defining a classloader-structure element in the weblogic-application.xml deployment descriptor file. See details here.
Wrap-up
More often than not, you want class definitions (which are stable) shared across applications. To facilitate sharing, you would place them at higher level of the classloading hierarchy (for example, getting loaded at system classloader instead of at application classloader).If it is common to change some modules, a separate classloader can be created for them and place them at the tip of the classloading tree. This allows you to reload individual modules easily, without the need for redeploying their parent applications.
References
posted on 2011-08-16 16:49 gembin 閱讀(570) 評論(0) 編輯 收藏 所屬分類: JavaEE 、Oracle