Search in sources :

Example 6 with Message

use of io.scalecube.transport.Message in project scalecube by scalecube.

the class ServiceHeadersTest method test_ServiceHeaders.

@Test
public void test_ServiceHeaders() {
    Message request = Message.builder().header(ServiceHeaders.METHOD, "m").header(ServiceHeaders.SERVICE_REQUEST, "req").header(ServiceHeaders.SERVICE_RESPONSE, "res").build();
    assertEquals(ServiceHeaders.serviceMethod(request), "m");
    assertEquals(ServiceHeaders.serviceRequest(request), "req");
    assertEquals(ServiceHeaders.serviceResponse(request), "res");
    try {
        Constructor<ServiceHeaders> constructor = (Constructor<ServiceHeaders>) ServiceHeaders.class.getDeclaredConstructors()[0];
        constructor.setAccessible(true);
        ServiceHeaders foo = constructor.newInstance();
        assertTrue(foo != null);
    } catch (Exception e) {
    }
}
Also used : Message(io.scalecube.transport.Message) Constructor(java.lang.reflect.Constructor) Test(org.junit.Test) BaseTest(io.scalecube.testlib.BaseTest)

Example 7 with Message

use of io.scalecube.transport.Message in project scalecube by scalecube.

the class ServiceTest method test_remote_async_greeting_return_Message.

@Test
public void test_remote_async_greeting_return_Message() throws InterruptedException, ExecutionException {
    // Create microservices cluster.
    Microservices provider = Microservices.builder().port(port.incrementAndGet()).services(new GreetingServiceImpl()).build();
    // Create microservices cluster.
    Microservices consumer = Microservices.builder().port(port.incrementAndGet()).seeds(provider.cluster().address()).build();
    // get a proxy to the service api.
    GreetingService service = createProxy(consumer);
    // call the service.
    CompletableFuture<Message> future = service.greetingMessage(Message.builder().data("joe").build());
    CountDownLatch timeLatch = new CountDownLatch(1);
    future.whenComplete((result, ex) -> {
        if (ex == null) {
            // print the greeting.
            System.out.println("10. remote_async_greeting_return_Message :" + result.data());
            // print the greeting.
            assertTrue(result.data().equals(" hello to: joe"));
        } else {
            // print the greeting.
            System.out.println("10 failed: " + ex);
            assertTrue(result.data().equals(" hello to: joe"));
        }
        timeLatch.countDown();
    });
    await(timeLatch, 20, TimeUnit.SECONDS);
    assertTrue(timeLatch.getCount() == 0);
    consumer.shutdown().get();
    provider.shutdown().get();
}
Also used : Message(io.scalecube.transport.Message) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test) BaseTest(io.scalecube.testlib.BaseTest)

Example 8 with Message

use of io.scalecube.transport.Message in project scalecube by scalecube.

the class ServiceTest method test_local_async_greeting_return_Message.

@Test
public void test_local_async_greeting_return_Message() throws InterruptedException, ExecutionException {
    // Create microservices cluster.
    Microservices microservices = Microservices.builder().port(port.incrementAndGet()).services(new GreetingServiceImpl()).build();
    // get a proxy to the service api.
    GreetingService service = createProxy(microservices);
    // call the service.
    CompletableFuture<Message> future = service.greetingMessage(Message.builder().data("joe").build());
    CountDownLatch timeLatch = new CountDownLatch(1);
    future.whenComplete((result, ex) -> {
        if (ex == null) {
            assertTrue(result.data().equals(" hello to: joe"));
            // print the greeting.
            System.out.println("9. local_async_greeting_return_Message :" + result.data());
        } else {
            // print the greeting.
            System.out.println(ex);
        }
        timeLatch.countDown();
    });
    await(timeLatch, 1, TimeUnit.SECONDS);
    microservices.shutdown().get();
}
Also used : Message(io.scalecube.transport.Message) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test) BaseTest(io.scalecube.testlib.BaseTest)

Example 9 with Message

