Does anybody have a project with a guava event bus...
# coroutines
g
Does anybody have a project with a guava event bus on it and suspending/async-ing subscribers? I would very much like to have
@guava.Subscribe suspend fun handle(event: MyEvent)
functions, but by default guava has no idea how to handle that. I've tried: 1. switching my tests to invoke handlers directly. This works for tests but has trouble with some of our components 2. adding an obnoxious inheritance system to try and keep a
List<Job>
on the event itself, that gets appended to by subscribers. This is not great. I'd be willing to take a fork of evenBus just for support for concurrent & synchronous event publication, especially if I can get it without retrofitting existing blocking java code.
e
What’s the purpose of making the
handle()
function a suspend function? Is it called by other things or are you trying to get it to be handled in some coroutine context?
i.e. are you trying to make the handling of an event not block the bus?
g
a summary of my problem: we post events when things change. Sometimes that kicks off "autosave", a big suspending IO operation. Right now we dot hat operation asynchronously and with bugs. It would be simpler if I could have the autosave operation be a
suspend
(read: synchronous & concurrent) operation, but to do that, it needs to back-pressure the caller of
post
by
suspend
-ing it.
it strikes me that an extension function on
EventBus
and inheritence on events (sigh) is still the best way to go.
e
Either way you’ll need to end up blocking a thread unless you want to rewrite EventBus to be suspending from the ground up. It’s asynchronous under the hood using java Executor based on what I’ve seen on github
If order doesn’t matter you might be able to spin up your own thread pool and convert it to a coroutine dispatcher, then toss autoSave() calls into the pool via launch()
If order does matter you’ll need to block the subscriber’s thread via a buffered channel with sendBlocking()
Then just toss some coroutine workers into the thread pool