CDI in EARs

Foreword

I was banging my head against the wall for the last few days when trying to solve a few tricky issues we saw with EAR support over at Apache DeltaSpike. I’m writing this up to clean my mind and to share my knowledge with other EAR-pig-wrestlers…

The EAR ClassLoader dilemma

EARs are a constant pain when it comes to portability amongst servers. This has to do with the fact that JavaEE still doesn’t define any standards for visibility. There is no clear rule about how the ClassLoaders, isolation and visibility has to be set up. There is just as single paragraph (JavaEE7 spec 8.3.1) about which classes you might probably see.

There are 2 standard ClassLoader setups we see frequently in EARs.
For the sake of further discussion we assume an EAR with the following structure:

sample.ear
├── some-ejb.jar
├── lib
│   ├── some-shared.jar
│   └── another-shared.jar
├── war1.war
│   └── WEB-INF
│       ├── classes 
│       └── lib
│           └── war1-lib.jar
└── war2.war
    └── WEB-INF
        ├── classes 
        └── lib
            └── war2-lib.jar

Flat ClassLoader

The whole EAR is served by just a single flat ClassLoader. If you have 2 WARs inside your application then the classes in each of it can see each other. And also the classes in the shared EAR lib folder can see. This is e.g. used in JBoss-4 (though not from the beginning). You can also configure most of the other containers to use this setup for your EAR. But nowadays it’s hardly a default anymore (and boy is that good!)

Hierarchic ClassLoader

This is the setup used by most containers these days – but it’s still not a mandated by the spec! The Container will provide a shared EAR Application ClassLoader which itself only contains the shared ear libs. This is the parent ClassLoader of all the ClassLoaders in the EAR. Each WAR and ejb-jar inside your EAR will get an own child WebAppClassLoader.

This means war2 doesn’t see any classes or resources from war1 and the other way around. It further means that the shared libs do not see the classes of war1 nor war2, etc!

If you need some caches in your shared libs, then you need to rely on the ThreadContextClassLoader (TCCL) as outlined in JavaEE 7 paragraph 8.2.5. While this section is about “Dynamic Class Loading” it also is valid for caches and storing other dynamic information in static variables. Otherwise you end up mixing values from war1 and probably re-use them in war2 (where you will get a ClassNotFound exception). Even if your 2 WARs contain the same jar (e.g. commons-lang.jar) the Class instances are different as they come from a different ClassLoader. If you store those in a shared-lib jar then you most probably end up with the (in)famous “Cannot cast class Xxx to class Xxx”.

One common solution to this problem will look something like:

public class MyInfoStore {
  private Map<ClassLoader, Set<Info>> infoMap;

  public void storeInfo(Info info) {
    ClassLoader tccl = Thread.currentThread().getContextClassLoader(); // probably guarded for SecurityManager
    infoMap.put(tccl, info);
  }
...
}

Of course you must really be careful to clean up this Map during shutdown. In CDI Applications you can use the @Observes BeforeShutdown event to trigger the cleanup.

The impact on us programmers

These various scenarios make it really hard to write any form of portable framework who runs fine inside of EARs. This is not only true for client frameworks but also for container frameworks itself like CDI and Spring.

Integrating CDI containers in EARs

It is pretty obvious that – mostly due to the lack of a guaranteed default isolation scenario in JavaEE – CDI containers have a hard time in finding a nice and portable handling of CDI beans and Extensions in EARs. I got involved in CDI in late 2008 when the name of the spec still was WebBeans. And that name was taken literally – it originally was only targetting web applications and not EARs. The EAR support only got roughly added (according to some interpretations) shortly before the EE-6 specification got published. So there are multiple reasons why CDI in EARs is not really a first class citizen yet.

To my knowledge there are 3 sane ways how a container can integrate CDI in ears. And of those 3 sane ways, 5 are used in various containers 😉

A.) 1 BeanManager per EAR

All the EAR is handled via 1 BeanManager. This is the way JBoss WildFly seems to handle things. First the BeanManager gets created and all it’s CDI Extensions get loaded (TODO research: all or only the ones from the shared libs?).

