Java and WebSphere Classloading

watermelonroachdaleInternet και Εφαρμογές Web

30 Ιουλ 2012 (πριν από 4 χρόνια και 10 μήνες)

380 εμφανίσεις

Java Technology Centre
Java and WebSphere Classloading
01/26/06
© 2006 IBM Corporation
Java and WebSphere Classloading
Hedley Proctor
Java Technology Centre
© 2006 IBM Corporation
2
Java and WebSphere Classloading
01/26/06
Contents

Overview of classloading:

Loading

Linking

Initialisation

Delegation model

Classloaders in Java

Classloaders in WebSphere
Java Technology Centre
© 2006 IBM Corporation
3
Java and WebSphere Classloading
01/26/06
Contents

Bespoke classloading example

Why you might like to write your own classloader

Implementation

Test and debug
Java Technology Centre
© 2006 IBM Corporation
4
Java and WebSphere Classloading
01/26/06
Overview of classloading
Classloading takes information from a disc or network
to create a class for use.
Definition:
Classloading is fairly new concept in programming:
Functional:
No classloading.
Object oriented:
Classes exist, but not classloaders.
Managed runtime:
Ability for programmers to write their
own
classloaders.
Java Technology Centre
© 2006 IBM Corporation
5
Java and WebSphere Classloading
01/26/06
Overview of classloading

What are the steps necessary in loading a
class?

What advantages does managed runtime
classloading give us over older languages
such as C and C++?
Java Technology Centre
© 2006 IBM Corporation
6
Java and WebSphere Classloading
01/26/06
Steps involved

Bytecode
verification

Resolution of
references

Preparation of
fields and
methods
Loading
Initialisation
Linking

Initialise
variables

Load class
bytecode
Java Technology Centre
© 2006 IBM Corporation
7
Java and WebSphere Classloading
01/26/06
Loading

Locate class bytes and load them into
memory.

Can apply security controls at this
stage.

This part of the process can be
customised by a programmer.
Java phase
Native phase

Create skeleton java.lang.Class object.

Virtual machine specific.
Note: Before loading a class, its superclass must be loaded,
so the above process is often recursive
Java Technology Centre
© 2006 IBM Corporation
8
Java and WebSphere Classloading
01/26/06
Linking

Bytecode verification

Run static analysis on the bytecode to ensure it doesn't
break the stack based virtual machine.

Resolution of references to other classes and
methods
Question:
When we “resolve” references, do we need
to load the referenced classes?
Java Technology Centre
© 2006 IBM Corporation
9
Java and WebSphere Classloading
01/26/06
Linking
IBM Java 1.3 / WebSphere 5
– Eager class resolution
IBM Java 1.4 / WebSphere 5.1
– Lazy class resolution
Answer:
We can choose to load now, or at point of
first use.

Preparation – creation of structures in memory

Fields

Methods
Java Technology Centre
© 2006 IBM Corporation
10
Java and WebSphere Classloading
01/26/06
Initialisation

Run static initialisers

Set other fields to default values
Java Technology Centre
© 2006 IBM Corporation
11
Java and WebSphere Classloading
01/26/06
Advantages of Managed Runtime Classloading

Coexistence of multiple versions of same
application on same machine.

Can dynamically change class definitions.
Java Technology Centre
© 2006 IBM Corporation
12
Java and WebSphere Classloading
01/26/06
Delegation model

We could do all classloading with one classloader,
but this would not give us much flexibility.

Need mechanism for allowing classloaders to call
other classloaders.

Use concept of parent classloader – the one that
loaded current classloader.
Java Technology Centre
© 2006 IBM Corporation
13
Java and WebSphere Classloading
01/26/06
Delegation model
1)

Cache of already
loaded classes
2)

Parent classloader
3)

Disc (or network)
Bootstrap
CL
Extension
CL
Application
CL
Cache
Cache
Cache
classpath
/jre/lib/ext
/jre/lib
1)
2)
3)
Note: This is how the
JVM is set up, but we
can change the order of
parent / disc searching,
as we'll see later.
Java Technology Centre
© 2006 IBM Corporation
14
Java and WebSphere Classloading
01/26/06
Classloaders in Java

