New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Automatic reload for dev-server #1109
Comments
I've been looking at bit at this, and it seems most projects provide a Maven or Gradle plugin to handle this. Some users of Javalin (ok, it's probably just me) use Bazel, so ideally any implementation of this should work across build tools. That might be unrealistic though... |
Hi @tipsy I spent some time this morning trying to work out how DCEVM Plugins worked. I've got to the point where I can call arbitrary user code whenever any class in the project changes. The problem I have is that, in order to pick up new routes or new configuration, I would need to restart the Javalin server and, as soon as I stop the Javalin server, the entire application exits! I don't have enough knowledge of Javalin internals to know how to get round this. I've therefore put together a prototype in the hope it might give you an idea of how to integrate into Javalin. Right now it is VERY rough and ready |
Really nice writeup! I'm on vacation currently, so I don't too much time to look into it, but I can try to assist you here.
Javalin just does |
That was my guess as to what was going wrong. I'm not massively familiar with threads so, rather than trying to work out how to adjust the thread join, I have put together what has to count as the hackiest prototype I've done in quite some time! Basically, I have added a class called AppRunner which starts the App and, if the app exits, goes into a loop with a thread sleep. That means my main thread never exits and so the reload method does indeed get executed. With that change (and I am in no way suggesting that's an actual solution) it shows that hot reload does indeed work and boy is it fast! I get a 20-50ms restart time for Javalin....
So I guess we can say that using a DCEVM plugin for hot reload looks both possible and performant and it just needs a design as to how to make it work more "generically" (i.e. without hard coded classes) and in a way which doesn't involve a hacky infinite loop. Enjoy your holiday. Happy to continue discussion when you're back |
Hi @tipsy I've not had too much time to progress with this, but I did manage to work out how to make the reload plugin "generic". Basically, HotswapAgent comes with a properties file (hotswap-agent.properties) and a PluginConfiguration class which gets injected into your own plugin and can then access properties from the properties file. As such, I was able to add javalin.main_class=com.blah.MyClass to the properties file and then inject into my plugin like this @Init
lateinit var pluginConfiguration: PluginConfiguration
@Init
lateinit var appClassLoader: ClassLoader
private val mainClassName by lazy { pluginConfiguration.getProperty("javalin.main_class") } and finally, having both the class name and the application classloader, we can get an instance of the class to call @OnClassLoadEvent(classNameRegexp = ".*", events = [LoadEvent.REDEFINE])
fun onClassReloaded() {
val cls = Class.forName(mainClassName,true,appClassLoader)
val instance = cls.getField("INSTANCE").get(cls)
scheduler.scheduleCommand(ReflectionCommand(instance,"restart"))
} I still haven't got rid of the hacky infinite loop and my implementation requires your main class to be a kotlin object at the moment (hence the cls.getField("INSTANCE") stuff, but it does at least work if you fancy discussing online at some point |
Oh, and I have updated the repo below with a tidier version which
https://github.com/tomhillgreyridge/hotswap My gut feeling is that you might not want this to be a core Javalin feature as I don't think it will work 100% of the time and you'll just get people saying "why doesn't this work with my random self-built dependency injection framework". If we could work out how to get rid of the hacky infinite loop part, I'd be happy to write a tutorial about how people can add it to their own Javalin projects though. I've now got it working with a reasonable sized Javalin project, complete with HikariCP, Guice, javamail etc so I should be able to get a feel for how well it works by using it "for real" |
I honestly don't see that as an issue at all, it's a dev server thing after all.
It could be nice to have it as a module? As things are now I don't provide much support for the modules, they are mainly managed by the community. |
Hi all, i am struggling to find an easy solution to automatically reaload my application when i change my code. So, i have write a python script that do that. |
FYI https://github.com/yusukezzz/javalin-restart-demo I wrote sample code to apply the method used in micronaut to javalin. Hope it helps for javalin users. |
|
@asad-awadia are you happy with that solution? Can you set what it should trigger on? Would you be interested in setting up an example? |
Yes - was the easiest way to get auto reload on edit for any maven project - works with anything that can be rerun via maven [not javalin specific]
Not without editing the code of the repo itself - eg i changed the watched directory from
What would that look like? Currently I call |
I assumed there would be some config, but If this is all there is I don't think a tutorial makes sense 😄 |
Maybe add an info tag here to include this information into your "Interesting issues" collection. Helpful would also be to highlight @asad-awadia answer somehow.. |
Thanks, added :) |
Anyone knows a easy way to have hot releoad in javalin using java and gradle (groovy)? |
Just to add another option using entr (https://github.com/eradman/entr) to do something like this:
where fd automatically ignore gitignored files |
what is fd here? Is it from fdclone package?, I am using ubuntu |
sorry! it is a modern |
|
Describe the feature
Quarkus has a nifty dev-server feature, which enables hot deployment with background compilation. This could make it easier to develop, as it'll refresh all endpoints and resources upon browser request if either kotlin/java/resources has any changes without the need to restart the process.
Additional context
Quarkus development mode
Quarkus github dev-mode code
The text was updated successfully, but these errors were encountered: