Skip to content

Commit 0d7bb0e

Browse files
committed
Update to Vert.x 5
1 parent 5b126bd commit 0d7bb0e

File tree

8 files changed

+106
-99
lines changed

8 files changed

+106
-99
lines changed

README.adoc

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
:page-permalink: /
33
:page-github: vertx-howtos/service-proxy-howto
44

5-
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.
5+
This document will show you how to bootstrap an Event Bus service using Vert.x Service Proxy module.
6+
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.
67

78
== What you will build
89

@@ -12,7 +13,7 @@ We are going to deploy two verticles: the `BarmanVerticle` that exposes the serv
1213
== What you need
1314

1415
* A text editor or IDE
15-
* Java 8 higher
16+
* Java 11 higher
1617
* Maven
1718

1819
== Create a project
@@ -25,28 +26,37 @@ Here is the content of the `pom.xml` file you should be using:
2526
include::pom.xml[]
2627
----
2728

28-
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.
29+
You must import both `vertx-codegen` with `processor` classifier and `vertx-service-proxy`.
30+
We are going to use `vertx-web-client` to load the beer names.
2931

3032
== Design the `BarmanService`
3133

32-
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.
34+
First, we must define our service interface.
35+
Under the hood, a code generator analyze it and generates the event bus message handler and the proxy class.
3336

3437
This is the `BarmanService` interface:
38+
3539
[source,java,indent=0]
3640
----
3741
include::src/main/java/io/vertx/howtos/ebservice/beers/BarmanService.java[]
3842
----
43+
3944
<1> Add `@VertxGen` and `@ProxyGen` to trigger code generation
40-
<2> Define the method that generates a new beer and adds it to the bill of the customer specified. When the beer is ready, It calls the `handler` with the new beer
45+
<2> Define the method that generates a new beer and adds it to the bill of the customer specified.
46+
When the beer is ready, the `Future` is completed.
4147
<3> Define the method that retrieves the bill.
42-
<4> Define the method that settles the bill. This is a fire and forget method
43-
<5> Specify the method that creates a new proxy. `BarmanServiceVertxEBProxy` is the class name of the proxy that the code generator will create for us
48+
<4> Define the method that settles the bill.
49+
This is a fire and forget method.
50+
<5> Specify the method that creates a new proxy.
51+
`BarmanServiceVertxEBProxy` is the class name of the proxy that the code generator will create for us.
4452

