/
Test6_1.java
87 lines (57 loc) · 2.23 KB
/
Test6_1.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import io.vertx.core.AbstractVerticle;
import io.vertx.core.AsyncResult;
import io.vertx.core.DeploymentOptions;
import io.vertx.core.MultiMap;
import io.vertx.core.eventbus.EventBus;
import io.vertx.core.eventbus.Message;
import io.vertx.core.http.HttpServerOptions;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.Route;
import io.vertx.ext.web.Router;
public class Test6_1 extends AbstractVerticle {
@Override
public void start() throws Exception {
super.start();
EventBus eventBus = vertx.eventBus();
JsonObject config = context.config();
DeploymentOptions options = new DeploymentOptions().setConfig(config);
vertx.deployVerticle("Test6_2.java", options);
vertx.deployVerticle("Test6_3.java", options);
/////////////////////////////////////////////////
// HTTP Server
/////////////////////////////////////////////////
Router router = Router.router(vertx);
Route route = router.route("/:METHOD");
route.handler(routingContext -> {
HttpServerRequest request = routingContext.request();
MultiMap params = request.params();
String method = params.get("METHOD");
System.out.println(method);
request.endHandler(empty -> {
JsonObject param = new JsonObject();
params.forEach(entry -> param.put(entry.getKey(), entry.getValue()));
eventBus.send(method, param, (AsyncResult<Message<String>> result) -> {
if (result.succeeded()) {
String returnValue = result.result().body();
request.response().putHeader("content-type", "application/json");
request.response().end("{\"METHOD\": \"" + method + "\", \"RESULT\": \"" + returnValue + "\"}");
} else {
request.response().putHeader("content-type", "application/json");
request.response().end("{\"METHOD\": \"" + method + "\", \"ERROR\": \"" + result.cause().getMessage() + "\"}");
result.cause().printStackTrace();
}
});
System.out.println("Send End!!!");
});
});
HttpServerOptions httpServerOptions = new HttpServerOptions();
httpServerOptions.setCompressionSupported(true);
vertx.createHttpServer(httpServerOptions)
.requestHandler(router::accept)
.listen(8080);
}
@Override
public void stop() throws Exception {
}
}