Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to let jetty block the request when reloading modified classes?

I'm using dcevm + run-jetty-run + livereload , try to develop a web app without restarting jetty when modifing java sources.

Everything works fine. When I modified a java class, livereload monitored the change, and triggered the browser refreshing opened pages to see the modified result.

But I found it still not that convenient: When browser reloads, dcevm and jetty may have not reloaded that modified classes yet. I have to manually refresh the page again, but I'm not sure if it shows the modified result this time, without checking the content carefully.

So I wonder is there any way to let jetty blocks the request when I modified some classes and dcevm is reloading. It will make sure the pages displayed are always modified.

like image 764
Freewind Avatar asked Apr 24 '12 05:04

Freewind


1 Answers

It's maybe too hacky for your palate, but you could insert a static initialization snippet in your Java sources to update a known, separate file after reloading. Than livereload can watch that separate file instead of letting it work directly on .java sources.

Something along the lines of:

public class ReloadUtils {
  public static void notifyUpdate(String className) {
    String baseDir = System.getProperty("DEV_MODE_BASEDIR") + "/";
    File file = new File(baseDir + className + ".updated");
    FileWriter fw = new FileWriter(file.getAbsoluteFile(), false); // overwrite instead of append
    BufferedWriter bw = new BufferedWriter(fw);
    bw.write(Long.toString(System.currentTimeMillis()));
    bw.close();
  }
}

public class Reloadable {

  private final static boolean DEV_MODE = System.getProperty("DEV_MODE").equals("true");

  static {
    // static finals trigger most compilers to remove the statements in this case
    if (DEV_MODE) { 
      ReloadUtils.notifyUpdate(Reloadable.class.getName());
    }
  }

  /* lots of useful stuff */
}
like image 129
skuro Avatar answered Oct 16 '22 02:10

skuro