use of io.confluent.examples.streams.avro.microservices.Payment in project kafka-streams-examples by confluentinc.
the class EmailServiceTest method shouldSendEmailWithValidContents.
@Test
public void shouldSendEmailWithValidContents() throws Exception {
// Given one order, customer and payment
String orderId = id(0L);
Order order = new Order(orderId, 15L, CREATED, UNDERPANTS, 3, 5.00d);
Customer customer = new Customer(15L, "Franz", "Kafka", "frans@thedarkside.net", "oppression street, prague, cze");
Payment payment = new Payment("Payment:1234", orderId, "CZK", 1000.00d);
emailService = new EmailService(details -> {
assertThat(details.customer).isEqualTo(customer);
assertThat(details.payment).isEqualTo(payment);
assertThat(details.order).isEqualTo(order);
complete = true;
});
send(Topics.CUSTOMERS, Collections.singleton(new KeyValue<>(customer.getId(), customer)));
send(Topics.ORDERS, Collections.singleton(new KeyValue<>(order.getId(), order)));
send(Topics.PAYMENTS, Collections.singleton(new KeyValue<>(payment.getId(), payment)));
// When
emailService.start(CLUSTER.bootstrapServers());
// Then
TestUtils.waitForCondition(() -> complete, "Email was never sent.");
}
use of io.confluent.examples.streams.avro.microservices.Payment in project kafka-streams-examples by confluentinc.
the class EmailService method processStreams.
private KafkaStreams processStreams(final String bootstrapServers, final String stateDir) {
KStreamBuilder builder = new KStreamBuilder();
// Create the streams/tables for the join
KStream<String, Order> orders = builder.stream(ORDERS.keySerde(), ORDERS.valueSerde(), ORDERS.name());
KStream<String, Payment> payments = builder.stream(PAYMENTS.keySerde(), PAYMENTS.valueSerde(), PAYMENTS.name());
GlobalKTable<Long, Customer> customers = builder.globalTable(CUSTOMERS.keySerde(), CUSTOMERS.valueSerde(), CUSTOMERS.name());
// Rekey payments to be by OrderId for the windowed join
payments = payments.selectKey((s, payment) -> payment.getOrderId());
// Join the two streams and the table then send an email for each
orders.join(payments, EmailTuple::new, // Join Orders and Payments streams
JoinWindows.of(1 * MIN), serdes).join(customers, (key1, tuple) -> tuple.order.getCustomerId(), // note how, because we use a GKtable, we can join on any attribute of the Customer.
(tuple, customer) -> tuple.setCustomer(customer)).peek((key, emailTuple) -> emailer.sendEmail(emailTuple));
return new KafkaStreams(builder, baseStreamsConfig(bootstrapServers, stateDir, APP_ID));
}
Aggregations