45-
There are a couple of rules you must follow while defining the service interface. Look at http://vertx.io/docs/vertx-service-proxy/java/#_restrictions_for_service_interface[Restrictions for service interface] for more info.
46-
In our interface we are using a couple of primitives and `Beer`, a POJO annotated as `@DataObject`.
47-
If you want to use `@DataObject`s inside your interface, they must define both a constructor with only a `JsonObject` parameter and a `toJson()` method that returns a `JsonObject`
53+
There are a couple of rules you must follow when defining the service interface.
54+
Look at http://vertx.io/docs/vertx-service-proxy/java/#_restrictions_for_service_interface[Restrictions for service interface] for more info.
55+
In our interface, we are using a couple of primitives and `Beer`, a POJO annotated as `@DataObject`.
56+
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`.
4857

4958
To trigger the code generation, you must also add in the same package of the interface a `package-info.java` with `@ModuleGen` annotation:
59+
5060
[source,java,indent=0]
5161
----
5262
include::src/main/java/io/vertx/howtos/ebservice/beers/package-info.java[]
@@ -57,23 +67,26 @@ include::src/main/java/io/vertx/howtos/ebservice/beers/package-info.java[]
5767
Now you can implement the `BarmanService`.
5868

5969
For `giveMeARandomBeer()`:
70+
6071
[source,java,indent=0]
6172
----
6273
include::src/main/java/io/vertx/howtos/ebservice/beers/impl/BarmanServiceImpl.java[tags=giveMeARandomBeer]
6374
----
75+
6476
<1> Send a request to the https://www.craftbeernamegenerator.com[Craft Beer Name Generator Service]
6577
<2> Fail the async method if the request to external service failed
6678
<3> Create a new `Beer`
6779
<4> Add the beer to the customer bill
68-
<5> Complete the async method with the generated beer
6980

7081
For `getMyBill()`:
82+
7183
[source,java,indent=0]
7284
----
7385
include::src/main/java/io/vertx/howtos/ebservice/beers/impl/BarmanServiceImpl.java[tags=getMyBill]
7486
----
7587

7688
For `settleMyBill()`:
89+
7790
[source,java,indent=0]
7891
----
7992
include::src/main/java/io/vertx/howtos/ebservice/beers/impl/BarmanServiceImpl.java[tags=payMyBill]
@@ -82,34 +95,40 @@ include::src/main/java/io/vertx/howtos/ebservice/beers/impl/BarmanServiceImpl.ja
8295
== Expose the service
8396

8497
Now we can expose the service inside the `BarmanVerticle` with the `ServiceBinder` class:
98+
8599
[source,java,indent=0]
86100
----
87101
include::src/main/java/io/vertx/howtos/ebservice/BarmanVerticle.java[]
88102
----
103+
89104
<1> Instantiate the `BarmanServiceImpl`
90105
<2> Instantiate the `ServiceBinder`
91106
<3> Configure the service address
92107
<4> Register the service on the event bus
93108

94109
== Use the service
95110

96-
Now we can use the service with the generated proxy. For demo purposes I'm going to use it inside another verticle called `DrunkVerticle`:
111+
Now we can use the service with the generated proxy.
112+
For demo purposes, we're going to use it inside another verticle called `DrunkVerticle`:
113+
97114
[source,java,indent=0]
98115
----
99116
include::src/main/java/io/vertx/howtos/ebservice/DrunkVerticle.java[]
100117
----
118+
101119
<1> Instantiate the proxy
102120
<2> Let's try the first beer 🍺
103-
<3> Something went wrong during beer retrieval, fail the verticle start
104-
<4> Drinking the first one 🍻
105-
<5> Give me another one 🍺
106-
<6> Drinking the second one 🍻
107-
<7> Time to go home. Give me the bill
108-
<8> Pay the bill
121+
<3> Drinking the first one 🍻
122+
<4> Give me another one 🍺
123+
<5> Drinking the second one 🍻
124+
<6> Time to go home.
125+
Give me the bill
126+
<7> Pay the bill
109127

110128
== Running the application
111129

112-
To run the application deploy the `BarmanVerticle` and subsequently the `DrunkVerticle`
130+
To run the application deploy the `BarmanVerticle` and subsequently the `DrunkVerticle`.
131+
113132
[source,java,indent=0]
114133
----
115134
include::src/main/java/io/vertx/howtos/ebservice/BeersApplication.java[]
@@ -121,13 +140,14 @@ You can run the application from:
121140
. with Maven: `mvn compile exec:java`
122141

123142
You should see something like this:
143+
124144
----
125145
The barman is ready to serve you
126-
Generated a new Beer! Sourpuss (American Malt Liquor)
127-
My first beer is a Sourpuss (American Malt Liquor) and it costs 5
128-
Generated a new Beer! Amateur Mountain IPA (American IPA)
129-
My second beer is a Amateur Mountain IPA (American IPA) and it costs 2
130-
My bill with the bar is 7
146+
Generated a new Beer! Manticore's Shorthand Barleywine (Barleywine)
147+
My first beer is a Manticore's Shorthand Barleywine and it costs 6€
148+
Generated a new Beer! Fortunate Pretzel (Cream Ale)
149+
My second beer is a Fortunate Pretzel and it costs 6€
150+
My bill with the bar is 12€
131151
Removed debt of homer
132152
----
133153

pom.xml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010
<version>1.0-SNAPSHOT</version>
1111

1212
<properties>
13-
<vertx.version>4.0.0</vertx.version>
14-
<maven.compiler.source>1.8</maven.compiler.source>
15-
<maven.compiler.target>1.8</maven.compiler.target>
13+
<vertx.version>5.0.0.CR2</vertx.version>
1614
</properties>
1715

1816
<dependencyManagement>
@@ -45,10 +43,18 @@
4543

4644
<build>
4745
<plugins>
46+
<plugin>
47+
<groupId>org.apache.maven.plugins</groupId>
48+
<artifactId>maven-compiler-plugin</artifactId>
49+
<version>3.13.0</version>
50+
<configuration>
51+
<release>11</release>
52+
</configuration>
53+
</plugin>
4854
<plugin>
4955
<groupId>org.codehaus.mojo</groupId>
5056
<artifactId>exec-maven-plugin</artifactId>
51-
<version>1.5.0</version>
57+
<version>3.5.0</version>
5258
<configuration>
5359
<mainClass>io.vertx.howtos.ebservice.BeersApplication</mainClass>
5460
</configuration>
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
package io.vertx.howtos.ebservice;
22

3-
import io.vertx.core.AbstractVerticle;
3+
import io.vertx.core.Future;
4+
import io.vertx.core.VerticleBase;
5+
import io.vertx.core.eventbus.MessageConsumer;
6+
import io.vertx.core.json.JsonObject;
47
import io.vertx.ext.web.client.WebClient;
58
import io.vertx.howtos.ebservice.beers.BarmanService;
69
import io.vertx.howtos.ebservice.beers.impl.BarmanServiceImpl;
710
import io.vertx.serviceproxy.ServiceBinder;
811

9-
public class BarmanVerticle extends AbstractVerticle {
12+
public class BarmanVerticle extends VerticleBase {
1013

1114
@Override
12-
public void start() {
15+
public Future<?> start() {
1316
BarmanService service = new BarmanServiceImpl(WebClient.create(vertx)); // <1>
1417

15-
new ServiceBinder(vertx) // <2>
18+
MessageConsumer<JsonObject> consumer = new ServiceBinder(vertx) // <2>
1619
.setAddress("beers.services.myapplication") // <3>
17-
.register(BarmanService.class, service); // <4>
20+
.register(BarmanService.class, service);// <4>
21+
22+
return consumer.completion();
1823
}
1924
}

src/main/java/io/vertx/howtos/ebservice/BeersApplication.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,9 @@ public class BeersApplication {
66

77
public static void main(String[] args) {
88
Vertx vertx = Vertx.vertx();
9-
vertx.deployVerticle(new BarmanVerticle(), ar -> {
10-
System.out.println("The barman is ready to serve you");
11-
vertx.deployVerticle(new DrunkVerticle(), ar2 -> {
12-
vertx.close();
13-
});
14-
});
9+
vertx.deployVerticle(new BarmanVerticle()).await();
10+
System.out.println("The barman is ready to serve you");
11+
vertx.deployVerticle(new DrunkVerticle()).await();
12+
vertx.close().await();
1513
}
16-
1714
}
Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,23 @@
11
package io.vertx.howtos.ebservice;
22

3-
import io.vertx.core.AbstractVerticle;
43
import io.vertx.core.Future;
5-
import io.vertx.core.Promise;
4+
import io.vertx.core.VerticleBase;
65
import io.vertx.howtos.ebservice.beers.BarmanService;
76

8-
public class DrunkVerticle extends AbstractVerticle {
7+
public class DrunkVerticle extends VerticleBase {
98

109
@Override
11-
public void start(Promise<Void> startPromise) {
10+
public Future<?> start() {
1211
BarmanService barmanService = BarmanService.createProxy(vertx, "beers.services.myapplication"); // <1>
13-
14-
barmanService.giveMeARandomBeer("homer", b1 -> { // <2>
15-
if (b1.failed()) { // <3>
16-
System.err.println("Cannot get my first beer!");
17-
startPromise.fail(b1.cause());
18-
return;
19-
}
20-
System.out.println("My first beer is a " + b1.result() + " and it costs " + b1.result().getPrice() + "€"); // <4>
21-
vertx.setTimer(1500, l ->
22-
barmanService.giveMeARandomBeer("homer", b2 -> { // <5>
23-
if (b2.failed()) {
24-
System.out.println("Cannot get my second beer!");
25-
startPromise.fail(b2.cause());
26-
return;
27-
}
28-
System.out.println("My second beer is a " + b2.result() + " and it costs " + b2.result().getPrice() + "€"); // <6>
29-
barmanService.getMyBill("homer", billAr -> {
30-
System.out.println("My bill with the bar is " + billAr.result()); // <7>
31-
barmanService.payMyBill("homer"); // <8>
32-
startPromise.complete();
33-
});
34-
})
35-
);
36-
});
37-
12+
return barmanService.giveMeARandomBeer("homer") // <2>
13+
.onSuccess(beer1 -> System.out.println("My first beer is a " + beer1.getName() + " and it costs " + beer1.getPrice() + "€")) // <3>
14+
.compose(v -> vertx.timer(1500))
15+
.compose(v -> barmanService.giveMeARandomBeer("homer")) // <4>
16+
.onSuccess(beer2 -> System.out.println("My second beer is a " + beer2.getName() + " and it costs " + beer2.getPrice() + "€")) // <5>
17+
.compose(v -> barmanService.getMyBill("homer")) // <6>
18+
.onSuccess(bill -> {
19+
System.out.println("My bill with the bar is " + bill + "€");
20+
barmanService.payMyBill("homer"); // <7>
21+
});
3822
}
3923
}

src/main/java/io/vertx/howtos/ebservice/beers/BarmanService.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@
22

33
import io.vertx.codegen.annotations.ProxyGen;
44
import io.vertx.codegen.annotations.VertxGen;
5-
import io.vertx.core.AsyncResult;
6-
import io.vertx.core.Handler;
5+
import io.vertx.core.Future;
76
import io.vertx.core.Vertx;
87

98
@VertxGen
109
@ProxyGen // <1>
1110
public interface BarmanService {
1211

13-
void giveMeARandomBeer(String customerName, Handler<AsyncResult<Beer>> handler); // <2>
12+
Future<Beer> giveMeARandomBeer(String customerName); // <2>
1413

15-
void getMyBill(String customerName, Handler<AsyncResult<Integer>> handler); // <3>
14+
Future<Integer> getMyBill(String customerName); // <3>
1615

1716
void payMyBill(String customerName); // <4>
1817

src/main/java/io/vertx/howtos/ebservice/beers/Beer.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package io.vertx.howtos.ebservice.beers;
22

33
import io.vertx.codegen.annotations.DataObject;
4+
import io.vertx.codegen.json.annotations.JsonGen;
45
import io.vertx.core.json.JsonObject;
56

6-
@DataObject(generateConverter = true)
7+
@DataObject
8+
@JsonGen
79
public class Beer {
810

911
String name;

src/main/java/io/vertx/howtos/ebservice/beers/impl/BarmanServiceImpl.java

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
package io.vertx.howtos.ebservice.beers.impl;
22

3-
import io.vertx.core.AsyncResult;
43
import io.vertx.core.Future;
5-
import io.vertx.core.Handler;
4+
import io.vertx.core.http.HttpResponseExpectation;
65
import io.vertx.core.json.JsonObject;
76
import io.vertx.ext.web.client.WebClient;
8-
import io.vertx.howtos.ebservice.beers.Beer;
97
import io.vertx.howtos.ebservice.beers.BarmanService;
8+
import io.vertx.howtos.ebservice.beers.Beer;
109

1110
import java.util.HashMap;
1211
import java.util.Map;
@@ -26,35 +25,30 @@ public BarmanServiceImpl(WebClient webClient) {
2625

2726
// tag::giveMeARandomBeer[]
2827
@Override
29-
public void giveMeARandomBeer(String customerName, Handler<AsyncResult<Beer>> handler) {
30-
webClient
31-
.get(443, "www.craftbeernamegenerator.com", "/api/api.php?type=classic")
28+
public Future<Beer> giveMeARandomBeer(String customerName) {
29+
return webClient
30+
.get(443, "www.craftbeernamegenerator.com", "/api/api.php?type=classic") // <1>
3231
.ssl(true)
33-
.send(ar -> { // <1>
34-
if (ar.failed()) handler.handle(Future.failedFuture(ar.cause())); // <2>
35-
else {
36-
JsonObject result = ar.result().bodyAsJsonObject();
37-
if (result.getInteger("status") != 200) // <2>
38-
handler.handle(Future.failedFuture("Beer Generator Service replied with " + result.getInteger("status") + ": " + result.getString("status_message")));
39-
else {
40-
Beer beer = new Beer( // <3>
41-
result.getJsonObject("data").getString("name"),
42-
result.getJsonObject("data").getString("style"),
43-
3 + random.nextInt(5)
44-
);
45-
System.out.println("Generated a new Beer! " + beer);
46-
bills.merge(customerName, beer.getPrice(), (oldVal, newVal) -> oldVal + newVal); // <4>
47-
handler.handle(Future.succeededFuture(beer)); // <5>
48-
}
49-
}
32+
.send()
33+
.expecting(HttpResponseExpectation.SC_OK) // <2>
34+
.map(bufferHttpResponse -> {
35+
JsonObject result = bufferHttpResponse.bodyAsJsonObject();
36+
Beer beer = new Beer( // <3>
37+
result.getJsonObject("data").getString("name"),
38+
result.getJsonObject("data").getString("style"),
39+
3 + random.nextInt(5)
40+
);
41+
System.out.println("Generated a new Beer! " + beer);
42+
bills.merge(customerName, beer.getPrice(), Integer::sum); // <4>
43+
return beer;
5044
});
5145
}
5246
// end::giveMeARandomBeer[]
5347

5448
// tag::getMyBill[]
5549
@Override
56-
public void getMyBill(String customerName, Handler<AsyncResult<Integer>> handler) {
57-
handler.handle(Future.succeededFuture(bills.get(customerName)));
50+
public Future<Integer> getMyBill(String customerName) {
51+
return Future.succeededFuture(bills.get(customerName));
5852
}
5953
// end::getMyBill[]
6054

0 commit comments

Comments
 (0)