use of io.atomix.rest.RestService in project atomix by atomix.
the class VertxRestServiceTest method afterTest.
@After
public void afterTest() throws Exception {
try {
List<CompletableFuture<Void>> serviceFutures = new ArrayList<>(3);
for (RestService service : services) {
serviceFutures.add(((ManagedRestService) service).stop());
}
CompletableFuture.allOf(serviceFutures.toArray(new CompletableFuture[serviceFutures.size()])).get(30, TimeUnit.SECONDS);
} finally {
List<CompletableFuture<Void>> instanceFutures = new ArrayList<>(3);
for (Atomix instance : instances) {
instanceFutures.add(instance.stop());
}
CompletableFuture.allOf(instanceFutures.toArray(new CompletableFuture[instanceFutures.size()])).get(30, TimeUnit.SECONDS);
deleteData();
}
}
use of io.atomix.rest.RestService in project atomix by atomix.
the class VertxRestServiceTest method beforeTest.
@Before
public void beforeTest() throws Exception {
deleteData();
List<CompletableFuture<Atomix>> instanceFutures = new ArrayList<>(3);
instances = new ArrayList<>(3);
for (int i = 1; i <= 3; i++) {
Atomix atomix = buildAtomix(i);
instanceFutures.add(atomix.start().thenApply(v -> atomix));
instances.add(atomix);
}
CompletableFuture.allOf(instanceFutures.toArray(new CompletableFuture[instanceFutures.size()])).get(30, TimeUnit.SECONDS);
List<CompletableFuture<RestService>> serviceFutures = new ArrayList<>(3);
services = new ArrayList<>(3);
for (int i = 0; i < 3; i++) {
ManagedRestService restService = new VertxRestService(instances.get(i), Address.from("localhost", findAvailablePort(BASE_PORT)));
serviceFutures.add(restService.start());
services.add(restService);
}
CompletableFuture.allOf(serviceFutures.toArray(new CompletableFuture[serviceFutures.size()])).get(30, TimeUnit.SECONDS);
specs = new ArrayList<>(3);
for (int i = 0; i < 3; i++) {
RequestSpecification spec = new RequestSpecBuilder().setContentType(ContentType.TEXT).setBaseUri(String.format("http://%s/v1/", services.get(i).address().toString())).addFilter(new ResponseLoggingFilter()).addFilter(new RequestLoggingFilter()).build();
specs.add(spec);
}
}
use of io.atomix.rest.RestService in project atomix by atomix.
the class VertxRestService method start.
@Override
public CompletableFuture<RestService> start() {
server = vertx.createHttpServer();
deployment = new VertxResteasyDeployment();
deployment.start();
deployment.getDispatcher().getDefaultContextObjects().put(ClusterMembershipService.class, atomix.getMembershipService());
deployment.getDispatcher().getDefaultContextObjects().put(ClusterCommunicationService.class, atomix.getCommunicationService());
deployment.getDispatcher().getDefaultContextObjects().put(ClusterEventService.class, atomix.getEventService());
deployment.getDispatcher().getDefaultContextObjects().put(PrimitiveFactory.class, atomix.getPrimitivesService());
deployment.getDispatcher().getDefaultContextObjects().put(PrimitivesService.class, atomix.getPrimitivesService());
deployment.getDispatcher().getDefaultContextObjects().put(EventManager.class, new EventManager());
deployment.getDispatcher().getDefaultContextObjects().put(AtomixRegistry.class, atomix.getRegistry());
final ClassLoader classLoader = atomix.getClass().getClassLoader();
final String[] whitelistPackages = StringUtils.split(System.getProperty("io.atomix.whitelistPackages"), ",");
final ClassGraph classGraph = whitelistPackages != null ? new ClassGraph().enableAnnotationInfo().whitelistPackages(whitelistPackages).addClassLoader(classLoader) : new ClassGraph().enableAnnotationInfo().addClassLoader(classLoader);
try (final ScanResult scanResult = classGraph.scan()) {
scanResult.getClassesWithAnnotation(AtomixResource.class.getName()).forEach(classInfo -> {
deployment.getRegistry().addPerInstanceResource(classInfo.loadClass(), "/v1");
});
}
deployment.getDispatcher().getProviderFactory().register(new JacksonProvider(createObjectMapper()));
server.requestHandler(new VertxRequestHandler(vertx, deployment));
CompletableFuture<RestService> future = new CompletableFuture<>();
server.listen(address.port(), address.address(true).getHostAddress(), result -> {
if (result.succeeded()) {
open.set(true);
LOGGER.info("Started");
future.complete(this);
} else {
future.completeExceptionally(result.cause());
}
});
return future;
}
Aggregations