I have a ktor application with routes/controllers and stuff. Now I also need a separate component which should, on startup, subscribe to a Google PubSub topic and at shutdown, unsubscribe. It also has dependencies and so it needs to have `KoinComponent`s injected. Since nobody is depending directly on this component, it's not getting created by Koin at all. What would be the idiomatic way of solving this use case in Ktor?
Patrik Åkerfeldt
09/30/2020, 10:57 AM
Have a solution now. The subscriber class is a
KoinComponent
with its dependencies injected. Inside our application set up
fun Application.module(...)
I initiate the component like so:
Copy code
val subscriber: MySubscriber by inject()
subscriber.initiate(environment)
This in turn uses the
environment
to add hooks for
ApplicationStarted
and
ApplicationStopped
. Seems to work as expected. Is this a decent solution?