Communicating with Event Bus
Event Bus is an implementation of the Observable design pattern, which we discussed in Chapter 4, Getting Familiar with Behavioral Patterns.
We've already mentioned that Vert.x is based on the concept of verticles, which are isolated actors. We've already seen the other types of actors in Chapter 6, Threads and Coroutines. Kotlin's coroutines
library provides the actor()
and producer()
coroutine generators, which create a coroutine bound to a channel.
Similarly, all the verticles in the Vert.x framework are bound by Event Bus and can pass messages to one another using it. Now, let's extract the code from our ServerVerticle
class into a new class, which we'll call CatVerticle
.
Any verticle can send a message over Event Bus by choosing between the following methods:
request()
will send a message to only one subscriber and wait for a response.send()
will send a message to only one subscriber, without waiting...