Arquillian and TomEE embedded: multiple webapps, different CDI extensions

TomEE embedded made several progresses recently and it is nice to use with Arquillian. Since it is fully integrated with Arquillian you can deploy 2, 3 or much more webapps in the TomEE embedded instance…but then you have an issue: CDI. CDI extension mecanism is based on Java ServiceLoader mecanism. It means it uses META-INF/services/* to load its extensions. The issue: TomEE embedded is embedded. Of course it shares its container classloader with all applications but in this case you’ll get in 90% of cases your extension as well (cause of Maven, gradle, …). So it means if you deploy your application with the desired CDI extension and another (a test one) where you don’t want them you’ll get trouble since the extension will be loaded.

How to avoid it?

There are several solutions like putting in the test Archive all is needed, activating the container classloader scanning but that’s not exactly what we want and there is since few weeks another one: switch the OpenWebBeans LoaderService by a custom one skipping extensions.

To do so, simply add in the test Archive a WEB-INF/ containing:

org.apache.webbeans.spi.LoaderService = org.superbiz.MyLoader

With ShrinkWrap it often looks like:

  new StringAsset(
    LoaderService.class.getName() + " = " + MyLoader.class.getName()),

And the loader implementation can be as simple as the following one if you want to skip all extensions:

// could extend DefaultLoaderService as well
public static class EmptyLoaderService extends OptimizedLoaderService {
  protected List<? extends Extension> loadExtensions(final ClassLoader classLoader) {
    return Collections.emptyList();

And here it is. The test application will use this loader to discover CDI extensions so it means nothing will be activated and if the extension relies on some particular beans it will not make the deployment fail :).


Leave a Reply

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

You are commenting using your 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 )

Connecting to %s