5 Replies Latest reply on Jun 3, 2008 1:01 PM by peterj

    Sharing a class between 2 ears (classloader)

      Hi, I'm trying to configure JBoss to allow one Ear to use a class that is included in another ear.

      I've read about the classloader, and I'm not really sure how to proceed.

      Do I need to configure the ear that contains the class? If so, what do I need to put into which configuration file to make this class load differently than the other classes?

      This class is an ejb 3.0 entity bean. Can I expect to be able to use it in another ear exactly as though it were an entity bean deployed within the other ear?

      Any advice is appreciated.

        • 1. Re: Sharing a class between 2 ears (classloader)
          peterj

          You should not have to do anything. The classes in both ears are loaded using the same class loader repository, so each ear should have access to the classes in the other ear. Only if you don't want this to happen do you have to do extra work.

          Note that for classes in a war it is exactly the opposite - you cannot use classes in a war file from external to that war.

          • 2. Re: Sharing a class between 2 ears (classloader)

            Well, I was getting noclassdeffound errors. The problem was that the the jboss-app for the 2 ears had a different entry in the <jboss-app><loader-repository></loader-repository></jboss-app> tag.

            By making those 2 entries identical, the classes loaded from the original ear were available to the other ear.

            Now, the problem I'm facing is that the Entity bean I wanted to share is persisting to Ear1's defined datasource instead of Ear2's datasource. I imagine this is because when jboss starts up, it does some maping for entity beans so it can quickly write to the DB.

            I'm not sure if I am going to be able to find a way to make it treat the Entity bean differently depending on which application is calling it. I may have to write some persistence code in Ear2 to make sure that it is writing the way I want it to, instead of just using EntityManager's persist method.

            • 3. Re: Sharing a class between 2 ears (classloader)
              peterj

              Ah, you defined a loader repository. That is what I meant by "Only if you don't want this to happen do you have to do extra work."

              • 4. Re: Sharing a class between 2 ears (classloader)

                Ya, I guess that the wizard that generated my project did some extra work for me without telling me :)

                Because the 2 projects had their own loader repository, it wasn't really automatic.

                That said, this solution isn't really 100% what I was hoping for. Now I have to worry about these 2 projects defining objects with the same name... since they have the same class loader and name, they will conflict, yes?

                I am really only interested in a specific package of the other ear. Any guesses on how to only share some parts of an ear and retain independent namespaces for the rest?

                • 5. Re: Sharing a class between 2 ears (classloader)
                  peterj

                  On sharing a jar, a couple of possibilities:

                  1) Put that jar in server/xxx/lib (not ideal, but doable)
                  2) Create a third ear without a classloader repository, and put the jar into that ear. (This is the solution I have used before.)