Search in sources :

Example 1 with Payment

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.");
}
Also used : Order(io.confluent.examples.streams.avro.microservices.Order) Payment(io.confluent.examples.streams.avro.microservices.Payment) MicroserviceTestUtils(io.confluent.examples.streams.microservices.util.MicroserviceTestUtils) BeforeClass(org.junit.BeforeClass) TestUtils(org.apache.kafka.test.TestUtils) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) KeyValue(org.apache.kafka.streams.KeyValue) OrderId.id(io.confluent.examples.streams.microservices.domain.beans.OrderId.id) Test(org.junit.Test) Customer(io.confluent.examples.streams.avro.microservices.Customer) UNDERPANTS(io.confluent.examples.streams.avro.microservices.Product.UNDERPANTS) Topics(io.confluent.examples.streams.microservices.domain.Schemas.Topics) Schemas(io.confluent.examples.streams.microservices.domain.Schemas) After(org.junit.After) Order(io.confluent.examples.streams.avro.microservices.Order) Collections(java.util.Collections) CREATED(io.confluent.examples.streams.avro.microservices.OrderState.CREATED) Payment(io.confluent.examples.streams.avro.microservices.Payment) KeyValue(org.apache.kafka.streams.KeyValue) Customer(io.confluent.examples.streams.avro.microservices.Customer) Test(org.junit.Test)

Example 2 with Payment

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));
}
Also used : KStreamBuilder(org.apache.kafka.streams.kstream.KStreamBuilder) Order(io.confluent.examples.streams.avro.microservices.Order) Payment(io.confluent.examples.streams.avro.microservices.Payment) MIN(io.confluent.examples.streams.microservices.util.MicroserviceUtils.MIN) Logger(org.slf4j.Logger) KStreamBuilder(org.apache.kafka.streams.kstream.KStreamBuilder) LoggerFactory(org.slf4j.LoggerFactory) CUSTOMERS(io.confluent.examples.streams.microservices.domain.Schemas.Topics.CUSTOMERS) ORDERS(io.confluent.examples.streams.microservices.domain.Schemas.Topics.ORDERS) KStream(org.apache.kafka.streams.kstream.KStream) Joined(org.apache.kafka.streams.kstream.Joined) Customer(io.confluent.examples.streams.avro.microservices.Customer) MicroserviceUtils.addShutdownHookAndBlock(io.confluent.examples.streams.microservices.util.MicroserviceUtils.addShutdownHookAndBlock) JoinWindows(org.apache.kafka.streams.kstream.JoinWindows) GlobalKTable(org.apache.kafka.streams.kstream.GlobalKTable) MicroserviceUtils.baseStreamsConfig(io.confluent.examples.streams.microservices.util.MicroserviceUtils.baseStreamsConfig) PAYMENTS(io.confluent.examples.streams.microservices.domain.Schemas.Topics.PAYMENTS) KafkaStreams(org.apache.kafka.streams.KafkaStreams) Order(io.confluent.examples.streams.avro.microservices.Order) MicroserviceUtils.parseArgsAndConfigure(io.confluent.examples.streams.microservices.util.MicroserviceUtils.parseArgsAndConfigure) Payment(io.confluent.examples.streams.avro.microservices.Payment) KafkaStreams(org.apache.kafka.streams.KafkaStreams) Customer(io.confluent.examples.streams.avro.microservices.Customer)

Aggregations

Customer (io.confluent.examples.streams.avro.microservices.Customer)2 Order (io.confluent.examples.streams.avro.microservices.Order)2 Payment (io.confluent.examples.streams.avro.microservices.Payment)2 CREATED (io.confluent.examples.streams.avro.microservices.OrderState.CREATED)1 UNDERPANTS (io.confluent.examples.streams.avro.microservices.Product.UNDERPANTS)1 Schemas (io.confluent.examples.streams.microservices.domain.Schemas)1 Topics (io.confluent.examples.streams.microservices.domain.Schemas.Topics)1 CUSTOMERS (io.confluent.examples.streams.microservices.domain.Schemas.Topics.CUSTOMERS)1 ORDERS (io.confluent.examples.streams.microservices.domain.Schemas.Topics.ORDERS)1 PAYMENTS (io.confluent.examples.streams.microservices.domain.Schemas.Topics.PAYMENTS)1 OrderId.id (io.confluent.examples.streams.microservices.domain.beans.OrderId.id)1 MicroserviceTestUtils (io.confluent.examples.streams.microservices.util.MicroserviceTestUtils)1 MIN (io.confluent.examples.streams.microservices.util.MicroserviceUtils.MIN)1 MicroserviceUtils.addShutdownHookAndBlock (io.confluent.examples.streams.microservices.util.MicroserviceUtils.addShutdownHookAndBlock)1 MicroserviceUtils.baseStreamsConfig (io.confluent.examples.streams.microservices.util.MicroserviceUtils.baseStreamsConfig)1 MicroserviceUtils.parseArgsAndConfigure (io.confluent.examples.streams.microservices.util.MicroserviceUtils.parseArgsAndConfigure)1 Collections (java.util.Collections)1 KafkaStreams (org.apache.kafka.streams.KafkaStreams)1 KeyValue (org.apache.kafka.streams.KeyValue)1 GlobalKTable (org.apache.kafka.streams.kstream.GlobalKTable)1