Search in sources :

Example 1 with Paths

use of io.confluent.examples.streams.microservices.util.Paths in project kafka-streams-examples by confluentinc.

the class EndToEndTest method startEverythingElse.

@Before
public void startEverythingElse() throws Exception {
    if (!CLUSTER.isRunning()) {
        CLUSTER.start();
    }
    Topics.ALL.keySet().forEach(CLUSTER::createTopic);
    Schemas.configureSerdesWithSchemaRegistryUrl(CLUSTER.schemaRegistryUrl());
    services.add(new FraudService());
    services.add(new InventoryService());
    services.add(new OrderDetailsService());
    services.add(new ValidationsAggregatorService());
    tailAllTopicsToConsole(CLUSTER.bootstrapServers());
    services.forEach(s -> s.start(CLUSTER.bootstrapServers()));
    final OrdersService ordersService = new OrdersService(HOST, restPort);
    ordersService.start(CLUSTER.bootstrapServers());
    path = new Paths("localhost", ordersService.port());
    services.add(ordersService);
}
Also used : Paths(io.confluent.examples.streams.microservices.util.Paths) Before(org.junit.Before)

Example 2 with Paths

use of io.confluent.examples.streams.microservices.util.Paths in project kafka-streams-examples by confluentinc.

the class OrdersService method getPostValidationWithTimeout.

@GET
@ManagedAsync
@Path("orders/{id}/validated")
public void getPostValidationWithTimeout(@PathParam("id") final String id, @QueryParam("timeout") @DefaultValue(CALL_TIMEOUT) Long timeout, @Suspended final AsyncResponse asyncResponse) {
    setTimeout(timeout, asyncResponse);
    HostStoreInfo hostForKey = getKeyLocationOrBlock(id, asyncResponse);
    if (hostForKey == null) {
        // request timed out so return
        return;
    }
    // Retrieve the order locally or reach out to a different instance if the required partition is hosted elsewhere.
    if (thisHost(hostForKey)) {
        fetchLocal(id, asyncResponse, (k, v) -> (v.getState() == OrderState.VALIDATED || v.getState() == OrderState.FAILED));
    } else {
        fetchFromOtherHost(new Paths(hostForKey.getHost(), hostForKey.getPort()).urlGetValidated(id), asyncResponse, timeout);
    }
}
Also used : Paths(io.confluent.examples.streams.microservices.util.Paths) HostStoreInfo(io.confluent.examples.streams.interactivequeries.HostStoreInfo) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET) ManagedAsync(org.glassfish.jersey.server.ManagedAsync)

Example 3 with Paths

use of io.confluent.examples.streams.microservices.util.Paths in project kafka-streams-examples by confluentinc.

the class OrdersService method getWithTimeout.

/**
 * Perform a "Long-Poll" styled get. This method will attempt to get the value for the passed key
 * blocking until the key is available or passed timeout is reached. Non-blocking IO is used to
 * implement this, but the API will block the calling thread if no metastore data is available
 * (for example on startup or during a rebalance)
 *
 * @param id - the key of the value to retrieve
 * @param timeout - the timeout for the long-poll
 * @param asyncResponse - async response used to trigger the poll early should the appropriate
 * value become available
 */
@GET
@ManagedAsync
@Path("/orders/{id}")
@Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN })
public void getWithTimeout(@PathParam("id") final String id, @QueryParam("timeout") @DefaultValue(CALL_TIMEOUT) Long timeout, @Suspended final AsyncResponse asyncResponse) {
    setTimeout(timeout, asyncResponse);
    HostStoreInfo hostForKey = getKeyLocationOrBlock(id, asyncResponse);
    if (hostForKey == null) {
        // request timed out so return
        return;
    }
    // Retrieve the order locally or reach out to a different instance if the required partition is hosted elsewhere.
    if (thisHost(hostForKey)) {
        fetchLocal(id, asyncResponse, (k, v) -> true);
    } else {
        String path = new Paths(hostForKey.getHost(), hostForKey.getPort()).urlGet(id);
        fetchFromOtherHost(path, asyncResponse, timeout);
    }
}
Also used : Paths(io.confluent.examples.streams.microservices.util.Paths) HostStoreInfo(io.confluent.examples.streams.interactivequeries.HostStoreInfo) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ManagedAsync(org.glassfish.jersey.server.ManagedAsync)

