Reload class file in tomcat

I am creating a class file at run time.

I want to replace the existing class file with the updated one in the class loader.

It is similar to hot swapping (e.g. JRebel) which avoids server restart and redeployment.

I found context.xml approach for tomcat for context reloading. But it is not very useful in case of production environment.

Can we register class with ClassLoader at runtime? Please suggest if any alternative is there to reload class at runtime.


I am using following code to retrieve current classLoader.

ClassLoader classLoader = LoggingAspect.class.getClassLoader();

Below is the implementation of load class method.

public class AspectClassLoader extends ClassLoader{

@Override
public synchronized Class<?> loadClass(String name) throws ClassNotFoundException
{
    String customLoadClass = "com.log.LoggingAspect";
    try
    {
        if(!customLoadClass.equals(name))
        {
            return super.loadClass(name);
        }
        else
        {
            URL classLoadUrl = new URL(this.reloadClassUrl);
            URLConnection connection = classLoadUrl.openConnection();
            InputStream input = connection.getInputStream();
            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
            int data = input.read();

            while(data != -1){
                buffer.write(data);
                data = input.read();
            }

            input.close();

            byte[] classData = buffer.toByteArray();
            return defineClass(name, classData, 0, classData.length);
        }
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    return null; 
}

To reload class i am using following code.

AspectClassLoader urlClsLoader = new AspectClassLoader(classLoader);
Class aspect = urlClsLoader.reloadClass("com.log.LoggingAspect", true, new String("file:"+className));

My reload method of the classloader is

public synchronized Class<?> reloadClass(String name, boolean resolve, String reloadClassUrl) throws ClassNotFoundException
{
    this.reloadClassUrl = reloadClassUrl;
    return this.loadClass(name, resolve);
}

After reloading if i create the new instance of LoggingAspect it still gives me the old instance. Please suggest.

Class aspect = urlClsLoader.reloadClass("com.log.LoggingAspect", true, new String("file:"+className));
        com.log.aspect.Aspect aspectObj = (com.log.aspect.Aspect)aspect.newInstance();

Still I am getting the old instance.

Please suggest why classloader does not load the modified class?

Answers


This is possible but complex. What you need to do is create a new URLClassLoader with the current ClassLoader as parent. Add the URL to the folder with your class (without package folders) to the new URLClassLoader

Next, you will have to patch loadClass() to return your new class before calling parent.loadClass() (otherwise, the existing class will be used and the updated one will be ignored).

It gets complex when you want to use the new class. For this, you will have to create it using the new classloader and make sure that everyone uses this instance of the type.

It's probably more safe (and easier to debug) if the class doesn't exist in your WAR. Instead of using the type, load the class with the URLClassLoader and create an instance of it. So the code using this class works like this:

IService service = createService();

where IService is an interface which defines the methods that the generated class supports. That way, you can write code which uses the methods without actually needing an implementation.


Need Your Help

strange image resize issue

html css image resize

I can't get the top image to fit into the div. I've tried looking at other questions on this site but none really answer this question. I have set it to 100% for each of the image sizes but they st...

Django user register url redirect broken

django authentication

My user registration flow has broken. I have the below code in views.py, but receive a no user.url attribute error (as included beneath the views code). The user's being created properly and I can