Skip to content

Commit 408a7fc

Browse files
Added example showing how to use Vertx and QBit
1 parent 6068168 commit 408a7fc

File tree

7 files changed

+187
-31
lines changed

7 files changed

+187
-31
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,5 @@ todo-scala/.gradle/
4545
todo-scala/.idea/
4646
todo-scala/build/
4747

48+
49+
foo/

todo-scala/src/main/scala/com/mammatustech/todo/Todo.scala

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,10 @@ package com.mammatustech.todo
33
import io.advantageous.qbit.annotation.Description
44

55
@Description("A `TodoItem`.")
6-
class Todo (aName: String, aDescription: String, aCreateTime: Long) {
7-
8-
@Description("Holds the description of the todo item")
9-
val description: String = aDescription
10-
@Description("Holds the name of the todo item")
11-
val name: String = aName
12-
val createTime: Long = aCreateTime
13-
val id: String = name + "::" + createTime
14-
15-
16-
def canEqual(other: Any): Boolean = other.isInstanceOf[Todo]
17-
18-
override def equals(other: Any): Boolean = other match {
19-
case that: Todo =>
20-
(that canEqual this) &&
21-
description == that.description &&
22-
name == that.name &&
23-
id == that.id &&
24-
createTime == that.createTime
25-
case _ => false
26-
}
27-
28-
override def hashCode(): Int = {
29-
val state = Seq(description, name, id, createTime)
30-
state.map(_.hashCode()).foldLeft(0)((a, b) => 31 * a + b)
31-
}
6+
case class Todo (name: String, description: String, createTime: Long) {
7+
def getName = { this.name }
8+
def getDescription = {this.description}
9+
def getCreateTime = { this.createTime }
10+
val id = s"$name-$createTime"
11+
def getId = {this.id}
3212
}

todo-scala/src/main/scala/com/mammatustech/todo/TodoService.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class TodoService {
6464
summary = "adds todo",
6565
returnDescription = "returns true if successful")
6666
def add(todo: Todo) = {
67-
todoMap.put(todo.id, todo)
67+
todoMap.put(todo.getId, todo)
6868
true
6969
}
7070

todo/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ repositories {
1818

1919
dependencies {
2020
testCompile group: 'junit', name: 'junit', version: '4.11'
21-
compile group: 'io.advantageous.qbit', name: 'qbit-admin', version: '0.9.0.M5-SNAPSHOT'
22-
compile group: 'io.advantageous.qbit', name: 'qbit-vertx3', version: '0.9.0.M5-SNAPSHOT'
21+
22+
compile 'com.github.advantageous:qbit-admin:0.9.1-RC2'
2323

2424
}

todo/src/main/java/com/mammatustech/todo/HttpClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class HttpClient {
77

88
public static void main(final String... args) throws Exception {
99

10-
for (int index = 0; index < 100; index++) {
10+
for (int index = 0; index < 5; index++) {
1111

1212
HTTP.postJSON("http://localhost:8888/v1/todo-service/todo",
1313
JsonFactory.toJson(new Todo("name" + index,

todo/src/main/java/com/mammatustech/todo/TodoService.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import io.advantageous.qbit.annotation.RequestMapping;
44
import io.advantageous.qbit.annotation.RequestMethod;
55
import io.advantageous.qbit.annotation.RequestParam;
6+
import io.advantageous.qbit.reactive.Callback;
67

78
import java.util.*;
89

@@ -62,6 +63,20 @@ public class TodoService {
6263

6364
private final Map<String, Todo> todoMap = new TreeMap<>();
6465

66+
@RequestMapping(value = "/ping", method = RequestMethod.GET,
67+
description = "ping", summary = "ping",
68+
returnDescription = "returns true if successful")
69+
public boolean ping() {
70+
return true;
71+
}
72+
73+
74+
@RequestMapping(value = "/ping2", method = RequestMethod.GET,
75+
description = "ping2", summary = "ping2",
76+
returnDescription = "returns true if successful")
77+
public void ping2(Callback<Boolean> callback) {
78+
callback.accept(true);
79+
}
6580

6681
@RequestMapping(value = "/todo", method = RequestMethod.POST,
6782
description = "add a todo item to the list", summary = "adds todo",
@@ -84,7 +99,7 @@ public void remove(@RequestParam(value = "id", description = "id of Todo item to
8499

85100

86101

87-
@RequestMapping(value = "/todo", method = RequestMethod.GET,
102+
@RequestMapping(value = "/todo/", method = RequestMethod.GET,
88103
description = "List all items in the system", summary = "list items",
89104
returnDescription = "return list of all items in system")
90105
public List<Todo> list() {
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
package com.mammatustech.todo;
2+
3+
import io.advantageous.qbit.admin.ManagedServiceBuilder;
4+
import io.advantageous.qbit.http.config.HttpServerConfig;
5+
import io.advantageous.qbit.http.server.HttpServer;
6+
import io.advantageous.qbit.meta.builder.ContextMetaBuilder;
7+
import io.advantageous.qbit.system.QBitSystemManager;
8+
import io.advantageous.qbit.vertx.http.VertxHttpServerBuilder;
9+
import io.vertx.core.AbstractVerticle;
10+
import io.vertx.core.Vertx;
11+
import io.vertx.core.http.HttpServerResponse;
12+
import io.vertx.ext.web.Route;
13+
import io.vertx.ext.web.Router;
14+
15+
import java.util.concurrent.CountDownLatch;
16+
import java.util.concurrent.TimeUnit;
17+
18+
/**
19+
* This is an alternate example that uses QBit Embedded inside of Vertx
20+
* instead of running QBit Standalone.
21+
*/
22+
public class VertxTodoServiceVerticle extends AbstractVerticle {
23+
24+
/** Used to startup QBit and use features like swagger, etc. */
25+
private final ManagedServiceBuilder managedServiceBuilder;
26+
/** Used to shutdown QBit services cleanly. */
27+
private QBitSystemManager systemManager;
28+
29+
/**
30+
* Create this verticle
31+
* @param managedServiceBuilder managedServiceBuilder
32+
*/
33+
public VertxTodoServiceVerticle(ManagedServiceBuilder managedServiceBuilder) {
34+
this.managedServiceBuilder = managedServiceBuilder;
35+
}
36+
37+
public static void main(final String... args) throws Exception{
38+
final ManagedServiceBuilder managedServiceBuilder = createManagedServiceBuilder();
39+
40+
41+
final CountDownLatch latch = new CountDownLatch(1);
42+
final Vertx vertx = Vertx.vertx();
43+
vertx.deployVerticle(new VertxTodoServiceVerticle(managedServiceBuilder), result -> {
44+
if (result.succeeded()) {
45+
System.out.println("Deployment id is: " + result.result());
46+
} else {
47+
System.out.println("Deployment failed!");
48+
result.cause().printStackTrace();
49+
}
50+
latch.countDown();
51+
});
52+
53+
54+
latch.await(5, TimeUnit.SECONDS);
55+
56+
57+
System.out.println("QBit and Vertx are open for e-Busbies");
58+
}
59+
60+
61+
@Override
62+
public void start() throws Exception {
63+
64+
65+
/* Vertx HTTP Server. */
66+
final io.vertx.core.http.HttpServer vertxHttpServer =
67+
this.getVertx().createHttpServer();
68+
69+
/* Route one call to a vertx handler. */
70+
final Router router = createRouterAndVertxOnlyRoute();
71+
72+
final HttpServer httpServer = getHttpServerAndRoutes(vertxHttpServer, router);
73+
74+
systemManager = managedServiceBuilder.getSystemManager();
75+
76+
managedServiceBuilder.addEndpointService(new TodoService());
77+
78+
79+
/*
80+
* Create and start new service endpointServer.
81+
*/
82+
managedServiceBuilder
83+
.getEndpointServerBuilder()
84+
.setHttpServer(httpServer)
85+
.build()
86+
.startServer();
87+
88+
89+
/*
90+
* Associate the router as a request handler for the vertxHttpServer.
91+
*/
92+
vertxHttpServer.requestHandler(router::accept).listen(
93+
managedServiceBuilder.getPort());
94+
}
95+
96+
private HttpServer getHttpServerAndRoutes(final io.vertx.core.http.HttpServer vertxHttpServer, final Router router) {
97+
98+
/* Route everything under /v1 to QBit http server. */
99+
final Route qbitRoute = router.route().path("/v1/*");
100+
101+
102+
103+
/*
104+
* Use the VertxHttpServerBuilder which is a special builder for Vertx/Qbit integration.
105+
*/
106+
return VertxHttpServerBuilder.vertxHttpServerBuilder()
107+
.setRoute(qbitRoute)
108+
.setHttpServer(vertxHttpServer)
109+
.setVertx(getVertx())
110+
.setConfig(new HttpServerConfig()) //not needed in master branch of qbit workaround for bug
111+
.build();
112+
}
113+
114+
private Router createRouterAndVertxOnlyRoute() {
115+
final Router router = Router.router(getVertx()); //Vertx router
116+
router.route("/svr/rout1/").handler(routingContext -> {
117+
HttpServerResponse response = routingContext.response();
118+
response.setStatusCode(202);
119+
response.end("route1 this does not have very much data to transfer");
120+
});
121+
return router;
122+
}
123+
124+
@Override
125+
public void stop() throws Exception {
126+
if (systemManager!=null) {
127+
systemManager.shutDown();
128+
}
129+
}
130+
131+
132+
private static ManagedServiceBuilder createManagedServiceBuilder() {
133+
/* Create the ManagedServiceBuilder which manages a clean shutdown, health, stats, etc. */
134+
final ManagedServiceBuilder managedServiceBuilder =
135+
ManagedServiceBuilder.managedServiceBuilder()
136+
.setRootURI("/v1") //Defaults to services
137+
.setPort(8888); //Defaults to 8080 or environment variable PORT
138+
139+
/* Context meta builder to document this endpoint.
140+
* Gets used by swagger support.
141+
*/
142+
ContextMetaBuilder contextMetaBuilder = managedServiceBuilder.getContextMetaBuilder();
143+
contextMetaBuilder.setContactEmail("lunati-not-real-email@gmail.com");
144+
contextMetaBuilder.setDescription("A great service to show building a todo list");
145+
contextMetaBuilder.setContactURL("http://www.bwbl.lunati/master/of/rodeo");
146+
contextMetaBuilder.setContactName("Buffalo Wild Bill Lunati");
147+
contextMetaBuilder.setLicenseName("Commercial");
148+
contextMetaBuilder.setLicenseURL("http://www.canttouchthis.com");
149+
contextMetaBuilder.setTitle("Todo Title");
150+
contextMetaBuilder.setVersion("47.0");
151+
152+
153+
managedServiceBuilder.getStatsDReplicatorBuilder().setHost("192.168.59.103");
154+
managedServiceBuilder.setEnableStatsD(true);
155+
return managedServiceBuilder;
156+
}
157+
158+
159+
}

0 commit comments

Comments
 (0)