Example 4 with Paths

use of io.confluent.examples.streams.microservices.util.Paths in project kafka-streams-examples by confluentinc.

the class OrdersServiceTest method shouldTimeoutGetIfNoResponseIsFound.

@Test
public void shouldTimeoutGetIfNoResponseIsFound() {
    final Client client = ClientBuilder.newClient();
    // Start the rest interface
    rest = new OrdersService("localhost");
    rest.start(CLUSTER.bootstrapServers());
    Paths paths = new Paths("localhost", rest.port());
    // Then GET order should timeout
    try {
        client.target(paths.urlGet(1)).queryParam("timeout", // Lower the request timeout
        100).request(APPLICATION_JSON_TYPE).get(new GenericType<OrderBean>() {
        });
        fail("Request should have failed as materialized view has not been updated");
    } catch (ServerErrorException e) {
        assertThat(e.getMessage()).isEqualTo("HTTP 504 Gateway Timeout");
    }
}
Also used : Paths(io.confluent.examples.streams.microservices.util.Paths) ServerErrorException(javax.ws.rs.ServerErrorException) Client(javax.ws.rs.client.Client) OrderBean(io.confluent.examples.streams.microservices.domain.beans.OrderBean) Test(org.junit.Test)

Example 5 with Paths

use of io.confluent.examples.streams.microservices.util.Paths in project kafka-streams-examples by confluentinc.

the class OrdersServiceTest method shouldGetValidatedOrderOnRequest.

@Test
public void shouldGetValidatedOrderOnRequest() {
    Order orderV1 = new Order(id(1L), 3L, OrderState.CREATED, Product.JUMPERS, 10, 100d);
    OrderBean beanV1 = OrderBean.toBean(orderV1);
    final Client client = ClientBuilder.newClient();
    // Given a rest service
    rest = new OrdersService("localhost");
    rest.start(CLUSTER.bootstrapServers());
    Paths paths = new Paths("localhost", rest.port());
    // When we post an order
    client.target(paths.urlPost()).request(APPLICATION_JSON_TYPE).post(Entity.json(beanV1));
    // Simulate the order being validated
    MicroserviceTestUtils.sendOrders(Collections.singletonList(newBuilder(orderV1).setState(OrderState.VALIDATED).build()));
    // When we GET the order from the returned location
    OrderBean returnedBean = client.target(paths.urlGetValidated(beanV1.getId())).queryParam("timeout", MIN / 2).request(APPLICATION_JSON_TYPE).get(new GenericType<OrderBean>() {
    });
    // Then status should be Validated
    assertThat(returnedBean.getState()).isEqualTo(OrderState.VALIDATED);
}
Also used : Order(io.confluent.examples.streams.avro.microservices.Order) Paths(io.confluent.examples.streams.microservices.util.Paths) OrderBean(io.confluent.examples.streams.microservices.domain.beans.OrderBean) Client(javax.ws.rs.client.Client) Test(org.junit.Test)

Aggregations

Paths (io.confluent.examples.streams.microservices.util.Paths)7 OrderBean (io.confluent.examples.streams.microservices.domain.beans.OrderBean)4 Client (javax.ws.rs.client.Client)4 Test (org.junit.Test)4 HostStoreInfo (io.confluent.examples.streams.interactivequeries.HostStoreInfo)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 GenericType (javax.ws.rs.core.GenericType)2 ManagedAsync (org.glassfish.jersey.server.ManagedAsync)2 Order (io.confluent.examples.streams.avro.microservices.Order)1 Produces (javax.ws.rs.Produces)1 ServerErrorException (javax.ws.rs.ServerErrorException)1 Response (javax.ws.rs.core.Response)1 Before (org.junit.Before)1