Uploaded image for project: 'Weld'
  1. Weld
  2. WELD-1437

Weld can't coexist with another CDI container in a large multi-module project.

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Done
    • Icon: Major Major
    • 2.1.1.Final
    • 1.1.13.Final
    • Java SE Support
    • None
    • Hide

      Use the following sample to boot OpenWebBeans and fire an event. This will make Weld listen to the AfterBeanDiscovery event:

      package com.company.mavenproject1.boot;
      
      import com.company.mavenproject1.ContainerStarted;
      import java.lang.annotation.Annotation;
      import javax.enterprise.inject.spi.BeanManager;
      import org.apache.webbeans.config.WebBeansContext;
      import org.apache.webbeans.spi.ContainerLifecycle;
      
      public class BootWebBeans implements Runnable {
      
          private final ContainerLifecycle clc = service(ContainerLifecycle.class);
      
          private static <T> T service(Class<T> clazz) {
              return webBeansContext().getService(clazz);
          }
      
          private static WebBeansContext webBeansContext() {
              //return new WebBeansContext();
              return WebBeansContext.getInstance();
          }
      
          public static void main(final String[] args) {
              new BootWebBeans().run();
          }
      
          @Override
          public void run() {
              startApplication();
              try {
                  fireEvent(new ContainerStarted());
              } finally {
                  stopApplication();
              }
          }
      
          private void startApplication() {
              clc.startApplication(this);
          }
      
          private void fireEvent(Object event, Annotation... qualifiers) {
              beanManager().fireEvent(event, qualifiers);
          }
      
          private BeanManager beanManager() {
              return clc.getBeanManager();
          }
      
          private void stopApplication() {
              clc.stopApplication(this);
          }
      }
      
      Show
      Use the following sample to boot OpenWebBeans and fire an event. This will make Weld listen to the AfterBeanDiscovery event: package com.company.mavenproject1.boot; import com.company.mavenproject1.ContainerStarted; import java.lang.annotation.Annotation; import javax.enterprise.inject.spi.BeanManager; import org.apache.webbeans.config.WebBeansContext; import org.apache.webbeans.spi.ContainerLifecycle; public class BootWebBeans implements Runnable { private final ContainerLifecycle clc = service(ContainerLifecycle.class); private static <T> T service( Class <T> clazz) { return webBeansContext().getService(clazz); } private static WebBeansContext webBeansContext() { // return new WebBeansContext(); return WebBeansContext.getInstance(); } public static void main( final String [] args) { new BootWebBeans().run(); } @Override public void run() { startApplication(); try { fireEvent( new ContainerStarted()); } finally { stopApplication(); } } private void startApplication() { clc.startApplication( this ); } private void fireEvent( Object event, Annotation... qualifiers) { beanManager().fireEvent(event, qualifiers); } private BeanManager beanManager() { return clc.getBeanManager(); } private void stopApplication() { clc.stopApplication( this ); } }

      Weld observes AfterBeanDiscovery events with @Default qualifier. However, if another CDI container is present, e.g. if another module is using OpenWebBeans to manage CDI solely for this component, then Weld will receive this event and throw an IllegalStateException:

      Exception in thread "main" java.lang.IllegalStateException: Singleton is not set
      	at org.jboss.weld.bootstrap.api.helpers.IsolatedStaticSingletonProvider$IsolatedStaticSingleton.get(IsolatedStaticSingletonProvider.java:52)
      	at org.jboss.weld.Container.instance(Container.java:54)
      	at org.jboss.weld.context.AbstractContext.<init>(AbstractContext.java:67)
      	at org.jboss.weld.context.AbstractManagedContext.<init>(AbstractManagedContext.java:11)
      	at org.jboss.weld.context.AbstractUnboundContext.<init>(AbstractUnboundContext.java:31)
      	at org.jboss.weld.environment.se.contexts.ThreadContext.<init>(ThreadContext.java:40)
      	at org.jboss.weld.environment.se.WeldSEBeanRegistrant.registerWeldSEContexts(WeldSEBeanRegistrant.java:49)
      	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      	at java.lang.reflect.Method.invoke(Method.java:597)
      	at org.apache.webbeans.event.ObserverMethodImpl.notify(ObserverMethodImpl.java:267)
      	at org.apache.webbeans.event.NotificationManager.fireEvent(NotificationManager.java:474)
      	at org.apache.webbeans.container.BeanManagerImpl.fireEvent(BeanManagerImpl.java:440)
      	at org.apache.webbeans.container.BeanManagerImpl.fireEvent(BeanManagerImpl.java:430)
      	at org.apache.webbeans.config.BeansDeployer.fireAfterBeanDiscoveryEvent(BeansDeployer.java:332)
      	at org.apache.webbeans.config.BeansDeployer.deploy(BeansDeployer.java:192)
      	at org.apache.webbeans.lifecycle.AbstractLifeCycle.startApplication(AbstractLifeCycle.java:129)
      	at com.company.mavenproject1.boot.BootWebBeans.startApplication(BootWebBeans.java:41)
      	at com.company.mavenproject1.boot.BootWebBeans.run(BootWebBeans.java:28)
      	at com.company.mavenproject1.boot.BootWebBeans.main(BootWebBeans.java:23)
      

      I consider this being a major issue because it implies that it's impossible to develop components / modules / bean archives (whatever you call it) which use a private CDI container. As soon as Weld is present on the class path, e.g. to manage CDI for the client application, you will get this IllegalStateException.

              bafco Matus Abaffy (Inactive)
              christians_jira Christian Schlichtherle (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

                Created:
                Updated:
                Resolved: