Search in sources :

Example 1 with RestService

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();
    }
}
Also used : Atomix(io.atomix.core.Atomix) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) ManagedRestService(io.atomix.rest.ManagedRestService) RestService(io.atomix.rest.RestService) After(org.junit.After)

Example 2 with RestService

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);
    }
}
Also used : Node(io.atomix.cluster.Node) RequestSpecBuilder(io.restassured.builder.RequestSpecBuilder) Address(io.atomix.utils.net.Address) ManagedRestService(io.atomix.rest.ManagedRestService) CompletableFuture(java.util.concurrent.CompletableFuture) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ArrayList(java.util.ArrayList) Atomix(io.atomix.core.Atomix) ServerSocket(java.net.ServerSocket) RequestSpecification(io.restassured.specification.RequestSpecification) After(org.junit.After) JsonNode(com.fasterxml.jackson.databind.JsonNode) Path(java.nio.file.Path) RestService(io.atomix.rest.RestService) RequestLoggingFilter(io.restassured.filter.log.RequestLoggingFilter) Before(org.junit.Before) SimpleFileVisitor(java.nio.file.SimpleFileVisitor) PrimaryBackupPartitionGroup(io.atomix.protocols.backup.partition.PrimaryBackupPartitionGroup) Files(java.nio.file.Files) ResponseLoggingFilter(io.restassured.filter.log.ResponseLoggingFilter) ContentType(io.restassured.http.ContentType) Test(org.junit.Test) IOException(java.io.IOException) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) TimeUnit(java.util.concurrent.TimeUnit) FileVisitResult(java.nio.file.FileVisitResult) List(java.util.List) Paths(java.nio.file.Paths) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory) BootstrapDiscoveryProvider(io.atomix.cluster.discovery.BootstrapDiscoveryProvider) Matchers.equalTo(org.hamcrest.Matchers.equalTo) RestAssured.given(io.restassured.RestAssured.given) Atomix(io.atomix.core.Atomix) ArrayList(java.util.ArrayList) RequestSpecification(io.restassured.specification.RequestSpecification) RequestSpecBuilder(io.restassured.builder.RequestSpecBuilder) ResponseLoggingFilter(io.restassured.filter.log.ResponseLoggingFilter) CompletableFuture(java.util.concurrent.CompletableFuture) RequestLoggingFilter(io.restassured.filter.log.RequestLoggingFilter) ManagedRestService(io.atomix.rest.ManagedRestService) Before(org.junit.Before)

Example 3 with RestService

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;
}
Also used : ScanResult(io.github.classgraph.ScanResult) CompletableFuture(java.util.concurrent.CompletableFuture) VertxRequestHandler(org.jboss.resteasy.plugins.server.vertx.VertxRequestHandler) VertxResteasyDeployment(org.jboss.resteasy.plugins.server.vertx.VertxResteasyDeployment) ClassGraph(io.github.classgraph.ClassGraph) ManagedRestService(io.atomix.rest.ManagedRestService) RestService(io.atomix.rest.RestService)

Aggregations

ManagedRestService (io.atomix.rest.ManagedRestService)3 RestService (io.atomix.rest.RestService)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 Atomix (io.atomix.core.Atomix)2 ArrayList (java.util.ArrayList)2 After (org.junit.After)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 JsonNodeFactory (com.fasterxml.jackson.databind.node.JsonNodeFactory)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 Node (io.atomix.cluster.Node)1 BootstrapDiscoveryProvider (io.atomix.cluster.discovery.BootstrapDiscoveryProvider)1 PrimaryBackupPartitionGroup (io.atomix.protocols.backup.partition.PrimaryBackupPartitionGroup)1 Address (io.atomix.utils.net.Address)1 ClassGraph (io.github.classgraph.ClassGraph)1 ScanResult (io.github.classgraph.ScanResult)1 RestAssured.given (io.restassured.RestAssured.given)1 RequestSpecBuilder (io.restassured.builder.RequestSpecBuilder)1 RequestLoggingFilter (io.restassured.filter.log.RequestLoggingFilter)1 ResponseLoggingFilter (io.restassured.filter.log.ResponseLoggingFilter)1 ContentType (io.restassured.http.ContentType)1