The Ktor examples I see are all statically defined in the Kotlin code. I’d like to be able to load and unload web apps, sort of like you do in servlet containers like Tomcat, Wildfly. Is that possible?
In other words, how do you define something like this so that it can be uninstalled?
I guess if you are referring to similar modules system loaded with NestJS, then the answer is yes. It can be dynamically loaded.
Use Ktor’s Modules feature:
Declare module classes and include a module() function
Load modules at runtime via Application.modules.loadModules()
Ktor will initialize any module registered this way
Manually load code using KClassLoader:
Get an instance of Ktor’s KClassLoader
Call loadClass() to dynamically load a Kotlin class
Create instances and access the module programmatically
Use a library like KDi for dependency injection
Annotate classes with @Module and @Provides
Load modules and call configure() to initialize bindings
Retrieve instances from the injector as needed
In summary, Ktor supports dynamic module loading in multiple ways - built-in modules, manual class loading, and dependency injection libraries.
This allows initializing code and dependencies on the fly similar to NestJS. The main difference is Ktor is not opinionated so you have to wire up the module system yourself.
Hi,
I found your post & it looked really useful but I cannot find any mention of Application.modules.loadModules() in the Ktor documentation or elsewhere. Could you provide an example showing how that would support dynamic loading of Ktor modules?
Thanks
The post you are replying to was most likely generated with ChatGPT, judging by the very characteristic structure of the post. I wouldn’t hold my breath for a response
@Deprecated(
"This method is misleading and will be removed. " +
"If you have use case that requires this functionaity, please add it in KTOR-2696",
level = DeprecationLevel.ERROR
)
public fun <A : Pipeline<*, PipelineCall>, B : Any, F : Any> A.uninstall(
plugin: Plugin<A, B, F>
)
So I guess you need to go explain your use-case on KTOR-2696.