Search in sources :

Example 1 with Microservices

use of io.scalecube.services.Microservices in project scalecube by scalecube.

the class MicroservicesExample method onConsumer.

private static void onConsumer(Address providerAddress) throws Exception {
    // Create microservice consumer
    Microservices consumer = Microservices.builder().seeds(providerAddress).build();
    // Get a proxy to the service API
    GreetingService greetingService = consumer.proxy().api(GreetingService.class).create();
    // Call service (successful case)
    CompletableFuture<String> future = greetingService.greeting("Joe");
    future.whenComplete((result, exception) -> System.out.println("Consumer: 'greeting' <- " + (exception == null ? result : exception)));
    // Call service (error case)
    CompletableFuture<String> futureError = greetingService.greetingException("Joe");
    futureError.whenComplete((result, exception) -> System.out.println("Consumer: 'greetingException' <- " + (exception == null ? result : exception)));
}
Also used : Microservices(io.scalecube.services.Microservices)

Example 2 with Microservices

use of io.scalecube.services.Microservices in project scalecube by scalecube.

the class TestStreamingService method test_call_quotes_snapshot.

@Test
public void test_call_quotes_snapshot() throws InterruptedException {
    int batchSize = 1_000_000;
    Microservices gateway = Microservices.builder().build();
    Microservices node = Microservices.builder().seeds(gateway.cluster().address()).services(new SimpleQuoteService()).build();
    ServiceCall service = gateway.dispatcher().create();
    CountDownLatch latch1 = new CountDownLatch(batchSize);
    Subscription sub1 = service.listen(Messages.builder().request(QuoteService.NAME, "snapshoot").data(batchSize).build()).subscribeOn(Schedulers.from(Executors.newCachedThreadPool())).serialize().subscribe(onNext -> latch1.countDown());
    long start = System.currentTimeMillis();
    latch1.await(batchSize / 40_000, TimeUnit.SECONDS);
    long end = (System.currentTimeMillis() - start);
    System.out.println("TIME IS UP! - recived batch (size: " + (batchSize - latch1.getCount()) + "/" + batchSize + ") in " + (System.currentTimeMillis() - start) + "ms: " + "rate of :" + batchSize / (end / 1000) + " events/sec ");
    sub1.unsubscribe();
    gateway.shutdown();
    node.shutdown();
}
Also used : ServiceCall(io.scalecube.services.ServiceCall) CountDownLatch(java.util.concurrent.CountDownLatch) Subscription(rx.Subscription) Microservices(io.scalecube.services.Microservices) Test(org.junit.Test) BaseTest(io.scalecube.testlib.BaseTest)

Example 3 with Microservices

use of io.scalecube.services.Microservices in project scalecube by scalecube.

the class TestStreamingService method test_just_once.

@Test
public void test_just_once() throws InterruptedException {
    int batchSize = 1;
    Microservices gateway = Microservices.builder().build();
    Microservices node = Microservices.builder().seeds(gateway.cluster().address()).services(new SimpleQuoteService()).build();
    QuoteService service = gateway.proxy().api(QuoteService.class).create();
    final CountDownLatch latch1 = new CountDownLatch(batchSize);
    AtomicReference<Subscription> sub1 = new AtomicReference<Subscription>(null);
    sub1.set(service.justOne().serialize().subscribe(onNext -> {
        sub1.get().unsubscribe();
        latch1.countDown();
    }));
    latch1.await(2, TimeUnit.SECONDS);
    assertTrue(latch1.getCount() == 0);
    assertTrue(sub1.get().isUnsubscribed());
    gateway.shutdown();
    node.shutdown();
}
Also used : MetricRegistry(com.codahale.metrics.MetricRegistry) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) AtomicReference(java.util.concurrent.atomic.AtomicReference) Executors(java.util.concurrent.Executors) Observable(rx.Observable) TimeUnit(java.util.concurrent.TimeUnit) Messages(io.scalecube.services.Messages) CountDownLatch(java.util.concurrent.CountDownLatch) Microservices(io.scalecube.services.Microservices) ServiceCall(io.scalecube.services.ServiceCall) Schedulers(rx.schedulers.Schedulers) Message(io.scalecube.transport.Message) BaseTest(io.scalecube.testlib.BaseTest) Subscription(rx.Subscription) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Subscription(rx.Subscription) Microservices(io.scalecube.services.Microservices) Test(org.junit.Test) BaseTest(io.scalecube.testlib.BaseTest)

Example 4 with Microservices

use of io.scalecube.services.Microservices in project scalecube by scalecube.

