Search in sources :

Example 41 with Message

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();
}
Also used : Message(io.scalecube.transport.Message) CountDownLatch(java.util.concurrent.CountDownLatch) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test) BaseTest(io.scalecube.testlib.BaseTest)

Example 42 with Message

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();
}
Also used : Message(io.scalecube.transport.Message) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test) BaseTest(io.scalecube.testlib.BaseTest)

Example 43 with Message

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();
}
Also used : Message(io.scalecube.transport.Message) Builder(io.scalecube.transport.Message.Builder)

Example 44 with Message

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;
}
Also used : Logger(org.slf4j.Logger) Address(io.scalecube.transport.Address) Collection(java.util.Collection) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) CompletableFuture(java.util.concurrent.CompletableFuture) Observer(rx.Observer) AtomicReference(java.util.concurrent.atomic.AtomicReference) Subject(rx.subjects.Subject) Observable(rx.Observable) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) Map(java.util.Map) Message(io.scalecube.transport.Message) Subscription(rx.Subscription) PublishSubject(rx.subjects.PublishSubject) Collections(java.util.Collections) Message(io.scalecube.transport.Message) AtomicReference(java.util.concurrent.atomic.AtomicReference) Subscription(rx.Subscription)

Example 45 with Message

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);
    });
}
Also used : Message(io.scalecube.transport.Message)

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