EventBus
This is the second time we have stumbled upon the same problem: our classes get bigger and bigger, which we would usually like to avoid as much as possible.
What if we split this creation of cats logic into a separate file yet again? Let's call it CatVerticle.kt
.
But then we need a way for ServerVerticle
to communicate with CatVerticle
. In frameworks such as SpringBoot, you would use dependency injection for that purpose. But what about reactive frameworks?
Consumer
To solve communication problems, Vert.x uses EventBus. It's an implementation of the Observable design pattern we discussed in Chapter 4, Getting Familiar with Behavioral Patterns. Any verticle can send a message over the event bus, choosing between these two modes:
send()
will send a message to only one subscriberpublish()
will send a message to all subscribers
No matter which method is used to send the message, you subscribe to it using the consumer()
method on the EventBus:
const val CATS = "cats:get" class CatVerticle...