5 Replies Latest reply on Aug 8, 2008 5:30 PM by Mark LoSacco

    How to share ejb project across multiple applications?

    Mark LoSacco Newbie

      Hi there,


      I have a Seam application which was original created with seamgen and has the following structure when deployed to JBossAS:


      lu-ear.ear
         lu.war
         lu-ejb.jar
         META-INF



      Now I have created a second application which needs to share many of the components (both entity beans and seam components) that are located in lu-ejb.jar.


      My first attempt was to create a similar structure.  When deployed it looks like this:


      mylu-ear.ear
         mylu.war
         lu-ejb.jar
         META-INF



      So both ear's are using a copy of the lu-ejb.jar.


      What happens under this scenario is that whatever application I deploy last is the one that works.  The other application gives ClassCastException errors whenever an entity bean is accessed.


      Clearly I am not approaching this correctly.  What is the right way to set this up?


      -Mark

        • 1. Re: How to share ejb project across multiple applications?
          Ken Clark Newbie

          I think if you wish to deploy this way, all the EJBs have to each have their own unique JNDI.  You are probably using the default JNDIs.  You will have to override this in some way (sorry, I am not familiar with how to do that in the JBoss/Seam realm.)


          Because they all share the same JNDI name, app 1 will look up a bean and try to load it, but because the jndi name is now associated to app 2, yet app 1's classloader cannot access classes in the app 2 deployment.


          You can avoid this by only deploying the ejbs once (in app 1, lets say.)


          In app 2, you would embed the ejb client stubs and access them as clients of app 1.  Then app 2 knows to make use of the external EJBs.  (Note if you want to deploy app 1 and app 2 to different servers at a later point, this will get complicated.)


          ken

          • 2. Re: How to share ejb project across multiple applications?
            Mark LoSacco Newbie

            Thanks Ken - I suspected something like that.


            Yes, part of our desire to split our application into two separate apps is so that in future we have ability to run on different servers.


            One other note is that to date the only EJBs we are using are Entity beans.  All of my Seam components to date are just POJO's, not EJBs.  (Using Hibernate JPA implementation).  This may change in future if need arises (not entirely sure today how using EJB Session beans would benefit me over just Seam POJOs).


            The main thing I would like to share across the applications is the Entity beans as app 2 will need to access app 1's database, in addition to using its own.


            -Mark


            • 3. Re: How to share ejb project across multiple applications?
              Pete Muir Master

              Use classloader isolation and it will just work (look in jboss-app.xml). There is no EJB JNDI name conflict as the JNDI name is namespaced by the ear name.

              • 4. Re: How to share ejb project across multiple applications?
                Francisco Jose Peredo Noguez Master

                Hi!
                Okey, here it goes, posting an answer for the 5th time (is this forum software failing? or there is something bad with my ISP? I am going to save this post in notpad and repost-it until it stays here)



                Mark LoSacco wrote on Aug 07, 2008 20:09:


                Thanks Ken - I suspected something like that.

                Yes, part of our desire to split our application into two separate apps is so that in future we have ability to run on different servers.


                You do not need EJBs or and EAR project for that, you can use a 2 plain WAR projects and 1 plain JAR project.


                One other note is that to date the only EJBs we are using are Entity beans.  All of my Seam components to date are just POJO's, not EJBs.  (Using Hibernate JPA implementation).


                If you guys are only using @Entity clasess, that you do not need an EAR project, you can do this with just 2 WAR (Dynamic Web Project in Eclipse) projects an one JAR (Java Project in Eclipse) with your shared POJOs.



                This may change in future if need arises (not entirely sure today how using EJB Session beans would benefit me over just Seam POJOs).



                I really recommend against this approach, I think you should follow YAGNI here. If you have not current need for EJBs, then, have a WAR project, and, if the need arises, evolve in to an EAR project. (I did it the other way around, started with an EAR project, and, as I realilzed that I didn't need anything POJOs and Seam couldn't offer, I switched to a WAR project in JBoss, and finally ended using a WAR project over Tomcat). In retrospective, I think I wasted the time of my teammates, I should have followed YAGNI and started simple, and then, if needed, used EJBs and JBoss AS, and not the other way around, the develop/deploy/cycle was just too slow at the beginning (cold start in 1 minute) and now, it is much more comfortable and way faster (8 seconds cold start when running over Tomcat). Same thing happened to my integration tests (24 seconds with embedded jboss, 14 seconds without it).




                The main thing I would like to share across the applications is the Entity beans as app 2 will need to access app 1's database, in addition to using its own.


                You can link (and share) your @Entity classes without an EAR project, just link your WAR project to your JAR project using Project Properties->Java Build Path->Projects>Add... and then, to have the JAR file automatically created and added to WEB-INF\lib on deployment, just check the linked project in Project Properties->J2EE Module Dependencies



                • 5. Re: How to share ejb project across multiple applications?
                  Mark LoSacco Newbie

                  Francisco - I did get your replies (even though they are disappearing off this forum minutes after you post them).


                  Thanks for your reply - I am playing around with this refactor now.


                  If we decide to stick with EAR then the Pete's classloader isolation tip looks exactly what I need.  Along that line I was directed to the following links:


                  http://wiki.jboss.org/wiki/JBossClassLoadingUseCases


                  http://wiki.jboss.org/wiki/ClassLoadingConfiguration



                  -Mark