Native
Bootstrap
CL
Extension
CL
Application
CL

sun.misc.Launcher$ExtClassLoader

sun.misc.Launcher$AppClassLoader
Java Technology Centre
© 2006 IBM Corporation
15
Java and WebSphere Classloading
01/26/06
Inheritance hierarchy

Custom classloaders should
inherit from URLClassLoader,
not java.lang.ClassLoader.

Don't confuse class
inheritance with the parent
delegation model!
java.lang.ClassLoader
java.net.URLClassLoader
java.security.SecureClassLoader
ExtClassLoader
AppClassLoader
Custom classloader
Java Technology Centre
© 2006 IBM Corporation
16
Java and WebSphere Classloading
01/26/06
Classloading in WebSphere v5.1
Java Classloaders
Web Module Classloader
WebSphere Extension CL
ws.ext.dirs
Application Module CL
Application Module CL

Load the J2EE API code.
Java Technology Centre
© 2006 IBM Corporation
17
Java and WebSphere Classloading
01/26/06
WebSphere Extension Classloader

For WebSphere classes.

Loads from locations specified by ws.ext.dirs
system property:

WebSphere/Appserver/classes

WebSphere/Appserver/lib

WebSphere/Appserver/lib/ext

User should not need to change ws.ext.dirs.
Java Technology Centre
© 2006 IBM Corporation
18
Java and WebSphere Classloading
01/26/06
Application Module Classloader

Loads:

EJB modules.

Resource archives (RARs).

Dependency JAR files.

Web modules, if configured to do so.

Can use separate application module classloader
for each application, or single one for all
applications.
Java Technology Centre
© 2006 IBM Corporation
19
Java and WebSphere Classloading
01/26/06
Application Module Classloader

Application Servers -> server1
Java Technology Centre
© 2006 IBM Corporation
20
Java and WebSphere Classloading
01/26/06
Web Module Classloader

Optional – web modules can be loaded by the
application classloader if you wish.

Configured at the application level:

Applications -> Enterprise Applications -> MyApp
Java Technology Centre
© 2006 IBM Corporation
21
Java and WebSphere Classloading
01/26/06
Delegation Model

Unlike the JVM, with WebSphere classloader
delegation can be configured:

PARENT_FIRST

PARENT_LAST

Delegating to the parent last allows one to provide
a second version of a class that exists in the
classpath of a parent classloader.

Default is PARENT_FIRST, like JVM.
Note: Most classloading problems occur because of the
location of classes in the delegation hierarchy.
Java Technology Centre
© 2006 IBM Corporation
22
Java and WebSphere Classloading
01/26/06
Bespoke Classloading Example

Generally unnecessary these days - WebSphere
classloading model provides capabilities required:

Application isolation provided by the separate Application
classloaders.

Ability to reload new versions of applications provided
under
Applications -> MyApp -> Update

WebSphere Rapid Deployment (v6) can watch a directory
and automatically apply updates as they become available.
Java Technology Centre
© 2006 IBM Corporation
23
Java and WebSphere Classloading
01/26/06
Online Flight Reservation System
Flights.jsp
S
S
S
FlightsServlet

Handles business

logic
.
FlightsVersionServlet

Checks application version.
FlightsSearch v1.0

Bean that carries
out search.
CL
FlightsUpdateServlet

Updates FlightsSearch
bean by using custom
classloader.
FlightsSearchClassloader
FlightsSearch v1.1

Updated version of
bean.
Java Technology Centre
© 2006 IBM Corporation
24
Java and WebSphere Classloading
01/26/06
FlightsClassloader

Overrides findClass(String name) method. Must do
two things:

Read class bytes into memory.

