This document will show you how to bootstrap an Event Bus service using Vert.x Service Proxy module. As you will see, this approach is particularly useful when you want to design and consume a service on the event bus using plain Java interfaces.
You will build a Vert.x application that exposes and uses BarmanService, an Event Bus Service that generates beers and manages customer bills.
We are going to deploy two verticles: the BarmanVerticle that exposes the service and the DrunkVerticle that consumes the service
Here is the content of the pom.xml file you should be using:
pom.xmllink:pom.xml[role=include]You must import both vertx-codegen with processor classifier and vertx-service-proxy.
We are going to use vertx-web-client to load the beer names.
First, we must define our service interface. Under the hood, a code generator analyze it and generates the event bus message handler and the proxy class.
This is the BarmanService interface:
link:src/main/java/io/vertx/howtos/ebservice/beers/BarmanService.java[role=include]-
Add
@VertxGenand@ProxyGento trigger code generation -
Define the method that generates a new beer and adds it to the bill of the customer specified. When the beer is ready, the
Futureis completed. -
Define the method that retrieves the bill.
-
Define the method that settles the bill. This is a fire and forget method.
-
Specify the method that creates a new proxy.
BarmanServiceVertxEBProxyis the class name of the proxy that the code generator will create for us.
There are a couple of rules you must follow when defining the service interface.
Look at Restrictions for service interface for more info.
In our interface, we are using a couple of primitives and Beer, a POJO annotated as @DataObject.
If you want to use types annotated with DataObject inside your interface, they must define both a constructor with only a JsonObject parameter and a toJson() method that returns a JsonObject.
To trigger the code generation, you must also add in the same package of the interface a package-info.java with @ModuleGen annotation:
link:src/main/java/io/vertx/howtos/ebservice/beers/package-info.java[role=include]Now you can implement the BarmanService.
For giveMeARandomBeer():
link:src/main/java/io/vertx/howtos/ebservice/beers/impl/BarmanServiceImpl.java[role=include]-
Send a request to the Craft Beer Name Generator Service
-
Fail the async method if the request to external service failed
-
Create a new
Beer -
Add the beer to the customer bill
For getMyBill():
link:src/main/java/io/vertx/howtos/ebservice/beers/impl/BarmanServiceImpl.java[role=include]For settleMyBill():
link:src/main/java/io/vertx/howtos/ebservice/beers/impl/BarmanServiceImpl.java[role=include]Now we can expose the service inside the BarmanVerticle with the ServiceBinder class:
link:src/main/java/io/vertx/howtos/ebservice/BarmanVerticle.java[role=include]-
Instantiate the
BarmanServiceImpl -
Instantiate the
ServiceBinder -
Configure the service address
-
Register the service on the event bus
Now we can use the service with the generated proxy.
For demo purposes, we’re going to use it inside another verticle called DrunkVerticle:
link:src/main/java/io/vertx/howtos/ebservice/DrunkVerticle.java[role=include]-
Instantiate the proxy
-
Let’s try the first beer 🍺
-
Drinking the first one 🍻
-
Give me another one 🍺
-
Drinking the second one 🍻
-
Time to go home. Give me the bill
-
Pay the bill
To run the application deploy the BarmanVerticle and subsequently the DrunkVerticle.
link:src/main/java/io/vertx/howtos/ebservice/BeersApplication.java[role=include]You can run the application from:
-
your IDE, by running the
mainmethod from theBeersApplicationclass, or -
with Maven:
mvn compile exec:java
You should see something like this:
The barman is ready to serve you Generated a new Beer! Manticore's Shorthand Barleywine (Barleywine) My first beer is a Manticore's Shorthand Barleywine and it costs 6€ Generated a new Beer! Fortunate Pretzel (Cream Ale) My second beer is a Fortunate Pretzel and it costs 6€ My bill with the bar is 12€ Removed debt of homer
This how-to explained to you:
-
How to design and implement an Event Bus Service using Vert.x Service Proxy, and
-
How to expose the service on the event bus, and
-
How to use the service