Search in sources :

Example 6 with Microservices

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

the class TestStreamingService method test_just_one_message.

@Test
public void test_just_one_message() 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 justOne = Messages.builder().request(QuoteService.NAME, "justOne").build();
    sub1.set(service.listen(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) 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 7 with Microservices

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

the class TestStreamingService method test_remote_node_died.

@Test
public void test_remote_node_died() 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 justOne = Messages.builder().request(QuoteService.NAME, "justOne").build();
    sub1.set(service.listen(justOne).subscribe(onNext -> {
        System.out.println(onNext);
    }));
    gateway.cluster().listenMembership().filter(predicate -> predicate.isRemoved()).subscribe(onNext -> {
        latch1.countDown();
    });
    node.cluster().shutdown();
    latch1.await(20, TimeUnit.SECONDS);
    Thread.sleep(100);
    assertTrue(latch1.getCount() == 0);
    assertTrue(sub1.get().isUnsubscribed());
    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 8 with Microservices

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

the class ServiceTagsExample method main.

public static void main(String[] args) {
    Microservices gateway = Microservices.builder().build();
    Microservices services1 = Microservices.builder().seeds(gateway.cluster().address()).services().service(new GreetingServiceImplA()).tag("Weight", "0.3").add().build().build();
    Microservices services2 = Microservices.builder().seeds(gateway.cluster().address()).services().service(new GreetingServiceImplB()).tag("Weight", "0.7").add().build().build();
    CanaryService service = gateway.proxy().router(CanaryTestingRouter.class).api(CanaryService.class).create();
    for (int i = 0; i < 10; i++) {
        service.greeting("joe").whenComplete((success, error) -> {
            success.startsWith("B");
            System.out.println(success);
        });
    }
}
Also used : Microservices(io.scalecube.services.Microservices)

Example 9 with Microservices

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

the class StockMain method main.

/**
 * Example that shows the basic usage of streaming quotes from a remove microservice. the example creates 2 cluster
 * nodes that communicates over the network: 1. gateway node cluster member that acts as seed and gateway. 2. service
 * nodes that provisions the SimpleQuoteService at the cluster.
 *
 * <pre>
 *
 * {@code
 *   Request:
 *      QuoteService -> gateway (--- network --->) node1 -> SimpleQuoteService
 *
 *   Stream:
 *         onNext(quote) <- gateway (<--- network ---) node1 <- onNext(quote)
 * }
 * </pre>
 * the SimpleQuoteService generates random stock price every 1 second. each time subscriber subscribe he will get all
 * the history plus the next coming update of stock price.
 * the example for simplicity using both nodes on the same jvm for ease of debugging.
 * @param args none.
 */
public static void main(String[] args) {
    // seed node as gateway
    Microservices gateway = Microservices.builder().build();
    // cluster member node with SimpleQuoteService
    // join gateway cluster.
    Microservices.builder().seeds(gateway.cluster().address()).services(new SimpleQuoteService()).build();
    // create stock Quote service from gateway.
    QuoteService qouteService = gateway.proxy().api(// Quote service interface.
    QuoteService.class).create();
    // subscribe on quotes changes
    qouteService.quotes("ORCL").subscribe(onNext -> {
        System.out.println(onNext);
    });
}
Also used : Microservices(io.scalecube.services.Microservices)

Example 10 with Microservices

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

the class TestStreamingService method test_unknown_method.

@Test
public void test_unknown_method() throws InterruptedException {
    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(1);
    Message scheduled = Messages.builder().request(QuoteService.NAME, "unknonwn").build();
    try {
        service.listen(scheduled);
    } catch (Exception ex) {
        if (ex.getMessage().contains("No reachable member with such service: unknonwn")) {
            latch1.countDown();
        }
    }
    latch1.await(2, TimeUnit.SECONDS);
    assertTrue(latch1.getCount() == 0);
    node.shutdown();
    gateway.shutdown();
}
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