5 Replies Latest reply on Nov 27, 2010 3:36 PM by lodrantj

    classloading and remote access in ejb3

    lodrantj

      Hi guys

       

      I love the dynamic classloading features of Java but it seems sometimes love is not enough, I'm lacking knowledge about it. Here it goes:

       

      I'm using JBPM 3.2 in my EJB3 / web based app. JBPM is hidden in EJB3 layer. Now, I'd like to make a clear separation between everything process related on one side and the web part on the other. So I figured I'd just add some GUI content generating classes into the jbpm par archive, dynamically load them through a par classloader and simply send them as Class objects to the client. That was the theory.

       

      So far, of course, it doesn't work. I simple get an exception on Class.newInstance call on the client.

       

      On the second ran, I tried to call Class.newInstance inside the EJB and return the Object to the client as an implementation of a known interface. All I got was ClassNotFoundExceptions.

       

      Now, before I dig any further, the question: Do I have I shot here, or am I just plain lurking in the impossible land?

       

      It's basically down to this: Can a remote EJB expose a method returning an interface, of which the implementing class is dynamically evaluated and - this is vital - is not present on the client?

       

      Please, advise!

       

      Thank you..

       

      Jure

        • 2. Re: classloading and remote access in ejb3
          lodrantj

          Hi,

           

          thanks for the pointers.I checked them but,

           

          - do I need to tackle remoting, even if my primary objective is getting it to work inside the embedded web container in the same jboss instance as the EJB facade. I thought jboss optimizes calls within the same VM to avoid unnecessary serialisation. But I get the same error on my web client. Would strictly using @Local annotation on the facade bean help?

          - My use case is even more dynamic than the examples provided: in theory, a class implementation is loaded from a database record on each call using a custom PAR file classloader provided by JBPM. I can't imagine being able to hook that classloader to some jboss exported classloading structure...

           

          Maybe I'll just have to retrieve the class bytecode as a byte[] and do the classloading on the client...

           

          Thoughts anyone?

           

          Jure

          • 3. Re: classloading and remote access in ejb3
            wolfc

            How about creating a ClassLoader that delegates to an EJB (or Servlet) which loads up the correct byte code?

             

            Then on the client it's just a matter of using that ClassLoader as your TCCL.

             

            In fact, if you use a Servlet, you can probably just use URLClassLoader.

            • 4. Re: classloading and remote access in ejb3
              lodrantj

              Thanks for this one!

               

              I'm starting to think that this would actually be the most correct method to implement! Use the EJB only as the raw bytecode supplier, defining classes on the client (as they are not relevant inside the EJB layer) , possibly caching them locally to enable reuse...

               

              I'll try to put together a prototype over the weekend!

               

              Thanks for the pointers!

               

              Jure

              • 5. Re: classloading and remote access in ejb3
                lodrantj

                It took me a while longer than expected, but I'm glad to report that the suggested solution is implemented and looks OK. I now use the EJB layer as a bytecode supplier and then creating classes using a custom classloader on the web tier. Since I only need to create implementations of a single interface this way, it's a very narrow use case and so far it seems to work just fine.

                 

                Thanks again for the hint!