IBM


12.5 Learning class loaders by example

We have now described all the different options for influencing class loader behavior. In this section, we take an example and use all the different options we have discussed to this point so that you can better evaluate the best solution for your applications.

We have created a very simple application, with one servlet and one EJB. Both call a class, VersionChecker, shown in Example 12-4. This class can print which class loader was used to load the class. The VersionChecker class also has an internal value that can be printed to check which version of the class we are using. This will be used later to demonstrate the use of multiple versions of the same utility JAR.

Example 12-4 VersionChecker class source code

package com.itso.classloaders;

public class VersionChecker {
  static final public String classV= "v1.0";

  public String getInfo() {
    return ("VersionChecker is " + classV+ 
      ". Loaded by " + this        .getClass().getClassLoader());
  }

}

Once installed, the application can be invoked through http://localhost:9080/ClassloaderExampleWeb/ExampleServlet. This invokes the ExampleServlet which calls VersionChecker and then displays the sample information in Example 12-5.

Example 12-5 Invoking ExampleServlet

VersionChecker is v1.0.

Loaded by com.ibm.ws.classloader.CompoundClassLoader@71827182

Local ClassPath: C:\WebSphere\AppServer\profiles\AppSrv02\installedApps\kcgg1d8Node02Cell\ClassloaderExample.ear\ClassloaderExampleWeb.war\WEB-INF\classes;C:\WebSphere\AppServer\profiles\AppSrv02\installedApps\kcgg1d8Node02Cell\ClassloaderExample.ear\ClassloaderExampleWeb.war\WEB-INF\lib\VersionCheckerV1.jar;C:\WebSphere\AppServer\profiles\AppSrv02\installedApps\kcgg1d8Node02Cell\ClassloaderExample.ear\ClassloaderExampleWeb.war

Delegation Mode: PARENT_FIRST

The VersionCheckerV1.jar file contains the VersionChecker class file that returns Vnumber 1.0. For all the following tests, we have, unless otherwise noted, left the class loader policies and loading modes to their defaults. In other words, we have one class loader for the application and one for the WAR file. Both have their delegation modes set to Classes loaded with parent class loader first (PARENT_FIRST). We assume the application has been deployed to an appserver called AppSrv02.


Redbooks ibm.com/redbooks

Next