Nav
You are viewing an older version of this section. Click here to navigate to the latest version.

Classloader Control in Mule

This topic introduces you to classloading in Mule and shows you how to override classloading in your applications and plugins.

User third-party libraries are stored in the lib/user directory under the Mule version directory. For example, for the 3.7.3 Mule Enterprise distribution, put the library in the mule-enterprise-3.7.3/lib/user directory.

Classloading in Mule

Mule defines a hierarchy of classloaders to find and load classes for execution. This hierarchy uses a "parent first" model by default, this means that each classloader attempts to load a class using its parent classloader before attempting to do it itself.

Although this classloading architecture meets most classloading needs, there are times when you might need to override the default classloading scheme. For example, suppose an application requires a third-party library that is bundled with it. This might conflict with a library version that the domain classloader would load (that is, a version of the library already bundled with Mule). How do you ensure that the required version of the library is used with the application? To address requirements such as these, Mule supports fine-grained class loading control that enables you to override default classloading.

The classloader hierarchy for the different artifacts consists of: 

  1. The bootstrap, extensions, and CLASSPATH class loaders created by the Java virtual machine. This classloader loads the core Java libraries.

  2. The Mule System classloader. This classloader loads standard Mule libraries, that is, libraries in the <MULE_HOME>/lib subdirectories, where <MULE_HOME> is the directory where Mule ESB is installed. Subdirectories are loaded in a given order: /boot, /user, /mule and /opt. This means that classes located in a given folder have precedence over classes with the same name located in a folder that was loaded after.

  3. Domain classloader. Domains are used to share resources and libraries between the applications that belong to it. This classloader contains all the libraries located on <MULE_HOME>/domains/<mydomain>   

    In Mule versions prior to 3.5.0 the concept of domain is a bit different. In those versions, a domain is defined by a folder located on <MULE_HOME>/lib/shared/<DOMAIN_NAME> and it was used to share only libraries files (each application had its own instance of the classes provided by those files) on the apps belonging to it. This feature is still supported, but the new domain concept is preferred.

  4. If an application contains application plugins, then a Plugins classloader is added. This classloader contains all the plugins classes and libraries.

    Plugins classloader supports fine grain classloading and the configuration for this classloader is obtained  merging the fine grain classloading configuration for all the application plugins. This means that plugins are not isolated one from the other inside the application.

  5. One or more Mule application class loaders that load classes or libraries from a Mule application, that is, classes and libraries from <MULE_HOME>/apps/<myapp>/classes and <MULE_HOME>/apps/<myapp>/lib directories, where <myapp> is the name of the application. Besides the resources included in the Mule application, on each application classloader is loaded all the libraries located on <MULE_HOME>\lib\mule\per-app directory.

CE-classloading-3.7

Classloader Folder location

JVM Bootstrap Classloader

JDK Classes

Mule System Classloader

${MULE_HOME}/lib/boot ${MULE_HOME}/lib/user ${MULE_HOME}/lib/mule ${MULE_HOME}/lib/opt

Mule Share Domain Classloader for "Domain1"

${MULE_HOME}/domains/domain1/lib

Application Shared Plugin Libraries Classloader for "Application1"

${MULE_HOME}/apps/application1/plugins/lib

Application Plugins Classloader for "Application1"

${MULE_HOME}/apps/application1/plugins/plugin1/classes ${MULE_HOME}/apps/application1/plugins/plugin1/lib ${MULE_HOME}/apps/application1/plugins/pluginN/classes ${MULE_HOME}/apps/application1/plugins/pluginN/lib

Application Classloader for "Application1"

${MULE_HOME}/lib/mule/per-app ${MULE_HOME}/apps/application1/classes ${MULE_HOME}/apps/application1/lib

Extended Mule EE Classloading Model

Mule EE provides a plugin framework to add plugins at Mule server level.

When a Mule EE server contains Mule Plugins, the classloading model is a bit different than the previously described hierarchy. The main difference is that applications can access resources exported by Mule Plugins.

To support that scenario, each Mule plugin has a classloader supporting fine grain classloading. These classloaders are created using the Mule system classloader as the parent. Mule Plugins can contain many classes, libraries and resources, but only some of them should be accessed from the application. To avoid exporting unnecessary resources, from a Mule plugin, a class loader filter is used. Then the classloader used in the Mule Application is a Composite ClassLoader that contains a list of classloaders. The first classloader in that list is a Mule Application classloader as the one described before, and the following elements are the classloaders filters for each installed Mule Plugin.

EE-Classloading-3.7

Classloader Folder location

JVM Bootstrap Classloader

JDK Classes

Mule System Classloader

${MULE_HOME}/lib/boot ${MULE_HOME}/lib/user ${MULE_HOME}/lib/mule ${MULE_HOME}/lib/opt

Mule Share Domain Classloader for "Domain1"

${MULE_HOME}/domains/domain1/lib

Application Shared Plugin Libraries Classloader for "Application1"

${MULE_HOME}/apps/application1/plugins/lib

Application Plugins Classloader for "Application1"

${MULE_HOME}/apps/application1/plugins/plugin1/classes ${MULE_HOME}/apps/application1/plugins/plugin1/lib ${MULE_HOME}/apps/application1/plugins/pluginN/classes ${MULE_HOME}/apps/application1/plugins/pluginN/lib

Application Classloader for "Application1"

${MULE_HOME}/lib/mule/per-app ${MULE_HOME}/apps/application1/classes ${MULE_HOME}/apps/application1/lib

Mule Plugin for "Plugin 1"

${MULE_HOME}/plugins/plugin1/classes ${MULE_HOME}/plugins/plugin1/lib