-
Notifications
You must be signed in to change notification settings - Fork 10
/
VertxTodoServiceVerticle.java
159 lines (121 loc) · 5.63 KB
/
VertxTodoServiceVerticle.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package com.mammatustech.todo;
import io.advantageous.qbit.admin.ManagedServiceBuilder;
import io.advantageous.qbit.http.config.HttpServerConfig;
import io.advantageous.qbit.http.server.HttpServer;
import io.advantageous.qbit.meta.builder.ContextMetaBuilder;
import io.advantageous.qbit.system.QBitSystemManager;
import io.advantageous.qbit.vertx.http.VertxHttpServerBuilder;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.ext.web.Route;
import io.vertx.ext.web.Router;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
/**
* This is an alternate example that uses QBit Embedded inside of Vertx
* instead of running QBit Standalone.
*/
public class VertxTodoServiceVerticle extends AbstractVerticle {
/** Used to startup QBit and use features like swagger, etc. */
private final ManagedServiceBuilder managedServiceBuilder;
/** Used to shutdown QBit services cleanly. */
private QBitSystemManager systemManager;
/**
* Create this verticle
* @param managedServiceBuilder managedServiceBuilder
*/
public VertxTodoServiceVerticle(ManagedServiceBuilder managedServiceBuilder) {
this.managedServiceBuilder = managedServiceBuilder;
}
public static void main(final String... args) throws Exception{
final ManagedServiceBuilder managedServiceBuilder = createManagedServiceBuilder();
final CountDownLatch latch = new CountDownLatch(1);
final Vertx vertx = Vertx.vertx();
vertx.deployVerticle(new VertxTodoServiceVerticle(managedServiceBuilder), result -> {
if (result.succeeded()) {
System.out.println("Deployment id is: " + result.result());
} else {
System.out.println("Deployment failed!");
result.cause().printStackTrace();
}
latch.countDown();
});
latch.await(5, TimeUnit.SECONDS);
System.out.println("QBit and Vertx are open for e-Busbies");
}
@Override
public void start() throws Exception {
/* Vertx HTTP Server. */
final io.vertx.core.http.HttpServer vertxHttpServer =
this.getVertx().createHttpServer();
/* Route one call to a vertx handler. */
final Router router = createRouterAndVertxOnlyRoute();
final HttpServer httpServer = getHttpServerAndRoutes(vertxHttpServer, router);
systemManager = managedServiceBuilder.getSystemManager();
managedServiceBuilder.addEndpointService(new TodoService());
/*
* Create and start new service endpointServer.
*/
managedServiceBuilder
.getEndpointServerBuilder()
.setHttpServer(httpServer)
.build()
.startServer();
/*
* Associate the router as a request handler for the vertxHttpServer.
*/
vertxHttpServer.requestHandler(router::accept).listen(
managedServiceBuilder.getPort());
}
private HttpServer getHttpServerAndRoutes(final io.vertx.core.http.HttpServer vertxHttpServer, final Router router) {
/* Route everything under /v1 to QBit http server. */
final Route qbitRoute = router.route().path("/v1/*");
/*
* Use the VertxHttpServerBuilder which is a special builder for Vertx/Qbit integration.
*/
return VertxHttpServerBuilder.vertxHttpServerBuilder()
.setRoute(qbitRoute)
.setHttpServer(vertxHttpServer)
.setVertx(getVertx())
.setConfig(new HttpServerConfig()) //not needed in master branch of qbit workaround for bug
.build();
}
private Router createRouterAndVertxOnlyRoute() {
final Router router = Router.router(getVertx()); //Vertx router
router.route("/svr/rout1/").handler(routingContext -> {
HttpServerResponse response = routingContext.response();
response.setStatusCode(202);
response.end("route1 this does not have very much data to transfer");
});
return router;
}
@Override
public void stop() throws Exception {
if (systemManager!=null) {
systemManager.shutDown();
}
}
private static ManagedServiceBuilder createManagedServiceBuilder() {
/* Create the ManagedServiceBuilder which manages a clean shutdown, health, stats, etc. */
final ManagedServiceBuilder managedServiceBuilder =
ManagedServiceBuilder.managedServiceBuilder()
.setRootURI("/v1") //Defaults to services
.setPort(8888); //Defaults to 8080 or environment variable PORT
/* Context meta builder to document this endpoint.
* Gets used by swagger support.
*/
ContextMetaBuilder contextMetaBuilder = managedServiceBuilder.getContextMetaBuilder();
contextMetaBuilder.setContactEmail("[email protected]");
contextMetaBuilder.setDescription("A great service to show building a todo list");
contextMetaBuilder.setContactURL("http://www.bwbl.lunati/master/of/rodeo");
contextMetaBuilder.setContactName("Buffalo Wild Bill Lunati");
contextMetaBuilder.setLicenseName("Commercial");
contextMetaBuilder.setLicenseURL("http://www.canttouchthis.com");
contextMetaBuilder.setTitle("Todo Title");
contextMetaBuilder.setVersion("47.0");
managedServiceBuilder.getStatsDReplicatorBuilder().setHost("192.168.59.103");
managedServiceBuilder.setEnableStatsD(true);
return managedServiceBuilder;
}
}