Call defineClass to create Class object.
Java Technology Centre
© 2006 IBM Corporation
25
Java and WebSphere Classloading
01/26/06
Diagnostics
-verbose:class
Application Servers -> server1 -> Process Definition -> Java Virtual Machine
Java Technology Centre
© 2006 IBM Corporation
26
Java and WebSphere Classloading
01/26/06
Diagnostics
-Dibm.cl.verbose=myPackage.myClass
Same panel, under Generic JVM Arguments
Java Technology Centre
© 2006 IBM Corporation
27
Java and WebSphere Classloading
01/26/06
Diagnostic Output
-verbose:class
Troubleshooting -> Logging and Tracing -> server1 -> Process logs ->
native_stderr.log
Massive amount of output – loading of every single class.
[Loaded java.lang.NoClassDefFoundError from
/opt/WebSphere/AppServer/java/bin/../jre/lib/core.jar]
[Loaded java.lang.Class from /opt/WebSphere/AppServer/java/bin/../jre/lib/core.jar]
[Loaded java.lang.Object from /opt/WebSphere/AppServer/java/bin/../jre/lib/core.jar]
[Loading superclass and interfaces of java/lang/Object]
[Loaded java.lang.Throwable from
/opt/WebSphere/AppServer/java/bin/../jre/lib/core.jar]
[Loading superclass and interfaces of java/lang/Throwable]
[Loaded java.io.Serializable from
/opt/WebSphere/AppServer/java/bin/../jre/lib/core.jar]
[Loading superclass and interfaces of java/io/Serializable]
Java Technology Centre
© 2006 IBM Corporation
28
Java and WebSphere Classloading
01/26/06
Diagnostic Output
-Dibm.cl.verbose=myPackage.myClass
Troubleshooting -> Logging and Tracing -> server1 -> JVM Logs
More detailed output - shows movement down the classloading
delegation hierarchy:
ExtClassLoader attempting to find com.xyzindustries.FlightsSearch
ExtClassLoader could not find com/xyzindustries/FlightsSearch.class in
/opt/WebSphere/AppServer/java/jre/lib/ext/PD.jar
AppClassLoader attempting to find com.xyzindustries.FlightsSearch
AppClassLoader could not find com/xyzindustries/FlightsSearch.class in
/opt/WebSphere/AppServer/properties
com.ibm.ws.bootstrap.ExtClassLoader attempting to find
com.xyzindustries.FlightsSearch
com.ibm.ws.bootstrap.ExtClassLoader could not find
com/xyzindustries/FlightsSearch.class in /opt/WebSphere/AppServer/java/lib
Java Technology Centre
© 2006 IBM Corporation
29
Java and WebSphere Classloading
01/26/06
Summary

Classloading is composed of loading, linking and
initialising.

Custom classloaders only alter the way the JVM
finds class bytes and loads them into memory.

Creation of the java.lang.Class object is done by
the JVM.

The delegation model governs how classloaders
call other classloaders to search for classes.

Most classloading problems occur because of the
location of classes within the delegation hierarchy
or classpath.
Java Technology Centre
© 2006 IBM Corporation
30
Java and WebSphere Classloading
01/26/06
Summary

The JVM uses three classloaders: bootstrap,
extensions and application.

WebSphere uses three classloaders:

WebSphere extensions.

Application module.

Web module.

Custom classloaders generally not needed
anymore.

If you must create one, subclass URLClassloader and
override findClass(String name).
Java Technology Centre
© 2006 IBM Corporation
31
Java and WebSphere Classloading
01/26/06
Summary

Two sets of diagnostic tracing available:
verbose:class
Shows loading of all classes.
-Dibm.cl.verbose=myPackage.myClass
More detailed output about one class.
Java Technology Centre
© 2006 IBM Corporation
32
Java and WebSphere Classloading
01/26/06
References

IBM Java Diagnostics Guide
http://www-128.ibm.com/developerworks/java/jdk/diagnosis/

Series of four developerWorks articles:
Demystifying class loading problems
Part 1: An introduction to class loading and debugging tools
Part 2: Basic class loading exceptions
Part 3: Tackling more unusual class loading problems
Part 4: Deadlocks and constraints