SlideShare a Scribd company logo
Edgar Domingues
October 2021
Kafka Client and Emitters
01
02
03
04
Agenda
Apache Kafka
Kafka vs. Message Broker
Quarkus Extension for Apache Kafka
Testing a Kafka application
2
Apache Kafka
Open-source distributed event streaming platform
“event streaming is the practice of
• capturing data in real-time from event sources (like databases, sensors, mobile
devices, cloud services, and software applications) in the form of streams of
events;
• storing these event streams durably for later retrieval;
• manipulating, processing, and reacting to the event streams in real-time as well
as retrospectively; and
• routing the event streams to different destination technologies as needed.”
-- https://kafka.apache.org/documentation/
3
4
https://miro.medium.com/max/3720/1*5DMYoWniIyN7YRoJof4K_w.png
Topics & Partitions
5
https://linuxhint.com/wp-content/uploads/2018/10/1-31.png
Partitioner
Producer will decide target partition to place any message, depending on:
1. Partition id, if it's specified within the message
2. key % num partitions, if no partition id is mentioned
3. Round robin if neither partition id nor message key are available in message,
meaning only value is available
6
Consumer Groups
7
https://betterprogramming.pub/rabbitmq-vs-kafka-1ef22a041793
Kafka vs. Message Broker
Kafka
8
• Dumb-Broker and Smart-Consumer
• Pull
• Topics/Partitions/Groups
• Connectors
• Stronger guarantees of messages
ordering
• Persisted events
Message Broker (e.g. RabbitMQ
• Smart-Broker and Dumb-Consumer
• Push
• Queues/Exchanges/Routing
• Superior support when it comes to
routing and filtering messages for
consumers to use.
• Message Time-To-Live and
delayed/scheduled messages
• Delivery retries and dead-letter
exchanges/queues
9
Quarkus Extension for Apache Kafka
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-reactive-messaging-kafka</artifactId>
</dependency>
10
Disable kafka health check
# Keep kafka health disabled to avoid leaking the address of the kafka instances
quarkus.kafka.health.enabled=false
11
Receiving messages from kafka
@Incoming("accounts")
@Blocking
public void consumeAccountEvent(final JsonObject json) {
// handle event
}
12
Configuring Receiver
#kafka.bootstrap.servers # from environment
mp.messaging.incoming.accounts.connector=smallrye-kafka
mp.messaging.incoming.accounts.topic=event-splitter.accounts
mp.messaging.incoming.accounts.value.deserializer=io.vertx.kafka.client.serialization.JsonObjectDeserializer
mp.messaging.incoming.accounts.group.id=wfm-mercury-accounts
mp.messaging.incoming.accounts.auto.offset.reset=earliest
mp.messaging.incoming.accounts.security.protocol=SASL_SSL
mp.messaging.incoming.accounts.sasl.mechanism=SCRAM-SHA-512
mp.messaging.incoming.accounts.sasl.jaas.config=org.apache.kafka.common.security.scram.ScramLoginModule
required username=${kafka.username} password="${kafka.password}";
mp.messaging.incoming.accounts.ssl.truststore.location=${kafka.truststore.location}
mp.messaging.incoming.accounts.ssl.truststore.password=${kafka.truststore.password}
13
Sending messages to kafka
@Inject
@Channel("timeseries-data-reporting")
@OnOverflow(value = OnOverflow.Strategy.UNBOUNDED_BUFFER)
Emitter<TimeSeriesReportEvent> emitter;
void publish(final TimeSeriesReportEvent event) {
// emitter.send(event);
String key = event.getAccountId() + "-" + event.getQueueId();
emitter.send(KafkaRecord.of(key, event));
}
14
Configuring Emitter
#kafka.bootstrap.servers # from environment
mp.messaging.outgoing.timeseries-data-reporting.connector=smallrye-kafka
mp.messaging.outgoing.timeseries-data-reporting.topic=wfm-timeseries.timeseries-data-reporting
mp.messaging.outgoing.timeseries-data-reporting.value.serializer=io.quarkus.kafka.client.serialization.JsonbSerializer
mp.messaging.outgoing.timeseries-data-reporting.security.protocol=SASL_SSL
mp.messaging.outgoing.timeseries-data-reporting.sasl.mechanism=SCRAM-SHA-512
mp.messaging.outgoing.timeseries-data-reporting.sasl.jaas.config=org.apache.kafka.common.security.scram.ScramLoginModule
required username=${kafka.username} password="${kafka.password}";
mp.messaging.outgoing.timeseries-data-reporting.ssl.truststore.location=${kafka.truststore.location}
mp.messaging.outgoing.timeseries-data-reporting.ssl.truststore.password=${kafka.truststore.password}
mp.messaging.outgoing.timeseries-data-reporting.compression.type=zstd
Overflow management
// Set the max size to 10 and fail if reached
@OnOverflow(value = OnOverflow.Strategy.BUFFER, bufferSize = 10)
@Inject @Channel("channel") Emitter<String> emitterWithBuffer;
// [DANGER ZONE] no limit
@OnOverflow(OnOverflow.Strategy.UNBOUNDED_BUFFER)
@Inject @Channel("channel") Emitter<String> danger;
// Drop the new messages if the size is reached
@OnOverflow(OnOverflow.Strategy.DROP)
@Inject @Channel("channel") Emitter<String> dropping;
// Drop the previously sent messages if the size is reached
@OnOverflow(OnOverflow.Strategy.LATEST)
@Inject @Channel("channel") Emitter<String> dropOldMessages;
https://smallrye.io/smallrye-reactive-messaging/smallrye-reactive-messaging/3.2/emitter/emitter.html#emitter-overflow
16
Testing a Kafka application
<dependency>
<groupId>io.smallrye.reactive</groupId>
<artifactId>smallrye-reactive-messaging-in-memory</artifactId>
<scope>test</scope>
</dependency>
17
Kafka Test Resource Lifecycle Manager
public class KafkaTestResourceLifecycleManager implements QuarkusTestResourceLifecycleManager {
@Override
public Map<String, String> start() {
return InMemoryConnector.switchIncomingChannelsToInMemory("accounts");
}
@Override
public void stop() {
InMemoryConnector.clear();
}
}
18
Test class
@QuarkusTest
@QuarkusTestResource(KafkaTestResourceLifecycleManager.class)
class KafkaListenerIT {
@Inject
@Any
InMemoryConnector connector;
19
Test receiving messages from kafka
@Test
void handleAccountEvent() {
// Arrange
InMemorySource<JsonObject> emitter = connector.source("accounts");
// Act
emitter.send(...);
// Assert
// assert event handled as expected…
20
Test sending messages to Kafka
@Test
public void sendTimeSeriesReportEvent() {
// Arrange
InMemorySink<TimeSeriesReportEvent> events = connector.sink("timeseries-data-reporting");
//...
// Act
service.publish(event);
// Assert
assertThat(events.received().size(), is(1));
// assert event key and payload as expected...
}
Questions?
21
22
23
https://betterprogramming.pub/rabbitmq-vs-kafka-1ef22a041793

More Related Content

PPTX
Introduction to Microservices Patterns
PPTX
Apache kafka
PDF
Apache Kafka Introduction
PDF
Building Microservices with Apache Kafka
PPTX
Cisco Web and Email Security Overview
PDF
Incrementally streaming rdbms data to your data lake automagically
PPTX
Apache Kafka - Overview
PDF
Apache Kafka - Martin Podval
Introduction to Microservices Patterns
Apache kafka
Apache Kafka Introduction
Building Microservices with Apache Kafka
Cisco Web and Email Security Overview
Incrementally streaming rdbms data to your data lake automagically
Apache Kafka - Overview
Apache Kafka - Martin Podval

What's hot (20)

PPTX
Kafka Tutorial - Introduction to Apache Kafka (Part 1)
DOCX
Data power Performance Tuning
PDF
Introduction to apache kafka
PPSX
Event Sourcing & CQRS, Kafka, Rabbit MQ
PPTX
Druid deep dive
PDF
Getting started with GCP ( Google Cloud Platform)
PPSX
Elastic-Engineering
PPTX
PPTX
Streaming Data and Stream Processing with Apache Kafka
PDF
Apache Kafka Fundamentals for Architects, Admins and Developers
PDF
Rover: Implementing Landing Zone Using Docker Container
PDF
ksqlDB: A Stream-Relational Database System
PDF
Reliable Event Delivery in Apache Kafka Based on Retry Policy and Dead Letter...
PDF
Exactly-Once Semantics Revisited: Distributed Transactions across Flink and K...
PPTX
Agile Reporting in JIRA
PDF
Introduction to Hortonworks Data Platform
PDF
Eventing Things - A Netflix Original! (Nitin Sharma, Netflix) Kafka Summit SF...
PPTX
A visual introduction to Apache Kafka
PDF
Distributed stream processing with Apache Kafka
PDF
Fundamentals of Apache Kafka
Kafka Tutorial - Introduction to Apache Kafka (Part 1)
Data power Performance Tuning
Introduction to apache kafka
Event Sourcing & CQRS, Kafka, Rabbit MQ
Druid deep dive
Getting started with GCP ( Google Cloud Platform)
Elastic-Engineering
Streaming Data and Stream Processing with Apache Kafka
Apache Kafka Fundamentals for Architects, Admins and Developers
Rover: Implementing Landing Zone Using Docker Container
ksqlDB: A Stream-Relational Database System
Reliable Event Delivery in Apache Kafka Based on Retry Policy and Dead Letter...
Exactly-Once Semantics Revisited: Distributed Transactions across Flink and K...
Agile Reporting in JIRA
Introduction to Hortonworks Data Platform
Eventing Things - A Netflix Original! (Nitin Sharma, Netflix) Kafka Summit SF...
A visual introduction to Apache Kafka
Distributed stream processing with Apache Kafka
Fundamentals of Apache Kafka
Ad

Similar to Kafka clients and emitters (20)

PDF
Connect K of SMACK:pykafka, kafka-python or?
PDF
Reactive messaging Quarkus and Kafka
PPTX
DevNexus - Reacting to an event driven world
PDF
Developing Realtime Data Pipelines With Apache Kafka
ODP
Introduction to Apache Kafka- Part 2
PPTX
Introduction to Kafka and Event-Driven
PDF
Introduction to Kafka and Event-Driven
PDF
Kafka Workshop
PPTX
Apache Kafka 0.8 basic training - Verisign
PPTX
Real-time streaming and data pipelines with Apache Kafka
PDF
Developing Real-Time Data Pipelines with Apache Kafka
ODP
Apache Kafka Demo
PPTX
Building an Event Bus at Scale
PDF
Grokking TechTalk #24: Kafka's principles and protocols
PDF
Introduction and Overview of Apache Kafka, TriHUG July 23, 2013
PDF
Fault Tolerance with Kafka
PPTX
Introduction Apache Kafka
PPTX
Kafkha real time analytics platform.pptx
PPTX
Large scale, distributed and reliable messaging with Kafka
PDF
Trivadis TechEvent 2016 Apache Kafka - Scalable Massage Processing and more! ...
Connect K of SMACK:pykafka, kafka-python or?
Reactive messaging Quarkus and Kafka
DevNexus - Reacting to an event driven world
Developing Realtime Data Pipelines With Apache Kafka
Introduction to Apache Kafka- Part 2
Introduction to Kafka and Event-Driven
Introduction to Kafka and Event-Driven
Kafka Workshop
Apache Kafka 0.8 basic training - Verisign
Real-time streaming and data pipelines with Apache Kafka
Developing Real-Time Data Pipelines with Apache Kafka
Apache Kafka Demo
Building an Event Bus at Scale
Grokking TechTalk #24: Kafka's principles and protocols
Introduction and Overview of Apache Kafka, TriHUG July 23, 2013
Fault Tolerance with Kafka
Introduction Apache Kafka
Kafkha real time analytics platform.pptx
Large scale, distributed and reliable messaging with Kafka
Trivadis TechEvent 2016 Apache Kafka - Scalable Massage Processing and more! ...
Ad

Recently uploaded (20)

PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
cuic standard and advanced reporting.pdf
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
KodekX | Application Modernization Development
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Machine learning based COVID-19 study performance prediction
PDF
Empathic Computing: Creating Shared Understanding
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
A Presentation on Artificial Intelligence
Network Security Unit 5.pdf for BCA BBA.
Reach Out and Touch Someone: Haptics and Empathic Computing
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
The Rise and Fall of 3GPP – Time for a Sabbatical?
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Review of recent advances in non-invasive hemoglobin estimation
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Building Integrated photovoltaic BIPV_UPV.pdf
20250228 LYD VKU AI Blended-Learning.pptx
Chapter 3 Spatial Domain Image Processing.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
cuic standard and advanced reporting.pdf
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
KodekX | Application Modernization Development
NewMind AI Weekly Chronicles - August'25 Week I
Machine learning based COVID-19 study performance prediction
Empathic Computing: Creating Shared Understanding
“AI and Expert System Decision Support & Business Intelligence Systems”
A Presentation on Artificial Intelligence

Kafka clients and emitters

  • 2. 01 02 03 04 Agenda Apache Kafka Kafka vs. Message Broker Quarkus Extension for Apache Kafka Testing a Kafka application 2
  • 3. Apache Kafka Open-source distributed event streaming platform “event streaming is the practice of • capturing data in real-time from event sources (like databases, sensors, mobile devices, cloud services, and software applications) in the form of streams of events; • storing these event streams durably for later retrieval; • manipulating, processing, and reacting to the event streams in real-time as well as retrospectively; and • routing the event streams to different destination technologies as needed.” -- https://kafka.apache.org/documentation/ 3
  • 6. Partitioner Producer will decide target partition to place any message, depending on: 1. Partition id, if it's specified within the message 2. key % num partitions, if no partition id is mentioned 3. Round robin if neither partition id nor message key are available in message, meaning only value is available 6
  • 8. Kafka vs. Message Broker Kafka 8 • Dumb-Broker and Smart-Consumer • Pull • Topics/Partitions/Groups • Connectors • Stronger guarantees of messages ordering • Persisted events Message Broker (e.g. RabbitMQ • Smart-Broker and Dumb-Consumer • Push • Queues/Exchanges/Routing • Superior support when it comes to routing and filtering messages for consumers to use. • Message Time-To-Live and delayed/scheduled messages • Delivery retries and dead-letter exchanges/queues
  • 9. 9 Quarkus Extension for Apache Kafka <dependency> <groupId>io.quarkus</groupId> <artifactId>quarkus-smallrye-reactive-messaging-kafka</artifactId> </dependency>
  • 10. 10 Disable kafka health check # Keep kafka health disabled to avoid leaking the address of the kafka instances quarkus.kafka.health.enabled=false
  • 11. 11 Receiving messages from kafka @Incoming("accounts") @Blocking public void consumeAccountEvent(final JsonObject json) { // handle event }
  • 12. 12 Configuring Receiver #kafka.bootstrap.servers # from environment mp.messaging.incoming.accounts.connector=smallrye-kafka mp.messaging.incoming.accounts.topic=event-splitter.accounts mp.messaging.incoming.accounts.value.deserializer=io.vertx.kafka.client.serialization.JsonObjectDeserializer mp.messaging.incoming.accounts.group.id=wfm-mercury-accounts mp.messaging.incoming.accounts.auto.offset.reset=earliest mp.messaging.incoming.accounts.security.protocol=SASL_SSL mp.messaging.incoming.accounts.sasl.mechanism=SCRAM-SHA-512 mp.messaging.incoming.accounts.sasl.jaas.config=org.apache.kafka.common.security.scram.ScramLoginModule required username=${kafka.username} password="${kafka.password}"; mp.messaging.incoming.accounts.ssl.truststore.location=${kafka.truststore.location} mp.messaging.incoming.accounts.ssl.truststore.password=${kafka.truststore.password}
  • 13. 13 Sending messages to kafka @Inject @Channel("timeseries-data-reporting") @OnOverflow(value = OnOverflow.Strategy.UNBOUNDED_BUFFER) Emitter<TimeSeriesReportEvent> emitter; void publish(final TimeSeriesReportEvent event) { // emitter.send(event); String key = event.getAccountId() + "-" + event.getQueueId(); emitter.send(KafkaRecord.of(key, event)); }
  • 14. 14 Configuring Emitter #kafka.bootstrap.servers # from environment mp.messaging.outgoing.timeseries-data-reporting.connector=smallrye-kafka mp.messaging.outgoing.timeseries-data-reporting.topic=wfm-timeseries.timeseries-data-reporting mp.messaging.outgoing.timeseries-data-reporting.value.serializer=io.quarkus.kafka.client.serialization.JsonbSerializer mp.messaging.outgoing.timeseries-data-reporting.security.protocol=SASL_SSL mp.messaging.outgoing.timeseries-data-reporting.sasl.mechanism=SCRAM-SHA-512 mp.messaging.outgoing.timeseries-data-reporting.sasl.jaas.config=org.apache.kafka.common.security.scram.ScramLoginModule required username=${kafka.username} password="${kafka.password}"; mp.messaging.outgoing.timeseries-data-reporting.ssl.truststore.location=${kafka.truststore.location} mp.messaging.outgoing.timeseries-data-reporting.ssl.truststore.password=${kafka.truststore.password} mp.messaging.outgoing.timeseries-data-reporting.compression.type=zstd
  • 15. Overflow management // Set the max size to 10 and fail if reached @OnOverflow(value = OnOverflow.Strategy.BUFFER, bufferSize = 10) @Inject @Channel("channel") Emitter<String> emitterWithBuffer; // [DANGER ZONE] no limit @OnOverflow(OnOverflow.Strategy.UNBOUNDED_BUFFER) @Inject @Channel("channel") Emitter<String> danger; // Drop the new messages if the size is reached @OnOverflow(OnOverflow.Strategy.DROP) @Inject @Channel("channel") Emitter<String> dropping; // Drop the previously sent messages if the size is reached @OnOverflow(OnOverflow.Strategy.LATEST) @Inject @Channel("channel") Emitter<String> dropOldMessages; https://smallrye.io/smallrye-reactive-messaging/smallrye-reactive-messaging/3.2/emitter/emitter.html#emitter-overflow
  • 16. 16 Testing a Kafka application <dependency> <groupId>io.smallrye.reactive</groupId> <artifactId>smallrye-reactive-messaging-in-memory</artifactId> <scope>test</scope> </dependency>
  • 17. 17 Kafka Test Resource Lifecycle Manager public class KafkaTestResourceLifecycleManager implements QuarkusTestResourceLifecycleManager { @Override public Map<String, String> start() { return InMemoryConnector.switchIncomingChannelsToInMemory("accounts"); } @Override public void stop() { InMemoryConnector.clear(); } }
  • 19. 19 Test receiving messages from kafka @Test void handleAccountEvent() { // Arrange InMemorySource<JsonObject> emitter = connector.source("accounts"); // Act emitter.send(...); // Assert // assert event handled as expected…
  • 20. 20 Test sending messages to Kafka @Test public void sendTimeSeriesReportEvent() { // Arrange InMemorySink<TimeSeriesReportEvent> events = connector.sink("timeseries-data-reporting"); //... // Act service.publish(event); // Assert assertThat(events.received().size(), is(1)); // assert event key and payload as expected... }
  • 22. 22