use of io.scalecube.transport.Message in project scalecube by scalecube.
the class ServiceCallTest method test_local_greeting_request_timeout_expires.
@Test
public void test_local_greeting_request_timeout_expires() throws InterruptedException, ExecutionException {
// Create microservices instance.
Microservices node = Microservices.builder().port(port.incrementAndGet()).services(new GreetingServiceImpl()).build();
ServiceCall service = node.dispatcher().timeout(Duration.ofSeconds(1)).create();
// call the service.
CompletableFuture<Message> future = service.invoke(Messages.builder().request(SERVICE_NAME, "greetingRequestTimeout").data(new GreetingRequest("joe", Duration.ofSeconds(2))).build(), Duration.ofMillis(1));
CountDownLatch timeLatch = new CountDownLatch(1);
future.whenComplete((success, error) -> {
if (error != null) {
// print the greeting.
System.out.println("7. local_greeting_request_timeout_expires : " + error);
assertTrue(error instanceof TimeoutException);
} else {
fail();
}
timeLatch.countDown();
});
await(timeLatch, 1, TimeUnit.SECONDS);
node.shutdown().get();
}
use of io.scalecube.transport.Message in project scalecube by scalecube.
the class ServiceTest method test_round_robin_selection_logic.
@Test
public void test_round_robin_selection_logic() throws InterruptedException, ExecutionException {
Microservices gateway = createSeed();
// Create microservices instance cluster.
Microservices provider1 = Microservices.builder().seeds(gateway.cluster().address()).port(port.incrementAndGet()).services(new GreetingServiceImpl()).build();
// Create microservices instance cluster.
Microservices provider2 = Microservices.builder().seeds(gateway.cluster().address()).port(port.incrementAndGet()).services(new GreetingServiceImpl()).build();
GreetingService service = createProxy(gateway);
CompletableFuture<Message> result1 = service.greetingMessage(Message.builder().data("joe").build());
CompletableFuture<Message> result2 = service.greetingMessage(Message.builder().data("joe").build());
CompletableFuture<Void> combined = CompletableFuture.allOf(result1, result2);
CountDownLatch timeLatch = new CountDownLatch(1);
combined.whenComplete((v, x) -> {
try {
// print the greeting.
System.out.println("11. round_robin_selection_logic :" + result1.get());
System.out.println("11. round_robin_selection_logic :" + result2.get());
boolean success = !result1.get().sender().equals(result2.get().sender());
assertTrue(success);
} catch (Throwable e) {
assertTrue(false);
}
timeLatch.countDown();
});
await(timeLatch, 2, TimeUnit.SECONDS);
assertTrue(timeLatch.getCount() == 0);
provider2.shutdown().get();
provider1.shutdown().get();
gateway.shutdown().get();
}
use of io.scalecube.transport.Message in project scalecube by scalecube.
the class Messages method asResponse.
/**
* utility method to build service response message.
*
* @param data to be use for the response.
* @param correlationId of a the given request.
* @param memberId that created the response.
* @return response message or response error message in case data is exception.
*/
public static Message asResponse(Object data, String correlationId, String memberId) {
Builder builder = Message.builder().correlationId(correlationId).header("memberId", memberId);
if (data instanceof Message) {
Message msg = (Message) data;
builder = builder.data(msg.data());
} else {
builder = builder.data(data);
if (data instanceof Throwable) {
builder = builder.header(ServiceHeaders.EXCEPTION, "");
}
}
return builder.build();
}
use of io.scalecube.transport.Message in project scalecube by scalecube.
the class RemoteServiceInstance method listen.
@Override
public Observable<Message> listen(final Message request) {
final String cid = request.correlationId();
AtomicReference<Subscription> subscription = new AtomicReference<>();
Observable<Message> observer = transportObservable.doOnUnsubscribe(() -> {
Message unsubscribeRequest = Messages.asUnsubscribeRequest(cid);
LOGGER.info("sending remote unsubscribed event: {}", unsubscribeRequest);
subscription.get().unsubscribe();
sendRemote(unsubscribeRequest);
});
Subscription sub = observer.filter(message -> message.correlationId().equals(cid)).subscribe(onNext -> {
serviceResponses.onNext(onNext);
});
subscription.set(sub);
sendRemote(request);
return serviceResponses;
}
use of io.scalecube.transport.Message in project scalecube by scalecube.
the class DispatchingFuture method handleComputable.
private void handleComputable(CompletableFuture<?> result) {
result.whenComplete((success, error) -> {
Message futureMessage = null;
if (error == null) {
if (success instanceof Message) {
Message successMessage = (Message) success;
futureMessage = composeResponse(successMessage.data());
} else {
futureMessage = composeResponse(success);
}
} else {
completeExceptionally(error);
}
cluster.send(request.sender(), futureMessage);
});
}
Aggregations