Skip to content

Commit 36dcd53

Browse files
committed
Merge pull request vert-x#20 from vert-x3/http-server-sharing-example
Add an example illustrating the HTTP Server sharing
2 parents db1453d + ba8795b commit 36dcd53

File tree

5 files changed

+97
-0
lines changed

5 files changed

+97
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
vertx.setPeriodic(100, { l ->
3+
vertx.createHttpClient().getNow(8080, "localhost", "/", { resp ->
4+
resp.bodyHandler({ body ->
5+
println(body.toString("ISO-8859-1"))
6+
})
7+
})
8+
})
9+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package io.vertx.example.core.http.sharing;
2+
3+
import io.vertx.core.AbstractVerticle;
4+
import io.vertx.example.util.Runner;
5+
6+
/**
7+
* A client illustrating the round robin made by vert.x. The client send a request to the server periodically and
8+
* print the received messages.
9+
*/
10+
public class Client extends AbstractVerticle {
11+
12+
// Convenience method so you can run it in your IDE
13+
public static void main(String[] args) {
14+
Runner.runExample(Client.class);
15+
}
16+
17+
@Override
18+
public void start() throws Exception {
19+
20+
vertx.setPeriodic(100, (l) -> {
21+
vertx.createHttpClient().getNow(8080, "localhost", "/", resp -> {
22+
resp.bodyHandler(body -> {
23+
System.out.println(body.toString("ISO-8859-1"));
24+
});
25+
});
26+
}
27+
);
28+
29+
}
30+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package io.vertx.example.core.http.sharing;
2+
3+
import io.vertx.core.AbstractVerticle;
4+
import io.vertx.example.util.Runner;
5+
6+
/**
7+
* An example illustrating the server sharing and round robin. The servers are identified using an id.
8+
*/
9+
public class Server extends AbstractVerticle {
10+
11+
// Convenience method so you can run it in your IDE
12+
public static void main(String[] args) {
13+
Runner.runExample(Server.class);
14+
}
15+
16+
@Override
17+
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+
}
39+
}
40+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
vertx.setPeriodic(100, function (l) {
3+
vertx.createHttpClient().getNow(8080, "localhost", "/", function (resp) {
4+
resp.bodyHandler(function (body) {
5+
console.log(body.toString("ISO-8859-1"));
6+
});
7+
});
8+
});
9+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
$vertx.set_periodic(100) { |l|
3+
$vertx.create_http_client().get_now(8080, "localhost", "/") { |resp|
4+
resp.body_handler() { |body|
5+
puts body.to_string("ISO-8859-1")
6+
}
7+
}
8+
}
9+

0 commit comments

Comments
 (0)