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#20 from vert-x3/http-server-sharing-example
Add an example illustrating the HTTP Server sharing
- Loading branch information
Showing
5 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
|
||
vertx.setPeriodic(100, { l -> | ||
vertx.createHttpClient().getNow(8080, "localhost", "/", { resp -> | ||
resp.bodyHandler({ body -> | ||
println(body.toString("ISO-8859-1")) | ||
}) | ||
}) | ||
}) | ||
|
30 changes: 30 additions & 0 deletions
30
core-examples/src/main/java/io/vertx/example/core/http/sharing/Client.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,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")); | ||
}); | ||
}); | ||
} | ||
); | ||
|
||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
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 |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
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 |
---|---|---|
@@ -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")); | ||
}); | ||
}); | ||
}); | ||
|
9 changes: 9 additions & 0 deletions
9
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 |
---|---|---|
@@ -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") | ||
} | ||
} | ||
} | ||
|