How to allow a berth to block a request when reloading modified classes?

I am using dcevm + run-jetty-run + livereload , try creating a web application without restarting the berth when changing java sources.

Everything works perfectly. When I changed the java class, the current function tracked the change and activated the browser, updating the open pages to see the changed result.

But I found that it was still not so convenient: when the browser rebooted, dcevm and jetty might not have reloaded the modified classes yet. I need to manually refresh the page again, but I'm not sure if it shows the changed result this time without checking the content.

So I'm wondering if there is a way to let jetty block the request when I modified some classes and dcevm reloads. He will ensure that the pages displayed always change.

+6
source share
1 answer

It may be too hacked for your taste, but you can insert a static initialization fragment in your Java sources to update a known separate file after a reboot. Instead of showing the current download, this separate file instead of letting it work directly with .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 */ } 
+1
source

Source: https://habr.com/ru/post/914003/


All Articles