6 Replies Latest reply on Apr 29, 2014 12:39 PM by kcbabo

    Alternative not injected

    jorgemoralespou_2

      Hi,

      I have a project consisting of 3 artifacts:

      • Interface definition (Logger)
      • Implementation (defined with @Alternative): AlternativeLogger
      • Service, with Bean component (with injection of Logger interface) and default implementation of interface: DefaultLogger.

       

      In the service, I have set in the service artifact's bean.xml the alternative to use, but it is not used. It always uses the default implementation, that is bundled within same jar file. Dependencies are correctly set. And in log traces, there seems to be some logs saying that the alternative is found, but it does not get correctly injected:

       

      09:14:11,458 DEBUG [org.jboss.weld.Bootstrap] (MSC service thread 1-2) WELD-000103 Enabled alternatives for Manager
      Enabled alternatives: [<class>com.example.switchyard.cdi_alternative_alternative.AlternativeLogger</class> in vfs:/content/cdi-alternative-injected-0.0.1-SNAPSHOT.jar/META-INF/beans.xml@5] []
      Registered contexts: [interface javax.inject.Singleton, interface javax.enterprise.context.RequestScoped, interface javax.enterprise.context.ApplicationScoped, interface javax.enterprise.context.SessionScoped, interface javax.enterprise.context.ConversationScoped, interface javax.enterprise.context.Dependent]
      Registered beans: 0
      

       

       

      There is a JIRA opened for this: [SWITCHYARD-2058] SwitchYard not injecting an alternative CDI Bean - JBoss Issue Tracker

       

      Linked in the JIRA there is a reproducer.

       

      Am I missing something?

        • 1. Re: Alternative not injected
          kcbabo

          This looks like an EAP issue and not something specific to SwitchYard.  Have you tested injection of alternatives in different modules with a vanilla Java EE application?

          • 2. Re: Re: Alternative not injected
            jorgemoralespou_2

            Hi keith,

            Nope, I haven't tested with plain vanilla Java EE. I suspected this worked, as it has been around for some time now. I'll try to test and come back to you.

            Also a note, in Junit tests it does work.

             

            I have updated the reproducer to have another Alternative Bean defined in same jar, so Service Bean as 2 injections, with 2 alternative beans, one defined in same jar, and the other defined in a separate jar. Output is the following:

             

            When run in EAP:

             

            14:17:25,017 INFO  [stdout] (Camel (camel-38) thread #43 - file:///tmp) Default: Jorge
            14:17:25,018 INFO  [stdout] (Camel (camel-38) thread #43 - file:///tmp) AternativePrinter: Jorge
            

             

            When run in JUnit

             

            Alternative: Jorge
            AternativePrinter: Jorge
            
            • 3. Re: Re: Alternative not injected
              kcbabo

              Yeah, I think this has to do with visibility of modules during CDI discovery/injection.  It makes sense that it works in the test environment since all dependencies are wide open on the build class path.

              • 4. Re: Re: Re: Alternative not injected
                jorgemoralespou_2

                Hi Keith,

                Maybe you are right. I have used jboss-cdi-alternative quickstart and added my alternative and it does not get picked either.

                15:52:00,561 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-2) JBAS015876: Starting deployment of "jboss-cdi-alternative.war" (runtime-name: "jboss-cdi-alternative.war")
                15:52:03,610 DEBUG [org.jboss.weld.deployer] (MSC service thread 1-2) Found beans.xml: "/content/jboss-cdi-alternative.war/WEB-INF/beans.xml"
                15:52:06,297 INFO  [org.jboss.weld.deployer] (MSC service thread 1-1) JBAS016002: Processing weld deployment jboss-cdi-alternative.war
                15:52:06,302 DEBUG [org.switchyard] (MSC service thread 1-1) Ignoring deployment unit 'jboss-cdi-alternative.war' as it is not recognized as being a SwitchYard Application.
                15:52:07,313 INFO  [org.jboss.weld.deployer] (MSC service thread 1-2) JBAS016005: Starting Services for CDI deployment: jboss-cdi-alternative.war
                15:52:08,037 INFO  [org.jboss.weld.deployer] (MSC service thread 1-2) JBAS016008: Starting weld service for deployment jboss-cdi-alternative.war
                15:52:08,083 DEBUG [org.jboss.weld.Bootstrap] (MSC service thread 1-2) WELD-000103 Enabled alternatives for Manager
                Enabled alternatives: [<class>com.example.switchyard.cdi_alternative_alternative.AlternativeLogger</class> in vfs:/content/jboss-cdi-alternative.war/WEB-INF/beans.xml@22, <class>org.jboss.as.quickstarts.cdi.alternative.TaxImpl_2</class> in vfs:/content/jboss-cdi-alternative.war/WEB-INF/beans.xml@21] []
                Registered contexts: [interface javax.inject.Singleton, interface javax.enterprise.context.RequestScoped, interface javax.enterprise.context.ApplicationScoped, interface javax.enterprise.context.SessionScoped, interface javax.enterprise.context.ConversationScoped, interface javax.enterprise.context.Dependent]
                Registered beans: 0
                : [<class>com.example.switchyard.cdi_alternative_alternative.AlternativeLogger</class> in vfs:/content/jboss-cdi-alternative.war/WEB-INF/beans.xml@22, <class>org.jboss.as.quickstarts.cdi.alternative.TaxImpl_2</class> in vfs:/content/jboss-cdi-alternative.war/WEB-INF/beans.xml@21] []
                15:52:08,084 DEBUG [org.jboss.weld.Bootstrap] (MSC service thread 1-2) WELD-000104 Enabled decorator types for Manager
                Enabled alternatives: [<class>com.example.switchyard.cdi_alternative_alternative.AlternativeLogger</class> in vfs:/content/jboss-cdi-alternative.war/WEB-INF/beans.xml@22, <class>org.jboss.as.quickstarts.cdi.alternative.TaxImpl_2</class> in vfs:/content/jboss-cdi-alternative.war/WEB-INF/beans.xml@21] []
                Registered contexts: [interface javax.inject.Singleton, interface javax.enterprise.context.RequestScoped, interface javax.enterprise.context.ApplicationScoped, interface javax.enterprise.context.SessionScoped, interface javax.enterprise.context.ConversationScoped, interface javax.enterprise.context.Dependent]
                Registered beans: 0
                : []
                15:52:08,085 DEBUG [org.jboss.weld.Bootstrap] (MSC service thread 1-2) WELD-000105 Enabled interceptor types for Manager
                Enabled alternatives: [<class>com.example.switchyard.cdi_alternative_alternative.AlternativeLogger</class> in vfs:/content/jboss-cdi-alternative.war/WEB-INF/beans.xml@22, <class>org.jboss.as.quickstarts.cdi.alternative.TaxImpl_2</class> in vfs:/content/jboss-cdi-alternative.war/WEB-INF/beans.xml@21] []
                Registered contexts: [interface javax.inject.Singleton, interface javax.enterprise.context.RequestScoped, interface javax.enterprise.context.ApplicationScoped, interface javax.enterprise.context.SessionScoped, interface javax.enterprise.context.ConversationScoped, interface javax.enterprise.context.Dependent]
                Registered beans: 0
                : []
                15:52:08,086 DEBUG [org.jboss.weld.Bootstrap] (MSC service thread 1-2) WELD-000103 Enabled alternatives for Manager
                Enabled alternatives: [] []
                Registered contexts: [interface javax.inject.Singleton, interface javax.enterprise.context.RequestScoped, interface javax.enterprise.context.ApplicationScoped, interface javax.enterprise.context.SessionScoped, interface javax.enterprise.context.ConversationScoped, interface javax.enterprise.context.Dependent]
                Registered beans: 0
                : [] []
                15:52:08,087 DEBUG [org.jboss.weld.Bootstrap] (MSC service thread 1-2) WELD-000104 Enabled decorator types for Manager
                Enabled alternatives: [] []
                Registered contexts: [interface javax.inject.Singleton, interface javax.enterprise.context.RequestScoped, interface javax.enterprise.context.ApplicationScoped, interface javax.enterprise.context.SessionScoped, interface javax.enterprise.context.ConversationScoped, interface javax.enterprise.context.Dependent]
                Registered beans: 0
                : []
                15:52:08,087 DEBUG [org.jboss.weld.Bootstrap] (MSC service thread 1-2) WELD-000105 Enabled interceptor types for Manager
                Enabled alternatives: [] []
                Registered contexts: [interface javax.inject.Singleton, interface javax.enterprise.context.RequestScoped, interface javax.enterprise.context.ApplicationScoped, interface javax.enterprise.context.SessionScoped, interface javax.enterprise.context.ConversationScoped, interface javax.enterprise.context.Dependent]
                Registered beans: 0
                : []
                15:52:08,286 DEBUG [org.jboss.weld.Bootstrap] (MSC service thread 1-2) WELD-000106 Bean: Built-in Bean [javax.validator.ValidatorFactory] with qualifiers [@Default]
                15:52:08,287 DEBUG [org.jboss.weld.Bootstrap] (MSC service thread 1-2) WELD-000106 Bean: org.jboss.weld.bean-jboss-cdi-alternative.war/content/jboss-cdi-alternative.war/WEB-INF/classes-Built-in-org.jboss.weld.context.http.HttpSessionContext
                15:52:08,289 DEBUG [org.jboss.weld.Bootstrap] (MSC service thread 1-2) WELD-000106 Bean: Implicit Bean [javax.enterprise.inject.spi.InjectionPoint] with qualifiers [@Default]
                15:52:08,291 DEBUG [org.jboss.weld.Bootstrap] (MSC service thread 1-2) WELD-000106 Bean: org.jboss.weld.bean-jboss-cdi-alternative.war/content/jboss-cdi-alternative.war/WEB-INF/classes-Built-in-javax.enterprise.context.Conversation
                15:52:08,293 DEBUG [org.jboss.weld.Bootstrap] (MSC service thread 1-2) WELD-000106 Bean: org.jboss.weld.bean-jboss-cdi-alternative.war/content/jboss-cdi-alternative.war/WEB-INF/classes-Built-in-org.jboss.weld.context.bound.BoundRequestContext
                15:52:08,294 DEBUG [org.jboss.weld.Bootstrap] (MSC service thread 1-2) WELD-000106 Bean: org.jboss.weld.bean-jboss-cdi-alternative.war/content/jboss-cdi-alternative.war/WEB-INF/classes-Built-in-org.jboss.weld.context.bound.BoundConversationContext
                15:52:08,294 DEBUG [org.jboss.weld.Bootstrap] (MSC service thread 1-2) WELD-000106 Bean: org.jboss.weld.bean-jboss-cdi-alternative.war/content/jboss-cdi-alternative.war/WEB-INF/classes-Built-in-org.jboss.weld.context.RequestContext
                15:52:08,295 DEBUG [org.jboss.weld.Bootstrap] (MSC service thread 1-2) WELD-000106 Bean: org.jboss.weld.bean-jboss-cdi-alternative.war/content/jboss-cdi-alternative.war/WEB-INF/classes-Built-in-org.jboss.weld.context.ejb.EjbRequestContext
                15:52:08,297 DEBUG [org.jboss.weld.Bootstrap] (MSC service thread 1-2) WELD-000106 Bean: Implicit Bean [javax.enterprise.event.Event] with qualifiers [@Default]
                15:52:08,298 DEBUG [org.jboss.weld.Bootstrap] (MSC service thread 1-2) WELD-000106 Bean: org.jboss.weld.bean-jboss-cdi-alternative.war/content/jboss-cdi-alternative.war/WEB-INF/classes-Built-in-org.jboss.weld.context.http.HttpConversationContext
                15:52:08,300 DEBUG [org.jboss.weld.Bootstrap] (MSC service thread 1-2) WELD-000106 Bean: Built-in Bean [javax.transaction.UserTransaction] with qualifiers [@Default]
                15:52:08,300 DEBUG [org.jboss.weld.Bootstrap] (MSC service thread 1-2) WELD-000106 Bean: org.jboss.weld.bean-jboss-cdi-alternative.war/content/jboss-cdi-alternative.war/WEB-INF/classes-Built-in-org.jboss.weld.context.bound.BoundSessionContext
                15:52:08,302 DEBUG [org.jboss.weld.Bootstrap] (MSC service thread 1-2) WELD-000106 Bean: Built-in Bean [java.security.Principal] with qualifiers [@Default]
                15:52:08,303 DEBUG [org.jboss.weld.Bootstrap] (MSC service thread 1-2) WELD-000106 Bean: Managed Bean [class org.jboss.as.quickstarts.cdi.alternative.Demo] with qualifiers [@Any @Default]
                15:52:08,304 DEBUG [org.jboss.weld.Bootstrap] (MSC service thread 1-2) WELD-000106 Bean: org.jboss.weld.bean-jboss-cdi-alternative.war/content/jboss-cdi-alternative.war/WEB-INF/classes-Built-in-org.jboss.weld.context.http.HttpRequestContext
                15:52:08,305 DEBUG [org.jboss.weld.Bootstrap] (MSC service thread 1-2) WELD-000106 Bean: org.jboss.weld.bean-jboss-cdi-alternative.war/content/jboss-cdi-alternative.war/WEB-INF/classes-Built-in-org.jboss.weld.context.DependentContext
                15:52:08,307 DEBUG [org.jboss.weld.Bootstrap] (MSC service thread 1-2) WELD-000106 Bean: Built-in Bean [javax.validator.Validator] with qualifiers [@Default]
                15:52:08,307 DEBUG [org.jboss.weld.Bootstrap] (MSC service thread 1-2) WELD-000106 Bean: org.jboss.weld.bean-jboss-cdi-alternative.war/content/jboss-cdi-alternative.war/WEB-INF/classes-Built-in-org.jboss.weld.context.SingletonContext
                15:52:08,308 DEBUG [org.jboss.weld.Bootstrap] (MSC service thread 1-2) WELD-000106 Bean: org.jboss.weld.bean-jboss-cdi-alternative.war/content/jboss-cdi-alternative.war/WEB-INF/classes-Built-in-org.jboss.weld.context.ApplicationContext
                15:52:08,308 DEBUG [org.jboss.weld.Bootstrap] (MSC service thread 1-2) WELD-000106 Bean: Managed Bean [class org.jboss.as.quickstarts.cdi.alternative.DefaultLogger] with qualifiers [@Any @Default]
                15:52:08,310 DEBUG [org.jboss.weld.Bootstrap] (MSC service thread 1-2) WELD-000106 Bean: Managed Bean [class org.jboss.as.quickstarts.cdi.alternative.TaxImpl_1] with qualifiers [@Any @Default]
                15:52:08,314 DEBUG [org.jboss.weld.Bootstrap] (MSC service thread 1-2) WELD-000106 Bean: Managed Bean [class org.jboss.as.quickstarts.cdi.alternative.TaxImpl_2] with qualifiers [@Any @Default]
                15:52:08,317 DEBUG [org.jboss.weld.Bootstrap] (MSC service thread 1-2) WELD-000106 Bean: Implicit Bean [javax.enterprise.inject.Instance] with qualifiers [@Default]
                15:52:08,318 DEBUG [org.jboss.weld.Bootstrap] (MSC service thread 1-2) WELD-000106 Bean: org.jboss.weld.bean-org.jboss.as.weld.deployment.WeldDeployment.additionalClasses-Built-in-org.jboss.weld.context.http.HttpRequestContext
                15:52:08,319 DEBUG [org.jboss.weld.Bootstrap] (MSC service thread 1-2) WELD-000106 Bean: Implicit Bean [javax.enterprise.inject.spi.InjectionPoint] with qualifiers [@Default]
                15:52:08,319 DEBUG [org.jboss.weld.Bootstrap] (MSC service thread 1-2) WELD-000106 Bean: org.jboss.weld.bean-org.jboss.as.weld.deployment.WeldDeployment.additionalClasses-Built-in-org.jboss.weld.context.bound.BoundConversationContext
                15:52:08,320 DEBUG [org.jboss.weld.Bootstrap] (MSC service thread 1-2) WELD-000106 Bean: org.jboss.weld.bean-org.jboss.as.weld.deployment.WeldDeployment.additionalClasses-Built-in-org.jboss.weld.context.DependentContext
                15:52:08,321 DEBUG [org.jboss.weld.Bootstrap] (MSC service thread 1-2) WELD-000106 Bean: Implicit Bean [javax.enterprise.inject.Instance] with qualifiers [@Default]
                15:52:08,322 DEBUG [org.jboss.weld.Bootstrap] (MSC service thread 1-2) WELD-000106 Bean: org.jboss.weld.bean-org.jboss.as.weld.deployment.WeldDeployment.additionalClasses-Built-in-org.jboss.weld.context.ejb.EjbRequestContext
                15:52:08,323 DEBUG [org.jboss.weld.Bootstrap] (MSC service thread 1-2) WELD-000106 Bean: org.jboss.weld.bean-org.jboss.as.weld.deployment.WeldDeployment.additionalClasses-Built-in-org.jboss.weld.context.http.HttpConversationContext
                15:52:08,324 DEBUG [org.jboss.weld.Bootstrap] (MSC service thread 1-2) WELD-000106 Bean: Built-in Bean [java.security.Principal] with qualifiers [@Default]
                15:52:08,325 DEBUG [org.jboss.weld.Bootstrap] (MSC service thread 1-2) WELD-000106 Bean: org.jboss.weld.bean-org.jboss.as.weld.deployment.WeldDeployment.additionalClasses-Built-in-org.jboss.weld.context.ApplicationContext
                15:52:08,325 DEBUG [org.jboss.weld.Bootstrap] (MSC service thread 1-2) WELD-000106 Bean: Implicit Bean [javax.enterprise.event.Event] with qualifiers [@Default]
                15:52:08,326 DEBUG [org.jboss.weld.Bootstrap] (MSC service thread 1-2) WELD-000106 Bean: org.jboss.weld.bean-org.jboss.as.weld.deployment.WeldDeployment.additionalClasses-Built-in-javax.enterprise.context.Conversation
                15:52:08,326 DEBUG [org.jboss.weld.Bootstrap] (MSC service thread 1-2) WELD-000106 Bean: org.jboss.weld.bean-org.jboss.as.weld.deployment.WeldDeployment.additionalClasses-Built-in-org.jboss.weld.context.bound.BoundRequestContext
                15:52:08,327 DEBUG [org.jboss.weld.Bootstrap] (MSC service thread 1-2) WELD-000106 Bean: org.jboss.weld.bean-org.jboss.as.weld.deployment.WeldDeployment.additionalClasses-Built-in-org.jboss.weld.context.RequestContext
                15:52:08,327 DEBUG [org.jboss.weld.Bootstrap] (MSC service thread 1-2) WELD-000106 Bean: Built-in Bean [javax.validator.ValidatorFactory] with qualifiers [@Default]
                15:52:08,328 DEBUG [org.jboss.weld.Bootstrap] (MSC service thread 1-2) WELD-000106 Bean: org.jboss.weld.bean-org.jboss.as.weld.deployment.WeldDeployment.additionalClasses-Built-in-org.jboss.weld.context.http.HttpSessionContext
                15:52:08,328 DEBUG [org.jboss.weld.Bootstrap] (MSC service thread 1-2) WELD-000106 Bean: org.jboss.weld.bean-org.jboss.as.weld.deployment.WeldDeployment.additionalClasses-Built-in-org.jboss.weld.context.SingletonContext
                15:52:08,329 DEBUG [org.jboss.weld.Bootstrap] (MSC service thread 1-2) WELD-000106 Bean: Built-in Bean [javax.validator.Validator] with qualifiers [@Default]
                15:52:08,329 DEBUG [org.jboss.weld.Bootstrap] (MSC service thread 1-2) WELD-000106 Bean: Built-in Bean [javax.transaction.UserTransaction] with qualifiers [@Default]
                15:52:08,329 DEBUG [org.jboss.weld.Bootstrap] (MSC service thread 1-2) WELD-000106 Bean: org.jboss.weld.bean-org.jboss.as.weld.deployment.WeldDeployment.additionalClasses-Built-in-org.jboss.weld.context.bound.BoundSessionContext
                15:52:08,333 DEBUG [org.jboss.weld.Bootstrap] (MSC service thread 1-2) WELD-000100 Weld initialized. Validating beans
                15:52:08,543 INFO  [org.jboss.web] (ServerService Thread Pool -- 60) JBAS018210: Register web context: /jboss-cdi-alternative
                15:52:52,729 ERROR [org.jboss.remoting.remote.connection] (Remoting "testbed:MANAGEMENT" read-1) JBREM000200: Remote connection failed: java.io.IOException: Received an invalid message length of 1195725856
                15:52:53,270 INFO  [org.jboss.as.server] (HttpManagementService-threads - 34) JBAS018559: Deployed "jboss-cdi-alternative.war" (runtime-name : "jboss-cdi-alternative.war")
                15:53:19,478 ERROR [org.jboss.remoting.remote.connection] (Remoting "testbed:MANAGEMENT" read-1) JBREM000200: Remote connection failed: java.io.IOException: Received an invalid message length of 1195725856
                15:54:52,468 ERROR [org.jboss.remoting.remote.connection] (Remoting "testbed:MANAGEMENT" read-1) JBREM000200: Remote connection failed: java.io.IOException: Received an invalid message length of 1195725856
                15:54:59,374 ERROR [org.jboss.remoting.remote.connection] (Remoting "testbed:MANAGEMENT" read-1) JBREM000200: Remote connection failed: java.io.IOException: Received an invalid message length of 1195725856
                15:55:26,716 INFO  [stdout] (http-/0.0.0.0:8084-1) Default: PRINT MESSAGE
                

                 

                Now the problem is that I do not know if it has something to do with visibility (I'll add export to my module) or scope.

                 

                I'll ask in AS forum.

                • 5. Re: Re: Re: Alternative not injected
                  jorgemoralespou_2

                  Hi Again,

                  It seems that it is possible from JBoss AS 7.2 onwards:

                  https://docs.jboss.org/author/display/AS72/CDI+Reference

                   

                  It is just a matter of including meta-inf when importing the dependency. I have updated the reproducer, and now it works fine.

                   

                  Should I close the JIRA, or you close it as "not a bug"?

                   

                  Thanks.

                  • 6. Re: Re: Re: Alternative not injected
                    kcbabo

                    Thanks for tracking that down.  Issue has been closed with a note to check the forum thread for your resolution.