4 Replies Latest reply on Aug 2, 2003 3:10 PM by melaraj

    Having problem reloading class in the classpool

      We are looking to use JavAssist to provide a dynamic deployment Classpool. By looking at the API, it seems that this should be supported. However, when we test this functionality it does not work correctly.

      Here is some psudo code to illustrate the objective:

      ClassPath classpath;

      cp = new ClassPool(null);
      classpath = cp.appendClassPath("C:\\Projects\\javassist\\libs\\jamessage.jar");
      CtClass cc = cp.get("com.proxymed.msg");
      Class clazz;
      clazz = cc.toClass();
      testint m = (testint)clazz.newInstance();
      //Test to verify that we are calling the old loaded class
      m.say();

      //Remove library so that it can be refreshed with new version
      cp.removeClassPath(classpath);
      //Load new updated library.
      classpath = cp.appendClassPath("C:\\Projects\\javassist\\libs\\jamessageupdated.jar");
      cc = cp.get("com.proxymed.msg");
      clazz = cc.toClass();
      testint f = (testint)clazz.newInstance();
      //Method should output consistant with updated class.
      f.say();

      If you run this code. You would get a duplicate definition exception.

      Is there a way that this can done using this JavAssist.

      Thanks,
      Manuel

        • 1. Re: Having problem reloading class in the classpool
          chiba

          First, once get("Point") is called, the ClassPool object
          keeps the CtClass object representing the Point class
          forever. Even if you remove a class path, the CtClass
          object is not removed. So you should create another
          ClassPool object.

          Second, currently all instances of ClassPool share
          a single class loader for executing toClass(). Hence,
          you cannot reload a class. Do you think I should
          make each instance of ClassPool have a distinct
          class loader?

          - Chiba

          • 2. Re: Having problem reloading class in the classpool

            I think you should allow the ability to have multiple loaders. This will expand your library so that it can be used in a number of new types of systems.

            We are looking to allow users to hot deploy and update libraries withouth having to bring down the system.


            Manuel

            • 3. Re: Having problem reloading class in the classpool
              chiba

              Hmm... if you want to implement hot deploy,
              you should not use toClass(). Rather you
              should use javassist.Loader or write your own
              class loader. For more details, please read
              the API document of ClassPool#writeAsClass(Stirng)
              and the section 4 of the tutorial.

              toClass() is a convenience method; not for complex
              work.

              • 4. Re: Having problem reloading class in the classpool

                Got it!
                I tried writing my own loader. However, whenever a jar containing class is updated. The following error is thrown when trying to define the class:

                java.lang.LinkageError: duplicate class definition

                I am not sure if what I am looking for is possible. I will update the thread if I come up with something.

                Thanks for the help!.

                Manuel