4 Replies Latest reply on Feb 11, 2013 10:10 AM by matlach

    producer.produce(ctx) issue in weld 2.0.0.Beta3

    matlach

      Hello everyone,

       

      I'm migrating from Weld SE 1.1.9.Final to Weld SE 2.0.0.Beta3 and I'm having issue to invoke producer method with unbounded creational context.

      The following piece of code that was working fine in the 1.1.9.Final version :

       

       

          public <T, X> void onProcessProducer(@Observes ProcessProducer<T, Configuration> event, BeanManager beanManager)
          {
              CacheName cacheName = event.getAnnotatedMember().getAnnotation(CacheName.class);
              if (cacheName == null)
              {
                  throw new NullPointerException("@CacheName must be defined");
              }
              CreationalContext<Configuration> ctx = beanManager.createCreationalContext(null);
              Configuration cacheConfiguration = event.getProducer().produce(ctx);
              configurations.put(cacheName.value(), cacheConfiguration);
          }
      
      

       

      but I get the following stack trace in the 2.0.0.Beta3 version :

       

      org.jboss.weld.exceptions.DefinitionException: Exception List with 1 exceptions:
      Exception 0 :
      org.jboss.weld.exceptions.IllegalArgumentException: WELD-001456 Argument bean must not be null
                at org.jboss.weld.util.Preconditions.checkArgumentNotNull(Preconditions.java:40)
                at org.jboss.weld.manager.BeanManagerImpl.getReference(BeanManagerImpl.java:715)
                at org.jboss.weld.bean.ManagedBean.getUnboundRequestContext(ManagedBean.java:272)
                at org.jboss.weld.bean.ManagedBean.create(ManagedBean.java:167)
                at org.jboss.weld.context.unbound.DependentContextImpl.get(DependentContextImpl.java:69)
                at org.jboss.weld.manager.BeanManagerImpl.getReference(BeanManagerImpl.java:699)
                at org.jboss.weld.injection.producer.AbstractMemberProducer.getReceiver(AbstractMemberProducer.java:116)
                at org.jboss.weld.injection.producer.AbstractMemberProducer.produce(AbstractMemberProducer.java:185)
                at XXX.core.cache.InfinispanExtension.onProcessProducer(InfinispanExtension.java:43)
                at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
                at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
                at java.lang.reflect.Method.invoke(Method.java:601)
                at org.jboss.weld.util.reflection.SecureReflections$13.work(SecureReflections.java:268)
                at org.jboss.weld.util.reflection.SecureReflectionAccess.run(SecureReflectionAccess.java:52)
                at org.jboss.weld.util.reflection.SecureReflectionAccess.runAsInvocation(SecureReflectionAccess.java:137)
                at org.jboss.weld.util.reflection.SecureReflections.invoke(SecureReflections.java:264)
                at org.jboss.weld.annotated.runtime.InvokableAnnotatedMethod.invokeOnInstance(InvokableAnnotatedMethod.java:82)
                at org.jboss.weld.injection.MethodInjectionPoint.invokeOnInstanceWithSpecialValue(MethodInjectionPoint.java:97)
                at org.jboss.weld.event.ObserverMethodImpl.sendEvent(ObserverMethodImpl.java:273)
                at org.jboss.weld.event.ExtensionObserverMethodImpl.sendEvent(ExtensionObserverMethodImpl.java:129)
                at org.jboss.weld.event.ObserverMethodImpl.sendEvent(ObserverMethodImpl.java:260)
                at org.jboss.weld.event.ObserverMethodImpl.notify(ObserverMethodImpl.java:239)
                at org.jboss.weld.event.ObserverNotifier.notifyObserver(ObserverNotifier.java:155)
                at org.jboss.weld.event.ObserverNotifier.notifyObservers(ObserverNotifier.java:116)
                at org.jboss.weld.event.ObserverNotifier.fireEvent(ObserverNotifier.java:101)
                at org.jboss.weld.bootstrap.events.AbstractContainerEvent.fire(AbstractContainerEvent.java:69)
                at org.jboss.weld.bootstrap.events.AbstractDefinitionContainerEvent.fire(AbstractDefinitionContainerEvent.java:38)
                at org.jboss.weld.bootstrap.events.ProcessProducerImpl.fire(ProcessProducerImpl.java:34)
                at org.jboss.weld.bootstrap.events.ContainerLifecycleEvents.fireProcessProducer(ContainerLifecycleEvents.java:211)
                at org.jboss.weld.bootstrap.AbstractBeanDeployer.fireBeanEvents(AbstractBeanDeployer.java:141)
                at org.jboss.weld.bootstrap.AbstractBeanDeployer.fireBeanEvents(AbstractBeanDeployer.java:133)
                at org.jboss.weld.bootstrap.BeanDeployer.deploy(BeanDeployer.java:313)
                at org.jboss.weld.bootstrap.BeanDeployment.deployBeans(BeanDeployment.java:267)
                at org.jboss.weld.bootstrap.WeldBootstrap.deployBeans(WeldBootstrap.java:462)
                at org.jboss.weld.bootstrap.api.helpers.ForwardingBootstrap.deployBeans(ForwardingBootstrap.java:70)
                at org.jboss.weld.environment.se.Weld.initialize(Weld.java:137)
                at XXX.integration.WeldRunner.initializeWeld(WeldRunner.java:35)
                at XXX.integration.WeldRunner.run(WeldRunner.java:20)
                at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
                at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
                at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
                at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
                at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
                at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
      
                at org.jboss.weld.bootstrap.events.AbstractDefinitionContainerEvent.fire(AbstractDefinitionContainerEvent.java:40)
                at org.jboss.weld.bootstrap.events.ProcessProducerImpl.fire(ProcessProducerImpl.java:34)
                at org.jboss.weld.bootstrap.events.ContainerLifecycleEvents.fireProcessProducer(ContainerLifecycleEvents.java:211)
                at org.jboss.weld.bootstrap.AbstractBeanDeployer.fireBeanEvents(AbstractBeanDeployer.java:141)
                at org.jboss.weld.bootstrap.AbstractBeanDeployer.fireBeanEvents(AbstractBeanDeployer.java:133)
                at org.jboss.weld.bootstrap.BeanDeployer.deploy(BeanDeployer.java:313)
                at org.jboss.weld.bootstrap.BeanDeployment.deployBeans(BeanDeployment.java:267)
                at org.jboss.weld.bootstrap.WeldBootstrap.deployBeans(WeldBootstrap.java:462)
                at org.jboss.weld.bootstrap.api.helpers.ForwardingBootstrap.deployBeans(ForwardingBootstrap.java:70)
                at org.jboss.weld.environment.se.Weld.initialize(Weld.java:137)
                at XXX.integration.WeldRunner.initializeWeld(WeldRunner.java:35)
                at XXX.integration.WeldRunner.run(WeldRunner.java:20)
                at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
                at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
                at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
                at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
                at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
                at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
      
      

       

      Any ideas how I could migrate my code to make this work again ?

       

      Thanks,

        • 1. Re: producer.produce(ctx) issue in weld 2.0.0.Beta3
          mkouba

          Hi Mathieu,

           

          I don't think calling Producer.produce(CreationalContext<T>) during application init is quite legal (at least on the internal producer construct).

           

          See javax.enterprise.inject.spi.Producer javadoc:

          "If the Producer represents a producer field or method, this will invoke the producer method on, or access the producer field of, a contextual instance of the bean that declares the producer."

          -> Beans and contexts needn't be fully initialized/deployed at this time

           

          For the similar reason some methods on BeanManager (e.g. getBeans()) aren't available during application init (see CDI-274, in CDI 1.1).

           

          Regarding the cause of the issue.. In CDI 1.1 the built-in request context must be active during @PostConstruct callback of any bean. So Weld tries to active built-in unbound request context. However it's not yet available (or its bean respectively).

           

          And why do you need to call the producer.produce() during onProcessProducer()?

          • 2. Re: producer.produce(ctx) issue in weld 2.0.0.Beta3
            matlach

            Thanks Martin for your answer,

             

            I now understand better with your explanation as well of the https://issues.jboss.org/browse/CDI-274 issue.

             

            By looking back at my code (that I wrote some months ago) I'm not sure I still need to be called at that point.

            It can for sure be delayed during onAfterDeploymentValidation when https://issues.jboss.org/browse/CDI-315 will be resolved.

             

            If I remember correctly, at that moment, my difficulty was to use an injected member in a producer method which I'm not sure if it is legal or doable.

            • 3. Re: producer.produce(ctx) issue in weld 2.0.0.Beta3
              mkouba

              Hm, I believe this should work:

              class Foo {
              
                @Inject
                Bar bar;
              
                @Produces
                public Qux produce() {
                 bar.invoke();
                 return new Qux();
                }
              }

              ... if that's the use-case you talk about

              • 4. Re: producer.produce(ctx) issue in weld 2.0.0.Beta3
                matlach

                I will try it for sure !

                Thanks you Martin.