In reality it’s a bit more complicated. Weld uses a single BeanManager for each and every JAR in your EAR. Don’t ask me why, but that’s what I’ve seen. Still you only get one set of Extensions for your whole EAR. Keep this in mind.

The TCCL always seems to stay the shared EAR ApplicationClassLoader during boot time. Even while processing the classes of the WAR files. Thus the TCCL during @Observes ProcessAnnotatedType will be the shared EAR ApplicationClassLoader. But if you access your Extensions (or the information collected by them) later at runtime you might end up with a different TCCL. This is especially true for all servlet requests in your WARs. This makes it really hard to store anything with the usual Map<ClassLoader, Set<Info>> trick.

B.) 1 BeanManager per WAR + 1 ‘shared’ BeanManager

Each WAR fully boots up his own BeanManager. The shared EAR libs also get an own BeanManager to serve non-servlet requests like JMS and remote EJB invocations. Classes in shared EAR-libs simply get discovered over again for each WAR. Each WAR also has it’s own set of Extensions (they are usually 1:1 to the BeanManager). This is what I’ve seen in Apache Geronimo, IBM WebSphere and early Apache TomEE containers.

This is a bit tricky when it comes to handling of the @ApplicationScoped context (see CDI-129 for more information) or if you handle EJBs (which are by nature shared across the EAR). WebSphere e.g. seems to solve this by having an own Bean<SomeSharedEarLibClass> instance for each BeanManager (and thus for each WAR) but they share a single ApplicationContext storage and those beans are equals(). Probably they just compare the PassivationCapable#getPassivationId()?

It usually works fairly nice and it allows the usage of most common programming patterns. This also allows to modify classes from shared libs via Extensions you register in a single WAR. The downside is that you have to scan and process all the shared classes over and over again for each WAR. This obviously slows down the application boot time.

Oh this mode strictly seen also conflicts with the requirements for modularity of section 5 of the CDI specification. Overall I’m not very happy with section 5 but it should be mentioned.

C.) Hierarchic BeanManagers

In this case we have an 1:1 relation between a ClassLoader and the BeanManager. The shared EAR libs will get scanned by an EAR-BeanManager. Then each of the WARs get scanned via their own WebAppBeanManager. In contrast to scenario B. these WebAppBaenManagers will only scan the classes of the local WARs WEB-INF/lib and WEB-INF/classes and not the shared ear lib over again. If you need information from shared EAR lib classes then the WebAppBeanManger simply delegates to it’s ‘parent’ EAR-BeanManager. Apache TomEE uses this mode since 1.7.x

There are a few tricks the container vendor need to do in this case. E.g. propagating events and Bean lookups to the parent BeanManager, etc. Or to suppress sending CDI Extension Events to the parent (we recently learned this the hard way – is fixed in TomEE-1.7.2 which is to be released soon).

It also has an important impact on CDI Extension programmers: As your Extensions are 1:1 to the BeanManager we now also have the ClassPath split up into different Extension instances. This works fine for ProcessAnnotatedType but can be tricky in some edge cases. E.g. the DeltaSpike MessageBundleException did collect info about a certain ProducerBean and stored in in the Extension for later usage in @Observes AfterBeanDiscovery. Too bad that in my case this certain ProducerMethod was in a shared ear lib and thus gets picked up by the Extension-instance of the EAR-BeanManager but the ‘consumer’ (the interface annotated with @MessageBundle is in some WARs. And the WebAppBeanManager of this WAR obviously is not the one scanning the ProducerMethod of the class in the shared ear lib. Thus the Extension created a NullPointerException. This will be fixed in the upcoming Apache DeltaSpike-1.2.2 release.

The Impact on poor CDI Extension programmers

TODO: this is a living document. I’ll add more info and put it up review.

Advertisements

About struberg
I'm an Apache Software Foundation member blogging about Java, µC, TheASF, OpenWebBeans, Maven, MyFaces, CODI, GIT, OpenJPA, TomEE, DeltaSpike, ...

8 Responses to CDI in EARs

  1. Jens Schumann says:

    Did you omit Servlet Spec 10.7.2 – web application class loader – on purpose? 😉 The servlet spec requires strict .war separation while recommending parent last delegation. The flat classloader mentioned above is – in my opinion – not spec compliant (if they expose .war internal classes).

    You have to consider parent first/parent last in your assertions too, which doesn’t make life easier. Usually you are able to switch between parent first / parent last delegation within your deployment (and .ear artefacts) and your app server, which in my experience, a lot of people do. (Luckily we don’t have to deal with the good old “first class is first” global JBoss Class Repository anymore).

    Regarding CDI BeanManager visibility: Before we fix classloading (haha!) we should understand what we would like to achieve. I for one could imagine use cases for your options A to C. They all seem valid, at least from a user/developer perspective. Is there anything that speaks against option A, B or C (apart from technical difficulties implementing it)?

  2. Thanks for summarizing the topic Mark. Some further comments for A)

    JBoss containers use BeanManager per bean archive. That is because interceptors, decorators and alternatives are enabled per bean archive. Therefore, enablement information can be kept within the particular BeanManager.

    The fact that you get only “one set of Extensions” is given by the spec that says that the container “instantiates a single instance of each service provider” in 12.2. All extensions (no matter if from webapp or shared lib) are handled this way.

  3. struberg says:

    Txs for the pointer, Jens! This looks like another discrepance between the EE umbrella spec and the servlet spec. The other one being what ‘Application’ means. So it will boil down to ‘eat the shit the big vendors are cooking’ – if it technically makes sense or not is another question 🙂
    But even most big EE irons come with parent-first and isolation as default these days.

    LieGrue,
    strub

  4. OmniFaces also struggled with this: http://balusc.blogspot.com/2013/10/cdi-behaved-unexpectedly-in-ear-so.html

    I cite: “It’s beyond me why CDI behaves by default like that and that CDI is apparently not configurable in such way that only one CDI context per WAR of an EAR is created. It’d perhaps make sense if the EAR-wide CDI context was configurable via some configuration setting in a /META-INF/beans.xml in the EAR (not in WAR!), or even implicitly enabled as EAR-wide when a /META-INF/beans.xml is present in the EAR, but it is not. There have been numerous discussions about this, among others in CDI spec issue 129.”.

  5. Jim says:

    I’m not in a CDI context, but I recently had the “cannot cast class Xxx to Xxx” issue. I’m interested in the “common solution” you posted, but I’m curious about your comment about shutdown. Do you have more info or some links that discuss the problem and solution?

    • struberg says:

      Hi Jim!

      If you have the same my-jar.jar in WEB-INF/lib of war1 and in war2 file then the class names are the same – but the class instances are still different (as they are from a different ClassLoader).

      Let’s now take a real example: facelets.jar contained in older Mojarra versions.
      This got fixed a long time, but it’s really a very good example. And even excellent programmers can sometimes fall into this trap. I don’t remember all the details anymore, but I’ll try to sketch it as good as I can.

      Afair this facelets.jar did contain a cache. This was basically a static Map where the key was a String representing the facelet path (e.g. “/WEB-INF/jsf/template.xhtml”) the value of the Map.Entry is an instance of Facelet. Now assume you have a facelet with the same name in your 2 wars (e.g. “/index.xhtml”) and facelet.jar in either your container or your shared ear lib.

      On accessing this page in war1 the compiled facelet will get stored in the shared cache. When accessing the same page in war2, the cache will simply return the instance with the Class from war1. And if you further work with it it will blow up with the mentioned exception.

      The solution for this problem is basically to maintain a
      Map<ClassLoader, Map<String, Facelet>> where the ClassLoader key is the current TCCL. That way you can maintain different caches for each of the webapps in your EAR.

      Of course, keeping a reference on the ClassLoader will essentially block the JVM from unloading it if you undeploy your application (at least if the cache is in a parent ClassLoader, e.g. the container libs).
      To avoid those mem-leaks you really need to carefully clean up all those caches when your application gets undeployed (==remove all the entries which have the very ClassLoader as key) .

      LieGrue,
      strub

  6. Pingback: Building extensible Java EE Applications – Part 1 | {Stephan}Knitelius

  7. Great news: The described issue has been solved in Weld 2.3.5 as per https://issues.jboss.org/browse/WELD-2143

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: