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) {
}
}
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();
}
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();
}
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);
}
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);
}
Aggregations