forked from vert-x/vertx-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request vert-x#21 from vert-x3/http-server-sharing-example
Improve the sharing HTTP server example
- Loading branch information
Showing
10 changed files
with
58 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 1 addition & 3 deletions
4
core-examples/src/main/groovy/io/vertx/example/core/http/sharing/client.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
}) | ||
}) | ||
}) | ||
|
3 changes: 3 additions & 0 deletions
3
core-examples/src/main/groovy/io/vertx/example/core/http/sharing/httpserververticle.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
core-examples/src/main/java/io/vertx/example/core/http/sharing/HttpServerVerticle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
27 changes: 5 additions & 22 deletions
27
core-examples/src/main/java/io/vertx/example/core/http/sharing/Server.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
4 changes: 1 addition & 3 deletions
4
core-examples/src/main/js/io/vertx/example/core/http/sharing/client.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
}); | ||
}); | ||
}); | ||
|
3 changes: 3 additions & 0 deletions
3
core-examples/src/main/js/io/vertx/example/core/http/sharing/httpserververticle.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
4 changes: 1 addition & 3 deletions
4
core-examples/src/main/rb/io/vertx/example/core/http/sharing/client.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} | ||
} | ||
|
3 changes: 3 additions & 0 deletions
3
core-examples/src/main/rb/io/vertx/example/core/http/sharing/httpserververticle.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |