Skip to content

Commit 584cc70

Browse files
committed
Improve the sharing HTTP server example
This commit adds a section in the REDME file with some explanation and launch instructions It also change how was made the example to allow code generation (codetrans) It also modifies the example to rely on the `-instances` vert.x parameter Signed-off-by: Clement Escoffier <[email protected]>
1 parent ba8795b commit 584cc70

File tree

10 files changed

+58
-34
lines changed

10 files changed

+58
-34
lines changed

core-examples/README.adoc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,28 @@ is uploaded successfully even if the file is very large (GigaBytes).
138138
link:src/main/java/io/vertx/example/core/http/upload/Server.java[Java upload server example]
139139
link:src/main/java/io/vertx/example/core/http/upload/Client.java[Java upload client example]
140140

141+
=== HTTP Server Sharing
142+
143+
A server that illustrates the round robin orchestrated by vert.x when several verticles are opening HTTP servers on the same port:
144+
145+
link:src/main/java/io/vertx/example/core/http/sharing/Server.java[Server Launcher]
146+
147+
link:src/main/java/io/vertx/example/core/http/sharing/HttpServerVerticle.java[HTTP Server Verticle]
148+
149+
The `Server` deploys two instances of the `HttpServerVerticle` verticle.
150+
151+
You can run the server then open a browser and point it at link:http://localhost:8080[]. Requests will be handled by an instance after the other.
152+
153+
The `Client` illustrates the round robin by periodically requesting the server and displays the response content.
154+
155+
link:src/main/java/io/vertx/example/core/http/sharing/Client.java[Java simple HTTP client]
156+
157+
You can directly launch the `HTTPServerVerticle` using the `vertx run` command. Then you can set the number of instance you want:
158+
159+
```
160+
vertx run io.vertx.example.core.http.sharing.HttpServerVerticle -instances 4
161+
```
162+
141163
=== WebSockets echo example
142164

143165
This example shows a Vert.x HTTP server which handles websockets connections. This example simply echoes back to the client
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
2-
vertx.setPeriodic(100, { l ->
1+
vertx.setPeriodic(1000, { l ->
32
vertx.createHttpClient().getNow(8080, "localhost", "/", { resp ->
43
resp.bodyHandler({ body ->
54
println(body.toString("ISO-8859-1"))
65
})
76
})
87
})
9-
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vertx.createHttpServer().requestHandler({ req ->
2+
req.response().putHeader("content-type", "text/html").end("<html><body><h1>Hello from ${this}</h1></body></html>")
3+
}).listen(8080)

core-examples/src/main/java/io/vertx/example/core/http/sharing/Client.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,13 @@ public static void main(String[] args) {
1616

1717
@Override
1818
public void start() throws Exception {
19-
20-
vertx.setPeriodic(100, (l) -> {
19+
vertx.setPeriodic(1000, (l) -> {
2120
vertx.createHttpClient().getNow(8080, "localhost", "/", resp -> {
2221
resp.bodyHandler(body -> {
2322
System.out.println(body.toString("ISO-8859-1"));
2423
});
2524
});
2625
}
2726
);
28-
2927
}
3028
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package io.vertx.example.core.http.sharing;
2+
3+
import io.vertx.core.AbstractVerticle;
4+
5+
/**
6+
* A very simple HTTP server returning it's 'id' in the response.
7+
*/
8+
public class HttpServerVerticle extends AbstractVerticle {
9+
@Override
10+
public void start() throws Exception {
11+
vertx.createHttpServer().requestHandler(req -> {
12+
req.response()
13+
.putHeader("content-type", "text/html")
14+
.end("<html><body><h1>Hello from " + this + "</h1></body></html>");
15+
}).listen(8080);
16+
}
17+
}
18+
Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,23 @@
11
package io.vertx.example.core.http.sharing;
22

33
import io.vertx.core.AbstractVerticle;
4+
import io.vertx.core.DeploymentOptions;
45
import io.vertx.example.util.Runner;
56

67
/**
78
* An example illustrating the server sharing and round robin. The servers are identified using an id.
9+
* The HTTP Server Verticle is instantiated twice in the deployment options.
810
*/
911
public class Server extends AbstractVerticle {
1012

11-
// Convenience method so you can run it in your IDE
1213
public static void main(String[] args) {
1314
Runner.runExample(Server.class);
1415
}
1516

1617
@Override
1718
public void start() throws Exception {
18-
getVertx().deployVerticle(new HttpVerticle("server-1"));
19-
getVertx().deployVerticle(new HttpVerticle("server-2"));
20-
}
21-
22-
23-
private class HttpVerticle extends AbstractVerticle {
24-
25-
private final String id;
26-
27-
private HttpVerticle(String id) {
28-
this.id = id;
29-
}
30-
31-
32-
@Override
33-
public void start() throws Exception {
34-
vertx.createHttpServer().requestHandler(req -> {
35-
req.response().putHeader("content-type", "text/html").end("<html><body><h1>Hello from " +
36-
id + "</h1></body></html>");
37-
}).listen(8080);
38-
}
19+
getVertx().deployVerticle(
20+
"io.vertx.example.core.http.sharing.HttpServerVerticle",
21+
new DeploymentOptions().setInstances(2));
3922
}
4023
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
2-
vertx.setPeriodic(100, function (l) {
1+
vertx.setPeriodic(1000, function (l) {
32
vertx.createHttpClient().getNow(8080, "localhost", "/", function (resp) {
43
resp.bodyHandler(function (body) {
54
console.log(body.toString("ISO-8859-1"));
65
});
76
});
87
});
9-
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vertx.createHttpServer().requestHandler(function (req) {
2+
req.response().putHeader("content-type", "text/html").end("<html><body><h1>Hello from " + this + "</h1></body></html>");
3+
}).listen(8080);
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
2-
$vertx.set_periodic(100) { |l|
1+
$vertx.set_periodic(1000) { |l|
32
$vertx.create_http_client().get_now(8080, "localhost", "/") { |resp|
43
resp.body_handler() { |body|
54
puts body.to_string("ISO-8859-1")
65
}
76
}
87
}
9-
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
$vertx.create_http_server().request_handler() { |req|
2+
req.response().put_header("content-type", "text/html").end("<html><body><h1>Hello from #{$this}</h1></body></html>")
3+
}.listen(8080)

0 commit comments

Comments
 (0)