Skip to content

Commit b9683e2

Browse files
Updating to the latest versions and latest styles.
1 parent e3138ba commit b9683e2

File tree

4 files changed

+220
-112
lines changed

4 files changed

+220
-112
lines changed
Lines changed: 6 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -1,119 +1,16 @@
11
package com.mammatustech.todo;
22

3-
import io.advantageous.qbit.admin.ServiceManagementBundle;
4-
import io.advantageous.qbit.annotation.Named;
5-
import io.advantageous.qbit.annotation.RequestMapping;
6-
import io.advantageous.qbit.annotation.RequestMethod;
7-
import io.advantageous.qbit.annotation.RequestParam;
8-
import io.advantageous.qbit.annotation.http.DELETE;
9-
import io.advantageous.qbit.annotation.http.GET;
10-
import io.advantageous.qbit.annotation.http.POST;
11-
import io.advantageous.qbit.reactive.Callback;
3+
import io.advantageous.reakt.promise.Promise;
124

13-
import java.time.Duration;
145
import java.util.ArrayList;
15-
import java.util.Map;
16-
import java.util.TreeMap;
17-
186

197
/**
20-
* Default port for admin is 7777.
21-
* Default port for main endpoint is 8888.
22-
*
23-
* <pre>
24-
* <code>
25-
*
26-
* Access the service:
27-
*
28-
* $ curl http://localhost:8888/v1/...
29-
*
30-
*
31-
* To see swagger file for this service:
32-
*
33-
* $ curl http://localhost:7777/__admin/meta/
34-
*
35-
* To see health for this service:
36-
*
37-
* $ curl http://localhost:8888/__health -v
38-
* Returns "ok" if all registered health systems are healthy.
39-
*
40-
* OR if same port endpoint health is disabled then:
41-
*
42-
* $ curl http://localhost:7777/__admin/ok -v
43-
* Returns "true" if all registered health systems are healthy.
44-
*
45-
*
46-
* A node is a service, service bundle, queue, or server endpoint that is being monitored.
47-
*
48-
* List all service nodes or endpoints
49-
*
50-
* $ curl http://localhost:7777/__admin/all-nodes/
51-
*
52-
*
53-
* List healthy nodes by name:
54-
*
55-
* $ curl http://localhost:7777/__admin/healthy-nodes/
56-
*
57-
* List complete node information:
58-
*
59-
* $ curl http://localhost:7777/__admin/load-nodes/
60-
*
61-
*
62-
* Show service stats and metrics
63-
*
64-
* $ curl http://localhost:8888/__stats/instance
65-
* </code>
66-
* </pre>
8+
* Created by rick on 6/18/16.
679
*/
68-
@Named("TodoService")
69-
@RequestMapping("/todo-service")
70-
public class TodoService {
71-
72-
73-
private final Map<String, Todo> todoMap = new TreeMap<>();
74-
75-
private final ServiceManagementBundle mgmt;
76-
77-
public TodoService(ServiceManagementBundle mgmt) {
78-
79-
this.mgmt = mgmt;
80-
81-
/** Send stat count i.am.alive every three seconds. */
82-
mgmt.reactor().addRepeatingTask(Duration.ofSeconds(3),
83-
() -> mgmt.increment("i.am.alive"));
84-
85-
}
86-
87-
88-
@POST(value = "/todo")
89-
public void add(final Callback<Boolean> callback, final Todo todo) {
90-
91-
/** Send KPI add called every time the add method gets called. */
92-
mgmt.increment("add.called");
93-
todoMap.put(todo.getId(), todo);
94-
callback.accept(true);
95-
}
96-
97-
98-
@DELETE(value = "/todo")
99-
public void remove(final Callback<Boolean> callback, final @RequestParam("id") String id) {
100-
101-
/** Send KPI add.removed every time the remove method gets called. */
102-
mgmt.increment("remove.called");
103-
Todo remove = todoMap.remove(id);
104-
callback.accept(remove != null);
105-
106-
}
107-
108-
109-
@GET(value = "/todo", method = RequestMethod.GET)
110-
public void list(final Callback<ArrayList<Todo>> callback) {
111-
112-
/** Send KPI add.list every time the list method gets called. */
113-
mgmt.increment("list.called");
114-
115-
callback.accept(new ArrayList<>(todoMap.values()));
116-
}
10+
public interface TodoService {
11+
Promise<Boolean> addTodo(Todo todo);
11712

13+
Promise<Boolean> removeTodo(String id);
11814

15+
Promise<ArrayList<Todo>> listTodos();
11916
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package com.mammatustech.todo;
2+
3+
import io.advantageous.qbit.admin.ServiceManagementBundle;
4+
import io.advantageous.qbit.annotation.RequestMapping;
5+
import io.advantageous.qbit.annotation.RequestMethod;
6+
import io.advantageous.qbit.annotation.RequestParam;
7+
import io.advantageous.qbit.annotation.http.DELETE;
8+
import io.advantageous.qbit.annotation.http.GET;
9+
import io.advantageous.qbit.annotation.http.POST;
10+
import io.advantageous.reakt.promise.Promise;
11+
12+
import java.time.Duration;
13+
import java.util.ArrayList;
14+
import java.util.Map;
15+
import java.util.TreeMap;
16+
17+
import static io.advantageous.reakt.promise.Promises.invokablePromise;
18+
19+
20+
/**
21+
* Default port for admin is 7777.
22+
* Default port for main endpoint is 8888.
23+
* <p>
24+
* <pre>
25+
* <code>
26+
*
27+
* Access the service:
28+
*
29+
* $ curl http://localhost:8888/v1/...
30+
*
31+
*
32+
* To see swagger file for this service:
33+
*
34+
* $ curl http://localhost:7777/__admin/meta/
35+
*
36+
* To see health for this service:
37+
*
38+
* $ curl http://localhost:8888/__health -v
39+
* Returns "ok" if all registered health systems are healthy.
40+
*
41+
* OR if same port endpoint health is disabled then:
42+
*
43+
* $ curl http://localhost:7777/__admin/ok -v
44+
* Returns "true" if all registered health systems are healthy.
45+
*
46+
*
47+
* A node is a service, service bundle, queue, or server endpoint that is being monitored.
48+
*
49+
* List all service nodes or endpoints
50+
*
51+
* $ curl http://localhost:7777/__admin/all-nodes/
52+
*
53+
*
54+
* List healthy nodes by name:
55+
*
56+
* $ curl http://localhost:7777/__admin/healthy-nodes/
57+
*
58+
* List complete node information:
59+
*
60+
* $ curl http://localhost:7777/__admin/load-nodes/
61+
*
62+
*
63+
* Show service stats and metrics
64+
*
65+
* $ curl http://localhost:8888/__stats/instance
66+
* </code>
67+
* </pre>
68+
*/
69+
@RequestMapping("/todo-service")
70+
public class TodoServiceImpl implements TodoService {
71+
72+
73+
private final Map<String, Todo> todoMap = new TreeMap<>();
74+
75+
private final ServiceManagementBundle mgmt;
76+
77+
public TodoServiceImpl(ServiceManagementBundle mgmt) {
78+
this.mgmt = mgmt;
79+
/** Send stat count i.am.alive every three seconds. */
80+
mgmt.reactor().addRepeatingTask(Duration.ofSeconds(3),
81+
() -> mgmt.increment("i.am.alive"));
82+
83+
}
84+
85+
86+
@Override
87+
@POST(value = "/todo")
88+
public Promise<Boolean> addTodo(final Todo todo) {
89+
return invokablePromise(promise -> {
90+
/** Send KPI addTodo called every time the addTodo method gets called. */
91+
mgmt.increment("addTodo.called");
92+
todoMap.put(todo.getId(), todo);
93+
promise.accept(true);
94+
});
95+
}
96+
97+
98+
@Override
99+
@DELETE(value = "/todo")
100+
public final Promise<Boolean> removeTodo(final @RequestParam("id") String id) {
101+
return invokablePromise(promise -> {
102+
/** Send KPI addTodo.removed every time the removeTodo method gets called. */
103+
mgmt.increment("removeTodo.called");
104+
todoMap.remove(id);
105+
promise.accept(true);
106+
});
107+
}
108+
109+
110+
@Override
111+
@GET(value = "/todo", method = RequestMethod.GET)
112+
public final Promise<ArrayList<Todo>> listTodos() {
113+
return invokablePromise(promise -> {
114+
/** Send KPI addTodo.listTodos every time the listTodos method gets called. */
115+
mgmt.increment("listTodos.called");
116+
promise.accept(new ArrayList<>(todoMap.values()));
117+
});
118+
}
119+
120+
121+
}

todo-statsd/src/main/java/com/mammatustech/todo/TodoServiceMain.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ public static void main(final String... args) throws Exception {
2626

2727
/** Create the management bundle for this service. */
2828
final ServiceManagementBundle serviceManagementBundle =
29-
serviceManagementBundleBuilder().setServiceName("TodoService")
29+
serviceManagementBundleBuilder().setServiceName("TodoServiceImpl")
3030
.setManagedServiceBuilder(managedServiceBuilder).build();
3131

32-
final TodoService todoService = new TodoService(serviceManagementBundle);
32+
final TodoService todoService = new TodoServiceImpl(serviceManagementBundle);
3333

3434
/* Start the service. */
3535
managedServiceBuilder
36-
//Register TodoService
36+
//Register TodoServiceImpl
3737
.addEndpointServiceWithServiceManagmentBundle(todoService, serviceManagementBundle)
3838
//Build and start the server.
3939
.startApplication();
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.mammatustech.todo;
2+
3+
import io.advantageous.qbit.admin.ManagedServiceBuilder;
4+
import io.advantageous.qbit.admin.ServiceManagementBundle;
5+
import io.advantageous.qbit.queue.QueueCallBackHandler;
6+
import io.advantageous.qbit.service.ServiceBuilder;
7+
import org.junit.Test;
8+
9+
import java.util.concurrent.TimeUnit;
10+
11+
import static io.advantageous.qbit.admin.ManagedServiceBuilder.managedServiceBuilder;
12+
import static io.advantageous.qbit.admin.ServiceManagementBundleBuilder.serviceManagementBundleBuilder;
13+
import static junit.framework.TestCase.assertFalse;
14+
import static org.junit.Assert.assertTrue;
15+
16+
public class TodoServiceImplTest {
17+
@Test
18+
public void test() throws Exception {
19+
final TodoService todoService = createTodoService();
20+
21+
final Todo rick = new Todo("foo", "rick", 1L);
22+
23+
//Add Rick
24+
assertTrue(todoService
25+
.addTodo(rick)
26+
.invokeAsBlockingPromise().get());
27+
28+
29+
//Add Diana
30+
assertTrue(todoService
31+
.addTodo(new Todo("bar", "diana", 1L))
32+
.invokeAsBlockingPromise().get());
33+
34+
//Remove Rick
35+
assertTrue(todoService.removeTodo(rick.getId())
36+
.invokeAsBlockingPromise().get());
37+
38+
//Make sure Diana is in the listTodos
39+
assertTrue(todoService.listTodos()
40+
.invokeAsBlockingPromise()
41+
.get()
42+
.stream()
43+
.filter(
44+
todo -> todo.getDescription().equals("diana")
45+
46+
)
47+
.findFirst()
48+
.isPresent()
49+
);
50+
51+
52+
//Make sure Rick is not in the listTodos
53+
assertFalse(todoService.listTodos()
54+
.invokeAsBlockingPromise()
55+
.get()
56+
.stream()
57+
.filter(
58+
todo -> todo.getDescription().equals("rick")
59+
60+
)
61+
.findFirst()
62+
.isPresent()
63+
);
64+
65+
}
66+
67+
private TodoService createTodoService() {
68+
/* Create the ManagedServiceBuilder which manages a clean shutdown, health, stats, etc. */
69+
final ManagedServiceBuilder managedServiceBuilder = managedServiceBuilder(); //Defaults to 8080 or environment variable PORT
70+
71+
72+
/** Create the management bundle for this service. */
73+
final ServiceManagementBundle serviceManagementBundle =
74+
serviceManagementBundleBuilder().setServiceName("TodoService")
75+
.setManagedServiceBuilder(managedServiceBuilder).build();
76+
77+
final TodoService todoServiceImpl = new TodoServiceImpl(serviceManagementBundle);
78+
79+
80+
return ServiceBuilder.serviceBuilder().setServiceObject(todoServiceImpl).addQueueCallbackHandler(new QueueCallBackHandler() {
81+
@Override
82+
public void queueProcess() {
83+
serviceManagementBundle.process();
84+
}
85+
})
86+
.buildAndStartAll()
87+
.createProxyWithAutoFlush(TodoService.class, 50, TimeUnit.MILLISECONDS);
88+
89+
}
90+
}

0 commit comments

Comments
 (0)