vendredi 25 septembre 2020

Dynamically reload JAR in tomcat

I have Jersey service runs in tomcat. I try to reload my JAR content when it has a new version. My service has instance manager type of IManager, its implementation is loaded through reflection.

private IManager manager; 

From different forums and questions here in stackoverflow, I understand that for reloading classes in JVM I have to implement my Classloader, due to default ClassLoaders assume that the class definition will not change through the life of the JVM and don't read its new version from disk.

So, I implemented my ClassLoader, but the problem in the next code:

private IManager manager;

public void loadManager() {
    MyWebAppLoader loader = new MyWebAppLoader(classesPathNorm, libPathNorm);
            
    Class<?> managerClass = loader.loadClass("mycompany.Manager");
    
    //EXCEPTION HERE: mycompany.Manager cannot be cast to mycompany.IManager
    manager = (IManager) managerClass.newInstance();

}
    

I know that the problem is IManager and Manager have different ClassLoaders (CatalinaClassLoader VS MyWebAppLoader). If I understand, I have 2 options:

  1. Use <Context reloadable="true">, BUT it isn't not recommended for use on deployed production applications + in this case I haven't a control on loaded JAR if it's corrupted, for example.

  2. Override classloader of Tomcat. I'm not sure that it's the safest way to solve my problem

So, my question, what is the best and the safest way to solve my problem? Am I right about two options that I have?





Aucun commentaire:

Enregistrer un commentaire