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