the class TestStreamingService method test_scheduled_messages.

@Test
public void test_scheduled_messages() throws InterruptedException {
    int batchSize = 1;
    Microservices gateway = Microservices.builder().build();
    Microservices node = Microservices.builder().seeds(gateway.cluster().address()).services(new SimpleQuoteService()).build();
    ServiceCall service = gateway.dispatcher().create();
    final CountDownLatch latch1 = new CountDownLatch(batchSize);
    AtomicReference<Subscription> sub1 = new AtomicReference<Subscription>(null);
    Message scheduled = Messages.builder().request(QuoteService.NAME, "scheduled").data(1000).build();
    sub1.set(service.listen(scheduled).serialize().subscribe(onNext -> {
        sub1.get().isUnsubscribed();
        latch1.countDown();
    }));
    latch1.await(2, TimeUnit.SECONDS);
    assertTrue(latch1.getCount() == 0);
    node.shutdown();
    gateway.shutdown();
}
Also used : MetricRegistry(com.codahale.metrics.MetricRegistry) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) AtomicReference(java.util.concurrent.atomic.AtomicReference) Executors(java.util.concurrent.Executors) Observable(rx.Observable) TimeUnit(java.util.concurrent.TimeUnit) Messages(io.scalecube.services.Messages) CountDownLatch(java.util.concurrent.CountDownLatch) Microservices(io.scalecube.services.Microservices) ServiceCall(io.scalecube.services.ServiceCall) Schedulers(rx.schedulers.Schedulers) Message(io.scalecube.transport.Message) BaseTest(io.scalecube.testlib.BaseTest) Subscription(rx.Subscription) ServiceCall(io.scalecube.services.ServiceCall) Message(io.scalecube.transport.Message) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Subscription(rx.Subscription) Microservices(io.scalecube.services.Microservices) Test(org.junit.Test) BaseTest(io.scalecube.testlib.BaseTest)

Example 5 with Microservices

use of io.scalecube.services.Microservices in project scalecube by scalecube.

the class SimpleStressTest method test_naive_dispatcher_stress.

@Test
public void test_naive_dispatcher_stress() throws InterruptedException, ExecutionException {
    // Create microservices cluster member.
    Microservices provider = Microservices.builder().port(port.incrementAndGet()).services(new GreetingServiceImpl()).metrics(registry).build();
    // Create microservices cluster member.
    Microservices consumer = Microservices.builder().port(port.incrementAndGet()).seeds(provider.cluster().address()).metrics(registry).build();
    reporter.start(10, TimeUnit.SECONDS);
    ServiceCall greetings = consumer.dispatcher().timeout(Duration.ofSeconds(30)).create();
    // Measure
    CountDownLatch countLatch = new CountDownLatch(count);
    long startTime = System.currentTimeMillis();
    for (int i = 0; i < count; i++) {
        CompletableFuture<Message> future = greetings.invoke(Messages.builder().request(GreetingService.class, "greetingMessage").data("1").build());
        future.whenComplete((success, error) -> {
            countLatch.countDown();
        });
    }
    System.out.println("Finished sending " + count + " messages in " + (System.currentTimeMillis() - startTime));
    countLatch.await(60, TimeUnit.SECONDS);
    reporter.stop();
    System.out.println("Finished receiving " + count + " messages in " + (System.currentTimeMillis() - startTime));
    assertTrue(countLatch.getCount() == 0);
    provider.shutdown().get();
    consumer.shutdown().get();
}
Also used : ServiceCall(io.scalecube.services.ServiceCall) Message(io.scalecube.transport.Message) CountDownLatch(java.util.concurrent.CountDownLatch) Microservices(io.scalecube.services.Microservices) Test(org.junit.Test) BaseTest(io.scalecube.testlib.BaseTest)

Aggregations

Microservices (io.scalecube.services.Microservices)14 BaseTest (io.scalecube.testlib.BaseTest)11 CountDownLatch (java.util.concurrent.CountDownLatch)11 Test (org.junit.Test)11 Subscription (rx.Subscription)8 ServiceCall (io.scalecube.services.ServiceCall)7 Message (io.scalecube.transport.Message)7 MetricRegistry (com.codahale.metrics.MetricRegistry)4 Messages (io.scalecube.services.Messages)4 Executors (java.util.concurrent.Executors)4 TimeUnit (java.util.concurrent.TimeUnit)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 Assert.assertTrue (org.junit.Assert.assertTrue)4 Observable (rx.Observable)4 Schedulers (rx.schedulers.Schedulers)4