`

weblogic class loader

阅读更多
WebLogic类加载, 遇到问题两天了,今天终于解决了,谢谢下面作者.

http://www.dev2dev.co.kr/pub/a/2002/10/musser.jsp

引用:

Making the Most of WebLogic Classloaders

by John Musser
2002/10/29

As a Java developer, have you ever found yourself running into what might be politely called 'issues' related to the CLASSPATH and class loading? If you haven't, you're one of the few. This is one of the most notorious sources of developer aggravation in Java development. Now J2EE has added a new set of wrinkles to this phenomenon.

This article dispels some of the mystery of what's going on behind the classloader curtain and provides insights into how you can use this knowledge to your advantage when designing, packaging, and deploying your WebLogic Server applications. Important items we'll cover include a refresher on class-loading fundamentals you need to be aware of; how WebLogic's own custom classloaders build on these basics; the often misunderstood relationship between classloader hierarchies and EARs, EJBs, and WARs; techniques for packaging and deploying utility classes; and a simple but handy tool for diagnosing how classes are loaded within your applications.

Quick Classloader Review
Let's begin with a short review of how classloaders work: Classes are loaded when new bytecode is executed for the first time, either via a new class instance as in "MyClass x = new MyClass();" or by a first reference made to a static class, "MyStatic.staticMethod();". When this occurs, the JVM relies on classloaders - either the standard Java classloaders that ship with the runtime or custom classloaders - to load the bytecode in memory (all classloaders are subclasses of java.lang.ClassLoader). While there's a standard format for how the class is loaded in memory, where a classloader loads the bytecode from, be it from the file system (your typical .class files), an archive (JARs), a network socket (think applets and RMI), or even generated on the fly, is left to each classloader.

Nonetheless, there are certain rules all classloaders follow, many of which can ultimately impact your WebLogic applications. In particular, they:

  • Are hierarchical: As of JDK 1.2 all classloaders exist in parent-child relationships. Each classloader, other than the initial loader, has a parent classloader. The root parent classloader, built into the virtual machine, is called the "bootstrap" (or "primordial") classloader.
  • Can have siblings: Given that any classloader may have multiple child classloaders, it's possible for these children to have sibling classloaders at their same level in the hierarchy.
  • Delegate load requests: Before attempting to load classes themselves, children delegate all load requests to their parent. And each parent will delegate to its parent until reaching the root of the hierarchy. Only if any parent does not first resolve a request does a child attempt to load the requested class.
  • Have visibility constraints: Child classloaders, and the classes loaded by these loaders, have access to (or can "see") classes loaded by their parents, but parents cannot access classes loaded by their children nor can children see classes loaded by sibling classloaders (as with delegation, this only goes up the hierarchy, not down).
  • Cannot unload classes: Classes cannot be unloaded directly by their classloader, but classloaders themselves can be unloaded. This effectively unloads any classes loaded by that classloader. More about this soon.


Classloaders in a WebLogic Application
In order to support the J2EE standard and useful features such as hot deployment, WebLogic Server defines and initializes a number of its own custom classloaders. These classloaders work under the covers within the application server to manage loading and unloading your classes, EARs, EJBs, and WARs (and if you're interested in writing your own specialized classloader, you might want to take a look at Philip Aston's excellent article on this in April's WLDJ [Vol. 1, issue 4]).

Always keep in mind that class loading and deployment are inseparable. When you deploy your modules to WebLogic, each is typically loaded in its own dynamic classloader. If you're deploying a single EJB JAR file, it will get its own classloader. When you deploy a single WAR file, it gets one too. And when deploying an EAR file, WebLogic will create, depending on what's in your EAR file, an entire hierarchy of classloaders.

How does this work? What rules does WebLogic follow? The principal concept is that of application deployment units such as an EAR, an EJB, or a WAR. When any of these are deployed separately they are loaded in separate sibling classloaders. If EJBs and WARs are grouped into an EAR, they get a hierarchy of classloaders: at the top is the application classloader, which loads the EJBs and JARs referenced in its manifest file's classpath (more on this later), and then classloaders for each WAR file.

Figure 1 illustrates a sample WebLogic Server deployment. Each shadowed box in the WebLogic section represents an independent classloader instance. At the top level we have two EARs, one EJB JAR, and one WAR. Each of these is loaded by a sibling classloader. Within the first EAR are three EJB JARs (each with potentially multiple EJBs) and manifest-referenced utility files that share a single classloader and two Web applications that each get another classloader. Because of the established classloader hierarchy, the JSPs, servlets, and utility classes in both WARs can directly use any of those EJBs. In the second EAR, a simpler hierarchy allows the same interactions as the first, but these components are isolated from those in the first EAR.

<!----><!---->1<!---->


In this arrangement, if either of the independently deployed modules, the EJB or the WAR, needs to access the other, this must be done via remote interfaces. This means that the EJB home and remote interfaces must be packaged into the calling modules and all communication may require more overhead than if they were packaged together (in which case WebLogic can perform various call optimizations such as using call-by-reference as it does for new EJB 2.0 local interfaces).

Table 1 lists the primary classloaders in a typical WebLogic EAR application. These are listed in order of creation from the primordial bootstrap classloader through various child classloaders down to the Web classloader. A couple of notes here: each of these classloaders may or may not be a distinct ClassLoader subtype or may be multiple instances of the same class configured differently. WebLogic may internally use additional classloaders (the details of which don't matter to us here); the native JVM classloaders are not set up to be reloadable but WebLogic's are (thus the familiar problem of having to reboot the application server if classes loaded by the non-reloadable classloaders have changed).

<!----><!---->1<!---->


What This Means
The consequences of this classloading scheme include:

  • Isolation: The positive, deliberate side effect of the lack of sibling classloader visibility is isolation - classes loaded by different classloaders at the same level do not, and cannot, directly access one another without explicitly including the other's remote interfaces. You can see an advantage of this sand boxing by looking at how WebLogic handles EAR files: each is run in its own classloader instance and can be deployed and redeployed independently. This means that when you and I both deploy our separate applications to Server A, we can do so in parallel without worrying about a host of issues that might occur if we shared the same classloader. As the old adage goes, fences make for good neighbors.
  • Visibility: A key benefit of the hierarchical approach to classloader visibility is that JSPs, servlets, and other Web application components have direct access to all EJBs within the larger application. This both simplifies deployment and gives the application server more opportunities to optimize the performance of calls between components (covered in more detail below).
  • Namespaces: Class instances within the JVM are identified by the combination of the full classname and the classloader that loaded it. This namespace identification can come into play in your WebLogic applications. For example, think about how common it is for a Web application to start from a JSP or servlet with a standard name like index.jsp. That's fine. But what happens when you have two Web apps (two WAR files) within the same EAR and both of those apps have that innocuously named JSP or servlet? Without the benefit of classloader isolation, after the compiler finishes turning those source files into compiled servlets, a namespace or class-loading issue occurs because they both resolve to the same name, resulting in only one being loaded. Regardless of what happens in that scenario, it won't happen in WebLogic because each WAR is loaded in its own classloader and even if the JSPs or servlets have the same name, each exists independently as far as the classloaders are concerned.
  • Performance: Keep in mind that class loading, or more generally, packaging, impacts performance. Components deployed together in an EAR allow the WebLogic Server to optimize performance by reducing RMI call overhead as well as allowing you to use the new EJB 2.0 local interfaces. And it's through the default class-loading behavior (whereby loading is first deferred to parents as opposed to having each classloader load its own copy of the same class) that overall memory consumption is reduced.
  • Singletons: If you use the singleton design pattern in your application you need to be aware that classloaders matter because singletons (at least the run-of-the-mill, GoF design pattern variety such as those accessed with a getInstance-type method) exist not just within a single JVM but within a single classloader. Thus, if the singleton is loaded at the EAR level (such as from a utility library included at that level and not at the system-wide classpath level) by EAR 1 and EAR 2, then each application will get a different copy of that singleton. And there are of course other caveats to be aware of with singletons: clustering can throw a monkey wrench into the works, and in a world of distributed objects other patterns may be necessary, such as using JNDI to manage a singleton EJB (see some active discussions on this topic at www.theserverside.com).
  • Deployment: Regardless of how you deploy your application module - by using the console, by WebLogic's automatic hot deployment mode (quite useful during development), or through command-line utilities - the server will create a new classloader instance for loading this application. If you choose to deploy EJBs and WARs separately and you want your Web application to use those EJBs, you'll need to include the EJB home and remote interfaces in your WAR file. These are loaded by sibling classloaders and you'll need to make remote calls across them. Similarly, if you want to reference EJBs across EAR files you'll need to package the home and remote interfaces from the one EAR into the other (or you could try the approach of generating client JARs for your EJBs by using the ejb-client-jar tag in ejb-jar.xml and add references to those JARs in the manifest classpath - see below for more about the manifest).


Handling Utility Classes
Nearly every application, large or small, needs utility classes, either your own or from one or more third parties. Given what we've seen in looking at classloaders and packaging, where can and should these go? Table 2 outlines some of the choices you have as well as their attendant pros and cons.

<!----><!---->1<!---->


To elaborate a bit on the last item in Table 2: because they are JAR files, all J2EE modules have a MANIFEST.MF file in their META-INF folder. The J2EE specification allows classes in one JAR - such as an EJB or WAR archive - to access other JARs by explicitly adding their names to a special "Class-Path:" entry in the manifest file (this is built upon the "extensions mechanism" introduced in JDK 1.2). By using this entry you can tailor EAR class-loading behavior and can modularly deal with many of the classpath issues mentioned earlier.

In order to make this work, first create a text file specifying a list of JAR filenames as relative URLs separated by spaces and then include this file when building your deployment. Here's what a typical entry might look like:

<o:p> </o:p>

  Manifest-Version: 1.0
  Class-Path: myparser.jar vendorlib.jar


And here's an example of how you might add it when creating an EJB JAR (presuming you have the above line in a file called mymanifest.txt):

<o:p> </o:p>

  jar cmd mymanifest.txt myejb.jar com META-INF


Now the classes in myejb.jar have access to myparser.jar and vendorlib.jar because they've been loaded with this EJB JAR into its same classloader. Within the context of an EAR file, the JARs referenced within EJBs and WARs are loaded at the same level as EJBs and are available to all beans and Web modules within the application. (Note what happens: WebLogic takes the transitive closure of all manifest classpath entries specified in the EJB and WAR archives for a given EAR and loads them at the same top level within that EAR.)

Regardless of which classloaders and packaging technique you use, there's one other caveat worth mentioning and that's the issue of classloading order. Based upon the interdependencies among your utilities, JARs, EJBs, etc., you may find yourself needing to tailor what's placed where solely to satisfy load-time ordering. For example, utility classes needed by WebLogic itself, such as JDBC drivers, must be specified on the system classpath so that they're available when it starts. Complications can also arise in the case of startup classes and their dependencies (this is covered in more detail on BEA's site at http://edocs.bea.com/wls/docs61/programming/packaging.html).

Overall, here are a few general guidelines on utility classes: packaging in JAR files is generally more manageable than using individual class files. If the utilities are needed only by a single Web module then put them in a jar in its WEB-INF/lib folder; if the utilities are to be shared by multiple components or modules within a single application then keep them as a JAR at the application level; and finally, if you need to use the utility classes across applications, first try to use the manifest classpath approach and if that's not feasible, then use the global classpath. You can find a good discussion of this as well as J2EE packaging in Tyler Jewell's articles on this topic at www.onjava.com/pub/a/onjava/2001/06/26/ejb.html.

Debugging Class-Loading Issues
As with any other development issue, class loading at times needs to be debugged. Here are a few suggestions:

  • Plan: Invest some up-front effort in considering how many JARs to build (or WARs, EJB JARs, and EARs as the case may be), their names, their location, and which classes go into which archives.
  • Refactor: The layout and structure of this runtime packaging, like all software, can be refactored. In this case, refactoring means modifying and refining your implementation of the aforementioned packaging plan. Yes, this can take some effort, but so does any sort of refactoring (and because debugging class-loading issues are inherently "not fun" this serves as a deterrent to treating this step too lightly).
  • Diagnose:
    - Walk through your shell environment, startup scripts, and configuration files to get a handle on what's being set where. Often it makes sense to consolidate setting file locations and paths in a single place such as one startup script (which itself does not assume any environment variables have been set or explicitly overrides them).
    - Given that some things won't be apparent until runtime, have your run script print out to the console all settings prior to server launch.
    - Write a simple Java debug routine that displays relevant values (including system properties such as "sun.boot.class.path").
    - Use a tool like the JSP page described below.

As an example, say you have a utility class, MyHandler, which for some reason (deliberate or accidental) appears more than once in your environment; it could be that the version loaded at runtime may not be the one you expect. If in this scenario your servlet (in a WAR file) instantiates MyHandler and the only physical location of MyHandler.class is under the WEB-INF/classes directory of that WAR, then this will be the one that gets loaded. But, if you have another version of MyHandler.class somewhere on the system classpath, it will be this one that gets loaded, regardless of the fact that there's one right there in the same archive as your program.

Why does this happen? Because of classloader delegation. As noted earlier, all classloaders first delegate load requests to their parent, recursively up the tree, allowing each parent the opportunity to handle the request. In this case, because the system classloader was able to resolve this class reference, the child WAR classloader never had the chance to resolve and load this class itself. The nuances of such behaviors can appear to be quite idiosyncratic even if they're not and certainly vary across application servers and server versions. If you have multiple versions of the same libraries (such as might happen with common utilities such as XML parsers), be prepared to do a little investigating.

Also keep in mind that because standard classloaders (including WebLogic's) delegate all load requests to their parents before their own searches, anything deployed on the system classpath can't be redeployed. As just shown, these classes will always be loaded by the JDK system classloader, which does not support reloading. Thus, anything you want to be redeployable without having to restart the server must only be contained within one of the dynamically loadable units such as EARs, EJB-JARs, and WARs.

One last debugging note: Classes with no visibility modifier before the class declaration are deemed to be "package level" or "package private". In order for another class to access these classes, they must be in the same package and loaded by the same classloader. If this isn't the case, the caller will get an IllegalAccessException.

J2EE Class-Loading Diagnostic Tool
In order to diagnose and debug the subtleties of class-loading issues, I've developed a small JSP utility page. Just drop this JSP into your default Web app directory or include it with a specific application to run some quick tests. It has two primary functions: the first is to allow you to specify a class to load (typically a utility class, but it could be any class that can be instantiated with a direct newInstance() call) and show you the classloader that loaded your class, the classloader hierarchy of ancestors to that classloader, and the file from which your class was loaded.

The second function is to outline in a table a number of the standard locations searched by the various classloaders. This includes the locations searched by the standard Java classloaders (such as the CLASSPATH or, more specifically, the locations identified in the system properties). If you are running the server on your local machine and can click on the names of any JARs, you might want to open them and view the contents.

You can get a sense of what this tool provides from Figure 2. In this example, the page has been reloaded after I specified that I wanted my utility class, MyHandler, to be loaded. At the top it shows how and where it found this class, and below it shows an outline of each set of files and directories searched by various Java and WebLogic classloaders. [Two notes: 1) the ClassLoader method getParent() will return null to represent the bootstrap classloader, which explains why a specific classname isn't given in this case; and 2) this utility doesn't try to dig into things like the manifest files or other bean references so it may not display all references in the lower table].

<!----><!---->1<!---->


When the tool is unable to locate one of the standard locations (such as WEB-INF/lib), it will print a "not found" message. In this case, the missing directory is perfectly acceptable given that those locations are optional. On the other hand, for items derived from your CLASSPATH, this can indicate a problem such as a JAR file that's specified in the environment but doesn't really exist at that location. This happens as JARs are moved around and can be a handy way to catch these errors (in the example pictured, under Application Classes the file Weblogic_sp.jar is not found - the right location for this should be determined or it should be removed from the classpath).

Conclusion
Keep in mind that the class-loading mechanisms described here are applicable to WebLogic Server 6.1 (and likely the final 7.0 release). These behaviors have changed over time as both WebLogic and the J2EE specification have evolved. Additionally, each application server vendor may implement its class-loading mechanisms differently as long as it conforms to the requirements outlined in the J2EE 1.3 specification. This means that other servers, while they may conform to the behavioral requirements defined by the J2EE spec, employ class-loading schemes that vary from that used by WebLogic.

<o:p> </o:p>

分享到:
评论

相关推荐

    weblogic 生成class

    weblogic weblogic生成的class

    SSH项目部署在window和linux下的Weblogic上出现 ClassNotFound异常解决办法

    ### SSH项目部署在Window和Linux下的Weblogic上出现ClassNotFound异常解决办法 #### 背景介绍 在部署Java EE项目时,特别是采用SSH(Struts + Hibernate + Spring)架构的项目,在不同的应用服务器(如Tomcat、...

    weblogic&java精华

    1. WebLogic 里面的一个 class 修改了,需要重新启动 WebLogic 吗? 答案是不需要重新启动 WebLogic。WebLogic 提供了热部署机制,可以在不重新启动的情况下部署新的类文件。 2. Tomcat 关于 UTF-8 JSP 文件的 BUG ...

    weblogic部署项目jar冲突解决

    2. **使用WebLogic的Class-Path元素**:在`weblogic.xml`配置文件中,可以使用`&lt;class-loader&gt;`标签来指定应用的类加载策略。例如,可以使用`delegate="false"`来让WebLogic优先使用应用自身的类库,避免与服务器...

    weblogic 破解和说明

    %MEDREC_WEBLOGIC_CLASSP 为 set CLASSPATH=d:\bea\weblogic_crack.jar;%CLASSPATH%;%MEDREC_WEBLOGIC_CLASSP 3. 重启weblogic linux下使用方法: 1.将license.bea和weblog_crack.jar拷贝到bea安装目录下,例如:...

    weblogic监控 weblogic调优 weblogic版本区别 weblogic启动关闭脚本

    WebLogic Server是一款由Oracle公司开发的企业级Java应用服务器,它为构建、部署和管理企业级Java应用程序提供了全面的平台。本文将深入探讨WebLogic的监控、调优、不同版本之间的区别以及启动和关闭脚本的使用。 ...

    Weblogic 套件和Weblogic 标准版 Weblogic 企业版 功能对比

    在IT领域,特别是针对企业级应用服务器的选择与配置,Oracle WebLogic Server无疑占据了重要的位置。WebLogic Server作为一款高性能、可扩展的企业级Java应用服务器,提供了丰富的功能与服务,适用于构建、部署和...

    weblogic10.3.3之后版本升级至weblogic10.3.6文档.docx

    ### WebLogic 10.3.3 至 10.3.6 升级指南 #### 一、概述 本指南旨在详细介绍如何从WebLogic Server 10.3.3及其后续版本升级到10.3.6版本的具体步骤。升级过程中需要考虑的因素以及必要的准备措施也将被涵盖。 ###...

    weblogic API FOR [weblogic.jar]

    public class WebLogicAPIExample { public static void main(String[] args) throws Exception { AdminServerMBean adminServer = ManagementFactory.getAdminServer(); String version = adminServer....

    eclipse的weblogic插件

    为了在Eclipse中方便地开发、调试和管理运行在WebLogic上的应用,Eclipse提供了WebLogic插件。 WebLogic插件的安装方法如描述所述,首先需要将下载的WebLogic插件压缩包解压。这个压缩包通常包含了若干个.jar文件,...

    weblogic详细安装部署手册

    ### WebLogic详细安装部署流程 #### 一、安装前准备 **1.1 JDK环境配置** - **确保JDK已安装:** 在安装WebLogic之前,必须先安装Java Development Kit (JDK)。WebLogic服务器依赖于JDK来运行。请确保安装的是与...

    weblogic weblogic weblogic

    WebLogic是Oracle公司的一款企业级Java应用服务器,它基于Java EE(Enterprise Edition)平台,用于构建、部署和管理分布式应用程序。WebLogic Server是许多大型企业和组织的核心组件,它提供了多种功能和服务,包括...

    weblogic傻瓜式安装教程

    ### WebLogic傻瓜式安装教程详解 #### 一、前言 本文档旨在提供一个简单易懂的WebLogic安装教程,适用于初次接触WebLogic或希望快速完成安装的用户。通过本教程,您将学会如何在Linux环境下进行WebLogic的安装与...

    weblogic10.3性能优化参数配置

    Weblogic 10.3 性能优化参数配置 Weblogic 服务器是 Oracle 公司推出的一个基于 Java 的中间件服务器,广泛应用于企业级应用系统中。为了确保 Weblogic 服务器的高性能和稳定运行,需要对其进行合理的配置和优化。...

    利用weblogic设置代理转发功能注意事项

    &lt;servlet-class&gt;weblogic.servlet.proxy.HttpProxyServlet&lt;/servlet-class&gt; &lt;param-name&gt;WebLogicHost &lt;param-value&gt;192.168.9.233 &lt;param-name&gt;WebLogicPort &lt;param-value&gt;7001 ``` 集群分发配置 ...

    AIX下的Weblogic安装

    在AIX操作系统上安装Oracle WebLogic Server是一项技术性较强的任务,需要对AIX系统和WebLogic有深入的理解。本文将详细介绍在AIX环境下如何进行WebLogic的安装、配置以及启动。 首先,AIX(Advanced Interactive ...

    weblogic日常巡检,问题排查

    ### WebLogic 日常巡检与问题排查 #### 引言 WebLogic作为一款广泛使用的Java应用服务器,在企业级应用环境中扮演着重要的角色。为了确保WebLogic服务器能够稳定、高效地运行,定期进行健康检查(巡检)是非常必要...

    weblogic 开机自动启动

    Weblogic 开机自动启动详解 Weblogic 作为一个流行的 Java 企业级应用服务器,通常需要在服务器启动时自动启动,以确保业务的连续性。然而,在 Windows 系统下,Weblogic 的自动启动却需要进行一些额外的配置。在这...

    WebLogic License

    WebLogic License是Oracle WebLogic Server的关键组成部分,它定义了用户可以如何使用和部署这款企业级应用服务器。在Oracle收购BEA之后,WebLogic License的管理方式发生了一些变化,旨在简化授权流程并提供更大的...

Global site tag (gtag.js) - Google Analytics