9 Replies Latest reply on Jun 8, 2012 2:53 AM by dzcs

    Doubt with CDI Interceptors in AS 7

    wilsonguimaraes

      Hi,

       

      We've created two projects: cdijar and webapp (https://github.com/zyc/jboss-as7-lab/tree/master/interceptors/scenario1). On cdijar we've implemented an interceptor bindig (MyInterceptorBinding) and its respective interceptor (MyInterceptor.java) that simply prints a message on console. The beans.xml was defined inside META-INF in the same project. webapp depends on cdijar and defines a bean (MyBean.java) whose method (public void method()) is annotated with @MyInterceptorBinding and prints another message on console. In this case, beans.xml is empty. To put all this together, we've created an index.html with a button that executes method().

       

      What we expect to be shown on console is the following:

      This is an interception.

      At this point, an interception should have occurred.

       

      That's exactly what happens when running the example on Jboss AS 6. However, the same doesn't occur on AS 7. Instead, when pushing the button on index.xhtml the only message printed is the one defined on MyBean.java ("At this point, an interception should have occurred.").

       

      We've faced a similar problem with CDI Portable Extensions that was solved when beans.xml was defined inside WEB-INF in the webapp. With that in mind we did the same procedure, but didn't remove it from beans.xml inside cdijar. This way, everything works as expected on AS 7, but AS 6 raises an exception saying that the interceptor MyInterceptor was specified twice.

       

      Seems that, on AS 7, the beans.xml inside cdijar is being ignored. Is that the correct behaviour?

       

      Kind regards,

      Wilson Guimarães

      Cleverson Sacramento

        • 1. Re: Doubt with CDI Interceptors in AS 7
          alesj

          Seems that, on AS 7, the beans.xml inside cdijar is being ignored. Is that the correct behaviour?

          It's not ignored.

           

          AS7 Weld integration is different from AS6, wrt CDI archives.

          AS6 is more flat ~ CDI / (sub)deployment, whereas AS7 is CDI / bean archive.

           

          In order for the interceptor to kick in, you would need to define it in .war' beans.xml.

          • 2. Re: Doubt with CDI Interceptors in AS 7
            wilsonguimaraes

            Ales,

             

            We didn't get the point of that. For example, if my app depends on a .jar that defines an interceptor in its beans.xml I need to redefine it in the war's beans.xml in order to use it?

             

            As an additional test we've deployed the same app (https://github.com/zyc/jboss-as7-lab/tree/master/interceptors/scenario1) on tomcat7. To achieve that we've created a Maven profile, added the Weld listener (org.jboss.weld.environment.servlet.Listener) in web.xml and added a dependency to the newest Weld lib (1.1.2.AS7). That works just fine.

             

            Thanks in advance,

            Wilson Guimarães

            Cleverson Sacramento

            • 3. Re: Doubt with CDI Interceptors in AS 7
              cleverson.sacramento

              Ales,

               

               

              According to section 12.1 (Bean archives) of CDI specification, "When searching for beans, the container considers: (...) any interceptor or decorator class declared in the beans.xml file of any bean archive".

               

              Additionally at top of section 12: "Bean discovery is the process of determining: The bean archives that exist in the application, and the beans they contain; Which alternatives, interceptors and decorators are enabled for each bean archive; (...)"

               

              Would the correct behavior considers beans.xml into the library jar (cdijar) in the way that Wilson referred?

              • 4. Re: Doubt with CDI Interceptors in AS 7
                swd847

                The two relevant sections are:

                 

                9.4 - "By default, a bean archive has no enabled interceptors bound via interceptor bindings. An interceptor must be explicitly

                enabled by listing its class under the <interceptors> element of the beans.xml file of the bean archive."

                 

                 

                9.5 - "An interceptor is bound to the method if: .....

                The interceptor is enabled in the bean archive containing the bean."

                 

                 

                I agree that this behaviour is not ideal, but we have to follow the spec.

                • 5. Re: Doubt with CDI Interceptors in AS 7
                  cleverson.sacramento

                  Thanks for complementation, Stuart. This is exactily what we mean on the first post: "Seems that, on AS 7, the beans.xml inside cdijar is being ignored. Is that the correct behaviour?".

                   

                  According to specification, the beans.xml inside "cdijar" which defines the interceptor should be considered... or not? If you run the example app you will notice that AS7 apparently does not follow the spec at this point.

                  • 6. Re: Doubt with CDI Interceptors in AS 7
                    swd847

                    The beans.xml if cdijar is only considered for beans deployed inside cdijar. If the beans.xml in cdijar defines enables an interceptor, it is only applied to beans that are packaged inside cdijar.

                     

                    Beans in WEB-INF/classes will not be intercepted unless the interceptors are enabled again in META-INF/beans.xml of the war archive.

                    • 7. Re: Doubt with CDI Interceptors in AS 7
                      cleverson.sacramento

                      In META-INF/beans.xml or WEB-INF/beans.xml ????

                      • 8. Re: Doubt with CDI Interceptors in AS 7
                        alesj

                        WEB-INF

                        • 9. Re: Doubt with CDI Interceptors in AS 7
                          dzcs

                          Hi Guys, I am pretty late with my note, but....

                          Say I have tree jars in a war archive. One of jars provides interceptor bindings and interceptors themselfs. Two other jars uses binding annotations and want to use interceptor. If I follow you, I would have to define interceptors in each of two jar. It would work probably in AS7 (haven't try it). But it does not work in other containers, when weld is deployed together with web application. The exception would be double interceptor registration.