use of io.scalecube.transport.Message in project scalecube by scalecube.

the class ClusterMetadataExample method main.

/**
 * Main method.
 */
public static void main(String[] args) throws Exception {
    // Start seed cluster member Alice
    Cluster alice = Cluster.joinAwait();
    // Join Joe to cluster with metadata
    Map<String, String> metadata = ImmutableMap.of("name", "Joe");
    Cluster joe = Cluster.joinAwait(metadata, alice.address());
    // Subscribe Joe to listen for incoming messages and print them to system out
    joe.listen().map(Message::data).subscribe(System.out::println);
    // Scan the list of members in the cluster and find Joe there
    Optional<Member> joeMemberOptional = alice.otherMembers().stream().filter(member -> "Joe".equals(member.metadata().get("name"))).findAny();
    // Send hello to Joe
    if (joeMemberOptional.isPresent()) {
        alice.send(joeMemberOptional.get(), Message.fromData("Hello Joe"));
    }
    TimeUnit.SECONDS.sleep(3);
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) Cluster(io.scalecube.cluster.Cluster) ImmutableMap(com.google.common.collect.ImmutableMap) Member(io.scalecube.cluster.Member) Map(java.util.Map) Optional(java.util.Optional) Message(io.scalecube.transport.Message) Cluster(io.scalecube.cluster.Cluster) Member(io.scalecube.cluster.Member)

Example 10 with Message

use of io.scalecube.transport.Message in project scalecube by scalecube.

the class MessagingExample method main.

/**
 * Main method.
 */
public static void main(String[] args) throws Exception {
    // Start cluster node Alice to listen and respond for incoming greeting messages
    Cluster alice = Cluster.joinAwait();
    alice.listen().subscribe(msg -> {
        System.out.println("Alice received: " + msg.data());
        alice.send(msg.sender(), Message.fromData("Greetings from Alice"));
    });
    // Join cluster node Bob to cluster with Alice, listen and respond for incoming greeting messages
    Cluster bob = Cluster.joinAwait(alice.address());
    bob.listen().subscribe(msg -> {
        System.out.println("Bob received: " + msg.data());
        bob.send(msg.sender(), Message.fromData("Greetings from Bob"));
    });
    // Join cluster node Carol to cluster with Alice and Bob
    Cluster carol = Cluster.joinAwait(alice.address(), bob.address());
    // Subscribe Carol to listen for incoming messages and print them to system out
    carol.listen().map(msg -> "Carol received: " + msg.data()).subscribe(System.out::println);
    // Send from Carol greeting message to all other cluster members (which is Alice and Bob)
    Message greetingMsg = Message.fromData("Greetings from Carol");
    carol.otherMembers().forEach(member -> carol.send(member, greetingMsg));
    // Avoid exit main thread immediately ]:->
    Thread.sleep(1000);
}
Also used : Cluster(io.scalecube.cluster.Cluster) Message(io.scalecube.transport.Message) Message(io.scalecube.transport.Message) Cluster(io.scalecube.cluster.Cluster)

Aggregations

Message (io.scalecube.transport.Message)51 BaseTest (io.scalecube.testlib.BaseTest)31 Test (org.junit.Test)31 CountDownLatch (java.util.concurrent.CountDownLatch)28 Observable (rx.Observable)10 Member (io.scalecube.cluster.Member)8 Microservices (io.scalecube.services.Microservices)6 TimeUnit (java.util.concurrent.TimeUnit)6 Logger (org.slf4j.Logger)6 LoggerFactory (org.slf4j.LoggerFactory)6 ServiceCall (io.scalecube.services.ServiceCall)5 Collections (java.util.Collections)5 Executors (java.util.concurrent.Executors)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 Schedulers (rx.schedulers.Schedulers)5 List (java.util.List)4 Subscription (rx.Subscription)4 PublishSubject (rx.subjects.PublishSubject)4 Subject (rx.subjects.Subject)4 MetricRegistry (com.codahale.metrics.MetricRegistry)3