How to reload properties file without rebooting Tomcat

时间秒杀一切 提交于 2021-02-08 03:29:05

问题


I load a property file from classpath with the method :

    String cheminFichier = new StringBuilder(100).append(classeBP.getPackage().getName().replace(".", "/")).append(File.separator).append(
        REPERTOIRE_MAPPING).append(nomFichier).append(".properties").toString();
    InputStream isMapping = Thread.currentThread().getContextClassLoader().getResourceAsStream(cheminFichier.toString());
    if (isMapping == null)
    {
        throw new ServiceMappingException("Erreur lors du chargement du mapping du service. Le fichier "
            + cheminFichier + " n'existe pas.");
    }
    else
    {
        Properties mapping = new Properties();
        try
        {
            mapping.load(isMapping);
        }
        catch (IOException e)
        ...
    }

Ok, it's work. But if I modify the content of the property file when Tomcat is running, changes are ignored. It's not hot-reloaded as with classes.

My context is configured with reloadable="true" option and the classloader returned by Thread.currentThread().getContextClassLoader() is the WEBAPP classloader (not the system classloader or other).

I read it's possible to use ServletContext.getResourceAsStream, but I haven't access to the servlet context.

It's Tomcat 5.5.

Any idea ? If not, do you have a solution for forcing to reload a specific resource (my property file) ?

Thanks !


回答1:


Actually, your webapp does have access to the ServletContext at various points. For example you can get it using ServletRequest.getServletContext(), or from a ServletContextEvent obtained via a ServletContextListener callback.


This is a bit off topic, but you should realise that this code:

String cheminFichier = new StringBuilder(100).append(classeBP.getPackage().getName().replace(".", "/")).append(File.separator).append(
    REPERTOIRE_MAPPING).append(nomFichier).append(".properties").toString();

could / should be written as:

String cheminFichier = classeBP.getPackage().getName().replace(".", "/")) +
    File.separator + REPERTOIRE_MAPPING + nomFichier + ".properties";

There is no performance advantage here in explicitly using a StringBuilder here, because the Java compiler will compile the concatenation to the same code.

You only need to use a StringBuilder if the concatenation involves a loop.




回答2:


You could use something like Commons Configuration that supports automatic reloading.




回答3:


You can still use the Tomcat Manager to reload your application.



来源:https://stackoverflow.com/questions/3573575/how-to-reload-properties-file-without-rebooting-tomcat

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