1

I have an app running in tomcat6 which reads a resource file (a simple text file, no xml) at startup to configure/instantiate some classes. Now I want to be able to modify that file and the changes to be reflected in the app without having to restart the server. Is there a way to do that?

Basically if I could define and attach a listener to that resource, that's all I need.

jmj
  • 237,923
  • 42
  • 401
  • 438
Yondaime
  • 11
  • 1

4 Answers4

1

Try one of the following:

NOTE

With Java 7, Watching a Directory for Changes becomes part of the standard API.

mre
  • 43,520
  • 33
  • 120
  • 170
0

Programatically you can trgger a service which will refresh those classes with new files

jmj
  • 237,923
  • 42
  • 401
  • 438
0

Not sure if there is a built in resource listener, but it would be fairly simple to start a new thread that monitors the file in the file system ( on date/time last updated for example ) and refreshed the classes if it changed.

DaveH
  • 7,187
  • 5
  • 32
  • 53
0

Start a timer in the web app which gets the resource file as below:

In a ServletContextListener.contextInitialized(),

package com.servlets;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Timer;
import java.util.TimerTask;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class WatcherListener
    implements ServletContextListener
{
public void contextInitialized(ServletContextEvent event)
{
    ServletContext servletContext = event.getServletContext();
    Timer timer = new Timer("ResourceListener");
    timer.schedule(new MyWatcherTask(servletContext), 15);
}

public void contextDestroyed(ServletContextEvent event)
{
}

private class MyWatcherTask extends TimerTask
{
    private final ServletContext servletContext;
    private long lastModifiedTime = -1;

    public MyWatcherTask(ServletContext servletContext)
    {
        this.servletContext = servletContext;
    }

    public void run()
    {
        URL resource = null;
        try {
            resource = servletContext.getResource("resouceFileName");
            File resourceFile = new File(resource.toString());
            long current = resourceFile.lastModified();
            if (current > lastModifiedTime) {
                reloadReources();
            }
            lastModifiedTime = current;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

    }
}
}

The above code is just a template

Ramesh PVK
  • 15,200
  • 2
  • 46
  • 50
  • You should prefer `ScheduledExecutorService` over `TimerTask`. See also http://stackoverflow.com/questions/409932/java-timer-vs-executorservice and http://stackoverflow.com/questions/5798688/reload-servlet-once-a-month. Or when you're on Java EE 6 with EJB capable container, prefer `@Singleton` with `@Schedule`. See also http://stackoverflow.com/questions/5357033/background-timer-task-in-jsp-web-application. – BalusC Jun 02 '11 at 14:33