Refactoring to Vert.x services

Tip
The corresponding source code is in the step-3 folder of the guide repository.

The previous refactoring was a big step forward compared to our initial implementation, as we extracted independent and configurable verticles connected using asynchronous messages on the event bus. We also saw that we could deploy several instances of a given verticle to better cope with load and to better leverage CPU cores.

In this section we see how to design and use Vert.x services. The main advantage of a service is that it defines an interface for doing certain operations that a verticle exposes. We also leverage code generation for all the event bus messaging plumbing, instead of crafting it ourselves like we did in the previous section.

We are also going to refactor the code into different Java packages:

  1. step-3/src/main/java/
  2. └── io
  3. └── vertx
  4. └── guides
  5. └── wiki
  6. ├── MainVerticle.java
  7. ├── database
  8.    ├── ErrorCodes.java
  9.    ├── SqlQuery.java
  10.    ├── WikiDatabaseService.java
  11.    ├── WikiDatabaseServiceImpl.java
  12.    ├── WikiDatabaseVerticle.java
  13.    └── package-info.java
  14. └── http
  15. └── HttpServerVerticle.java

io.vertx.guides.wiki will now contain the main verticle, io.vertx.guides.wiki.database the database verticle and service, and io.vertx.guides.wiki.http the HTTP server verticle.