Reload class file in tomcat

好久不见. 提交于 2019-12-08 06:11:11

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/12317331/reload-class-file-in-tomcat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!