Skip to content

Commit

Permalink
Merge pull request vert-x#20 from vert-x3/http-server-sharing-example
Browse files Browse the repository at this point in the history
Add an example illustrating the HTTP Server sharing
  • Loading branch information
cescoffier committed Jun 4, 2015
2 parents db1453d + ba8795b commit 36dcd53
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

vertx.setPeriodic(100, { l ->
vertx.createHttpClient().getNow(8080, "localhost", "/", { resp ->
resp.bodyHandler({ body ->
println(body.toString("ISO-8859-1"))
})
})
})

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.vertx.example.core.http.sharing;

import io.vertx.core.AbstractVerticle;
import io.vertx.example.util.Runner;

/**
* A client illustrating the round robin made by vert.x. The client send a request to the server periodically and
* print the received messages.
*/
public class Client extends AbstractVerticle {

// Convenience method so you can run it in your IDE
public static void main(String[] args) {
Runner.runExample(Client.class);
}

@Override
public void start() throws Exception {

vertx.setPeriodic(100, (l) -> {
vertx.createHttpClient().getNow(8080, "localhost", "/", resp -> {
resp.bodyHandler(body -> {
System.out.println(body.toString("ISO-8859-1"));
});
});
}
);

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.vertx.example.core.http.sharing;

import io.vertx.core.AbstractVerticle;
import io.vertx.example.util.Runner;

/**
* An example illustrating the server sharing and round robin. The servers are identified using an id.
*/
public class Server extends AbstractVerticle {

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

@Override
public void start() throws Exception {
getVertx().deployVerticle(new HttpVerticle("server-1"));
getVertx().deployVerticle(new HttpVerticle("server-2"));
}


private class HttpVerticle extends AbstractVerticle {

private final String id;

private HttpVerticle(String id) {
this.id = id;
}


@Override
public void start() throws Exception {
vertx.createHttpServer().requestHandler(req -> {
req.response().putHeader("content-type", "text/html").end("<html><body><h1>Hello from " +
id + "</h1></body></html>");
}).listen(8080);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

vertx.setPeriodic(100, function (l) {
vertx.createHttpClient().getNow(8080, "localhost", "/", function (resp) {
resp.bodyHandler(function (body) {
console.log(body.toString("ISO-8859-1"));
});
});
});

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

$vertx.set_periodic(100) { |l|
$vertx.create_http_client().get_now(8080, "localhost", "/") { |resp|
resp.body_handler() { |body|
puts body.to_string("ISO-8859-1")
}
}
}

0 comments on commit 36dcd53

Please sign in to comment.