Skip to content

Commit

Permalink
Merge pull request vert-x#21 from vert-x3/http-server-sharing-example
Browse files Browse the repository at this point in the history
Improve the sharing HTTP server example
  • Loading branch information
purplefox committed Jun 4, 2015
2 parents 36dcd53 + 584cc70 commit ea519ad
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 34 deletions.
22 changes: 22 additions & 0 deletions core-examples/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,28 @@ is uploaded successfully even if the file is very large (GigaBytes).
link:src/main/java/io/vertx/example/core/http/upload/Server.java[Java upload server example]
link:src/main/java/io/vertx/example/core/http/upload/Client.java[Java upload client example]

=== HTTP Server Sharing

A server that illustrates the round robin orchestrated by vert.x when several verticles are opening HTTP servers on the same port:

link:src/main/java/io/vertx/example/core/http/sharing/Server.java[Server Launcher]

link:src/main/java/io/vertx/example/core/http/sharing/HttpServerVerticle.java[HTTP Server Verticle]

The `Server` deploys two instances of the `HttpServerVerticle` verticle.

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.

The `Client` illustrates the round robin by periodically requesting the server and displays the response content.

link:src/main/java/io/vertx/example/core/http/sharing/Client.java[Java simple HTTP client]

You can directly launch the `HTTPServerVerticle` using the `vertx run` command. Then you can set the number of instance you want:

```
vertx run io.vertx.example.core.http.sharing.HttpServerVerticle -instances 4
```

=== WebSockets echo example

This example shows a Vert.x HTTP server which handles websockets connections. This example simply echoes back to the client
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@

vertx.setPeriodic(100, { l ->
vertx.setPeriodic(1000, { 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,3 @@
vertx.createHttpServer().requestHandler({ req ->
req.response().putHeader("content-type", "text/html").end("<html><body><h1>Hello from ${this}</h1></body></html>")
}).listen(8080)
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@ public static void main(String[] args) {

@Override
public void start() throws Exception {

vertx.setPeriodic(100, (l) -> {
vertx.setPeriodic(1000, (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,18 @@
package io.vertx.example.core.http.sharing;

import io.vertx.core.AbstractVerticle;

/**
* A very simple HTTP server returning it's 'id' in the response.
*/
public class HttpServerVerticle extends AbstractVerticle {
@Override
public void start() throws Exception {
vertx.createHttpServer().requestHandler(req -> {
req.response()
.putHeader("content-type", "text/html")
.end("<html><body><h1>Hello from " + this + "</h1></body></html>");
}).listen(8080);
}
}

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

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

/**
* An example illustrating the server sharing and round robin. The servers are identified using an id.
* The HTTP Server Verticle is instantiated twice in the deployment options.
*/
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);
}
getVertx().deployVerticle(
"io.vertx.example.core.http.sharing.HttpServerVerticle",
new DeploymentOptions().setInstances(2));
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@

vertx.setPeriodic(100, function (l) {
vertx.setPeriodic(1000, 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,3 @@
vertx.createHttpServer().requestHandler(function (req) {
req.response().putHeader("content-type", "text/html").end("<html><body><h1>Hello from " + this + "</h1></body></html>");
}).listen(8080);
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
$vertx.create_http_server().request_handler() { |req|
req.response().put_header("content-type", "text/html").end("<html><body><h1>Hello from #{$this}</h1></body></html>")
}.listen(8080)

0 comments on commit ea519ad

Please sign in to comment.