Contact Us 1-800-596-4880

Classloader Control in Mule

Mule Runtime Engine versions 3.5, 3.6, and 3.7 reached End of Life on or before January 25, 2020. For more information, contact your Customer Success Manager to determine how you can migrate to the latest Mule version.

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

Store user third-party libraries in the lib/user directory under the Mule version directory. For example, for the 3.6.0 Mule Enterprise distribution, put the library in the mule-enterprise-3.6.0/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 will attempt to load a class using his parent classloader before attempted 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 class loader . 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 on a given folder will 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 will be added. This classloader will contain 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 will be loaded all the libraries located on <MULE_HOME>\lib\mule\per-app directory.

    CE-classloading-3.6

The Mule classloader order is illustrated in the following diagram:

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 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 supportingfine 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 will be a Composite ClassLoader that contains a list of classloaders. The first classloader in that list will be a Mule Application classloader as the one described before and the following elements will be the classloaders filters for each installed Mule Plugin.

EE-Classloading-3.6
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 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