3 Replies Latest reply on Jun 30, 2014 4:49 PM by Keith Babo

    Two instances of @ApplicationScoped bean?

    Artem Egorkine Newbie

      Hi,

       

      I have a UserRegistry bean in my application that is annotated as @ApplicationScoped. It is then injected into a number of service beans. As far as I understand, there should be exactly one instance of the bean created, no? I noticed that, in fact, I have two. The first one is created during the WAR deployment. The second one is created during the first time any service (where UserRegistry is injected) is accessed. My services are CDI beans with HTTP interfaces.

       

      Here's a trace on the first instantiation (this = com.esb.user.UsersRepository$Proxy$_$$_WeldClientProxy@d53240):

       

      MSC service thread 1-11@1677, prio=5, in group 'main', status: 'RUNNING'
            at com.esb.user.UsersRepository.<init>(UsersRepository.java:21)
            at com.esb.user.UsersRepository$Proxy$_$$_WeldClientProxy.<init>(UsersRepository$Proxy$_$$_WeldClientProxy.java:-1)
            at sun.reflect.NativeConstructorAccessorImpl.newInstance0(NativeConstructorAccessorImpl.java:-1)
            at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
            at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
            at java.lang.Class.newInstance0(Class.java:357)
            at java.lang.Class.newInstance(Class.java:310)
            at org.jboss.weld.util.reflection.SecureReflections$16.work(SecureReflections.java:344)
            at org.jboss.weld.util.reflection.SecureReflectionAccess.run(SecureReflectionAccess.java:52)
            at org.jboss.weld.util.reflection.SecureReflectionAccess.runAsInstantiation(SecureReflectionAccess.java:173)
            at org.jboss.weld.util.reflection.SecureReflections.newInstance(SecureReflections.java:341)
            at org.jboss.weld.bean.proxy.ProxyFactory.create(ProxyFactory.java:242)
            at org.jboss.weld.bean.proxy.ClientProxyFactory.create(ClientProxyFactory.java:105)
            at org.jboss.weld.bean.proxy.ClientProxyProvider.createClientProxy(ClientProxyProvider.java:87)
            at org.jboss.weld.bean.proxy.ClientProxyProvider.access$000(ClientProxyProvider.java:43)
            at org.jboss.weld.bean.proxy.ClientProxyProvider$1.apply(ClientProxyProvider.java:53)
            at org.jboss.weld.bean.proxy.ClientProxyProvider$1.apply(ClientProxyProvider.java:46)
            at com.google.common.collect.ComputingConcurrentHashMap$ComputingValueReference.compute(ComputingConcurrentHashMap.java:358)
            at com.google.common.collect.ComputingConcurrentHashMap$ComputingSegment.compute(ComputingConcurrentHashMap.java:184)
            at com.google.common.collect.ComputingConcurrentHashMap$ComputingSegment.getOrCompute(ComputingConcurrentHashMap.java:153)
            at com.google.common.collect.ComputingConcurrentHashMap.getOrCompute(ComputingConcurrentHashMap.java:69)
            at com.google.common.collect.ComputingConcurrentHashMap$ComputingMapAdapter.get(ComputingConcurrentHashMap.java:396)
            at org.jboss.weld.bean.proxy.ClientProxyProvider.getClientProxy(ClientProxyProvider.java:102)
            at org.jboss.weld.manager.BeanManagerImpl.getReference(BeanManagerImpl.java:603)
            at org.jboss.weld.manager.BeanManagerImpl.getReference(BeanManagerImpl.java:674)
            at org.jboss.weld.injection.FieldInjectionPoint.inject(FieldInjectionPoint.java:134)
            at org.jboss.weld.util.Beans.injectBoundFields(Beans.java:763)
            at org.jboss.weld.util.Beans.injectFieldsAndInitializers(Beans.java:772)
            at org.jboss.weld.bean.ManagedBean$ManagedBeanInjectionTarget$1$1.proceed(ManagedBean.java:165)
            at org.jboss.weld.injection.InjectionContextImpl.run(InjectionContextImpl.java:48)
            at org.jboss.weld.bean.ManagedBean$ManagedBeanInjectionTarget$1.work(ManagedBean.java:161)
            at org.jboss.weld.bean.ManagedBean$FixInjectionPoint.run(ManagedBean.java:135)
            at org.jboss.weld.bean.ManagedBean$ManagedBeanInjectionTarget.inject(ManagedBean.java:157)
            at org.jboss.weld.bean.ManagedBean.create(ManagedBean.java:297)
            at org.jboss.weld.context.unbound.DependentContextImpl.get(DependentContextImpl.java:68)
            at org.jboss.weld.manager.BeanManagerImpl.getReference(BeanManagerImpl.java:608)
            at org.jboss.weld.manager.BeanManagerImpl.getReference(BeanManagerImpl.java:635)
            at org.switchyard.component.bean.deploy.CDIBeanServiceDescriptor.getHandler(CDIBeanServiceDescriptor.java:81)
            at org.switchyard.component.bean.deploy.BeanComponentActivator.activateService(BeanComponentActivator.java:80)
            at org.switchyard.deploy.internal.Deployment.deployImplementations(Deployment.java:546)
            at org.switchyard.deploy.internal.Deployment.start(Deployment.java:144)
            at org.switchyard.as7.extension.deployment.SwitchYardDeployment.start(SwitchYardDeployment.java:121)
            at org.switchyard.as7.extension.services.SwitchYardService.start(SwitchYardService.java:74)
            at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811)
            at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746)
            at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
            at java.lang.Thread.run(Thread.java:662)
      
      
      

      The second one (this = com.esb.user.UsersRepository@30ffcdae):

       

      http-/127.0.0.1:8080-1@17031 daemon, prio=5, in group 'main', status: 'RUNNING'
            at com.esb.user.UsersRepository.<init>(UsersRepository.java:21)
            at sun.reflect.NativeConstructorAccessorImpl.newInstance0(NativeConstructorAccessorImpl.java:-1)
            at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
            at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
            at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
            at org.jboss.weld.introspector.jlr.WeldConstructorImpl.newInstance(WeldConstructorImpl.java:206)
            at org.jboss.weld.injection.ConstructorInjectionPoint.newInstance(ConstructorInjectionPoint.java:115)
            at org.jboss.weld.bean.ManagedBean.createInstance(ManagedBean.java:340)
            at org.jboss.weld.bean.ManagedBean$ManagedBeanInjectionTarget.produce(ManagedBean.java:204)
            at org.jboss.weld.bean.ManagedBean.create(ManagedBean.java:296)
            at org.jboss.weld.context.AbstractContext.get(AbstractContext.java:101)
            at org.jboss.weld.bean.proxy.ContextBeanInstance.getInstance(ContextBeanInstance.java:90)
            at org.jboss.weld.bean.proxy.ProxyMethodHandler.invoke(ProxyMethodHandler.java:79)
            at com.esb.user.UsersRepository$Proxy$_$$_WeldClientProxy.getUserById(UsersRepository$Proxy$_$$_WeldClientProxy.java:-1)
            at com.esb.settings.DeviceSettingsServiceBean.process(DeviceSettingsServiceBean.java:47)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethodAccessorImpl.java:-1)
            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.switchyard.component.bean.ServiceProxyHandler.handle(ServiceProxyHandler.java:163)
            at org.switchyard.component.bean.ServiceProxyHandler.handleMessage(ServiceProxyHandler.java:94)
            at org.switchyard.bus.camel.processors.ProviderProcessor.process(ProviderProcessor.java:29)
            at org.apache.camel.util.AsyncProcessorConverterHelper$ProcessorToAsyncProcessorBridge.process(AsyncProcessorConverterHelper.java:61)
            at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:73)
            at org.apache.camel.processor.DelegateAsyncProcessor.processNext(DelegateAsyncProcessor.java:99)
            at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:90)
            at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:73)
            at org.apache.camel.processor.DelegateAsyncProcessor.processNext(DelegateAsyncProcessor.java:99)
            at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:90)
            at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:73)
            at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:73)
            at org.apache.camel.processor.DelegateAsyncProcessor.processNext(DelegateAsyncProcessor.java:99)
            at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:90)
            at org.apache.camel.processor.interceptor.TraceInterceptor.process(TraceInterceptor.java:91)
            at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:73)
            at org.apache.camel.processor.DelegateAsyncProcessor.processNext(DelegateAsyncProcessor.java:99)
            at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:90)
            at org.switchyard.bus.camel.audit.FaultProcessor.process(FaultProcessor.java:46)
            at org.apache.camel.processor.RouteContextProcessor.processNext(RouteContextProcessor.java:45)
            at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:90)
            at org.apache.camel.processor.interceptor.DefaultChannel.process(DefaultChannel.java:303)
            at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:73)
            at org.apache.camel.processor.Pipeline.process(Pipeline.java:117)
            at org.apache.camel.processor.Pipeline.process(Pipeline.java:80)
            at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:73)
            at org.apache.camel.processor.TryProcessor.process(TryProcessor.java:104)
            at org.apache.camel.processor.TryProcessor.process(TryProcessor.java:78)
            at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:73)
            at org.apache.camel.processor.DelegateAsyncProcessor.processNext(DelegateAsyncProcessor.java:99)
            at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:90)
            at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:73)
            at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:73)
            at org.apache.camel.processor.DelegateAsyncProcessor.processNext(DelegateAsyncProcessor.java:99)
            at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:90)
            at org.apache.camel.processor.interceptor.TraceInterceptor.process(TraceInterceptor.java:91)
            at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:73)
            at org.apache.camel.processor.DelegateAsyncProcessor.processNext(DelegateAsyncProcessor.java:99)
            at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:90)
            at org.switchyard.bus.camel.audit.FaultProcessor.process(FaultProcessor.java:46)
            at org.apache.camel.processor.RouteContextProcessor.processNext(RouteContextProcessor.java:45)
            at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:90)
            at org.apache.camel.processor.interceptor.DefaultChannel.process(DefaultChannel.java:303)
            at org.apache.camel.processor.RouteContextProcessor.processNext(RouteContextProcessor.java:45)
            at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:90)
            at org.apache.camel.processor.UnitOfWorkProcessor.process(UnitOfWorkProcessor.java:122)
            at org.apache.camel.processor.RouteInflightRepositoryProcessor.processNext(RouteInflightRepositoryProcessor.java:48)
            at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:90)
            at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:73)
            at org.apache.camel.processor.DelegateAsyncProcessor.processNext(DelegateAsyncProcessor.java:99)
            at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:90)
            at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:73)
            at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:73)
            at org.apache.camel.component.direct.DirectProducer.process(DirectProducer.java:61)
            at org.apache.camel.processor.UnitOfWorkProcessor.processAsync(UnitOfWorkProcessor.java:150)
            at org.apache.camel.processor.UnitOfWorkProcessor.process(UnitOfWorkProcessor.java:117)
            at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:99)
            at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:86)
            at org.apache.camel.processor.UnitOfWorkProducer.process(UnitOfWorkProducer.java:63)
            at org.apache.camel.impl.ProducerCache$2.doInProducer(ProducerCache.java:366)
            at org.apache.camel.impl.ProducerCache$2.doInProducer(ProducerCache.java:337)
            at org.apache.camel.impl.ProducerCache.doInProducer(ProducerCache.java:233)
            at org.apache.camel.impl.ProducerCache.sendExchange(ProducerCache.java:337)
            at org.apache.camel.impl.ProducerCache.send(ProducerCache.java:159)
            at org.apache.camel.impl.DefaultProducerTemplate.send(DefaultProducerTemplate.java:106)
            at org.apache.camel.impl.DefaultProducerTemplate.send(DefaultProducerTemplate.java:92)
            at org.switchyard.bus.camel.ExchangeDispatcher.dispatch(ExchangeDispatcher.java:87)
            at org.switchyard.bus.camel.CamelExchange.sendInternal(CamelExchange.java:234)
            at org.switchyard.bus.camel.CamelExchange.send(CamelExchange.java:171)
            at org.switchyard.component.http.InboundHandler.invoke(InboundHandler.java:118)
            at org.switchyard.component.http.HttpGatewayServlet.handle(HttpGatewayServlet.java:143)
            at org.switchyard.component.http.HttpGatewayServlet.service(HttpGatewayServlet.java:114)
            at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
            at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:295)
            at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
            at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
            at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:149)
            at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:145)
            at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:97)
            at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:102)
            at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:336)
            at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
            at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:653)
            at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:920)
            at java.lang.Thread.run(Thread.java:662)
      
      

      Do you have any idea, what is going on?

       

      Regards,

      Artem

        • 1. Re: Two instances of @ApplicationScoped bean?
          Keith Babo Master

          That's curious.  It would be interesting to see if the BeanManager reference is the same each time the bean is created.  We don't create the bean directly from SY code, that's managed by CDI/Weld.  Some possibilities:

           

          • Somehow two instances of the BeanManager are being used.  This can happen if, for example, the same ENC is not used during deployment and runtime.
          • Possibly a peculiarity with CDI/Weld that results in the bean being instantiated twice.  I'm not sure if the spec prohibits multiple instantiation, it seems the important part is that the same bean is used for all invocations within the app.
          • 2. Re: Two instances of @ApplicationScoped bean?
            Artem Egorkine Newbie

            It crossed my mind to check, if the first instance gets destroyed before the other one is created, but i haven't gotten around to it yet. When I do, I'll try to check for BeanManager as well. What is ENC?

             

            So far I know that if I remove @ApplicationScoped annotation, I get 6 independent ininstances for 6 service beans that inject this one. All of them of real type, not the proxy one.

            • 3. Re: Two instances of @ApplicationScoped bean?
              Keith Babo Master

              ENC = Enterprise Naming Context.  Shorthand way of talking about stuff retrieved via "java:comp" JNDI lookup in an EE app.  There is one per application and it's the place where the CDI BeanManager is stored/retrieved.