https://kotlinlang.org logo
#ktor
Title
# ktor
p

Patrik Åkerfeldt

09/30/2020, 9:46 AM
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?
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?
6 Views