SlideShare a Scribd company logo
Confidential │ ©2021 VMware, Inc.
Walking through the Spring
Stack for Apache Kafka
Kafka Summit London
April 25-26, 2022
Confidential │ ©2022 VMware, Inc. 2
About me...
Soby Chacko
Committer - Spring Cloud Stream/Spring
Cloud Data Flow at VMware
Tech lead for Spring Cloud Stream
Kafka/Kafka Streams binders
Twitter: @sobychacko
Github: github.com/sobychacko
Confidential │ ©2022 VMware, Inc. 3
Agenda
● Comprehensive look at the Apache Kafka
ecosystem in Spring
● Demo Applications
● Questions
Confidential │ ©2022 VMware, Inc. 4
Spring Stack for Apache Kafka
Spring Boot
Spring for Apache Kafka
Spring Integration Kafka
Spring Cloud Stream Binders for Kafka
and Kafka Streams
Reactor Kafka
Apache Kafka Java Client / Kafka Streams Client
Confidential │ ©2022 VMware, Inc. 5
Spring for Apache Kafka - Top Level Ideas
➔ Foundational library for Apache Kafka in Spring
➔ Spring friendly programming abstractions
➔ Many features that allow developers to focus on the business logic
➔ Provides abstractions for low-level infrastructure concerns
➔ Leverages and builds upon Apache Kafka Java Client / Kafka
Streams
Confidential │ ©2022 VMware, Inc. 6
Spring for Apache Kafka - Topic Management
@Bean
public KafkaAdmin admin() {
Map<String, Object> configs = new HashMap<>();
configs.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
return new KafkaAdmin(configs);
}
@Bean
public NewTopic myTopic() {
return TopicBuilder.name("my-topic")
.partitions(10)
.replicas(3)
.compact()
.build();
}
@Bean
public KafkaAdmin.NewTopics bunchOfTopics() {
return new KafkaAdmin.NewTopics(
TopicBuilder.name("topic1")
.build(),
TopicBuilder.name("topic2")
.replicas(1)
.build(),
TopicBuilder.name("topic3")
.partitions(3)
.build());
}
Confidential │ ©2022 VMware, Inc. 7
Publishing Records using KafkaTemplate API
KafkaTemplate provides a wide variety of template methods to publish records
ListenableFuture<SendResult<K, V>> sendDefault(K key, V data);
ListenableFuture<SendResult<K, V>> send(String topic, K key, V data);
ListenableFuture<SendResult<K, V>> send(String topic, Integer partition, K key, V data);
Confidential │ ©2022 VMware, Inc. 8
Async Send Example
ListenableFuture<SendResult<Integer, String>> future =
template.send("topic", 1, "data");
future.addCallback(new KafkaSendCallback<Integer, String>() {
@Override
public void onSuccess(SendResult<Integer, String> result) {...}
@Override
public void onFailure(KafkaProducerException ex) {
ProducerRecord<Integer, String> failed = ex.getFailedProducerRecord();
...
}
});
Confidential │ ©2022 VMware, Inc. 9
Producer Factory
● KafkaTemplate uses the producer factory for creating the producer
● Spring Boot auto-configures a producer factory bean
● Applications can provide custom producer factories
@Bean
public ProducerFactory<?, ?> kafkaProducerFactory() {
DefaultKafkaProducerFactory<?, ?> factory = new DefaultKafkaProducerFactory<>(
Map.of(...))
…
return factory;
}
Confidential │ ©2022 VMware, Inc. 10
Consuming Records using KafkaListener
● @KafkaListener annotation provides a lot of flexible options to consume records
from Kafka topics
● @EnableKafka annotation
@KafkaListener(id = "my-group", topics = "my-topic")
public void listen(String in) {
logger.info("Data Received : " + in);
}
Basic KafkaListener
Confidential │ ©2022 VMware, Inc. 11
KafkaListener with More Options
See the reference docs for all the available options of KafkaListener.
@KafkaListener(id = "my-group", topicPartitions =
@TopicPartition(topic = "my-topic", partitionOffsets = {
@PartitionOffset(partition = "0-2", initialOffset = "0"),
@PartitionOffset(partition = "6-9", initialOffset = "0")}),
concurrency = ${config.concurrency}
properties = "key.deserializer:LongDeserializer")
public void listen(String in, @Header(KafkaHeaders.RECEIVED_MESSAGE_KEY) Long key,
@Header(KafkaHeaders.RECEIVED_PARTITION_ID) int part,
@Header(KafkaHeaders.OFFSET) int offset) {
logger.info( "Data Received : {} with key {} from partition {} and offset {}.",
in, key,part,offset);
}
Confidential │ ©2022 VMware, Inc. 12
KafkaListener on a Class
@KafkaListener(id = "multi", topics = "myTopic")
static class MultiListenerBean {
@KafkaHandler
public void listen1(String foo) {
...
}
@KafkaHandler
public void listen2(Integer bar) {
...
}
@KafkaHandler(isDefault = true)
public void listenDefault(Object object) {
...
}
}
Confidential │ ©2022 VMware, Inc. 13
MessageListenerContainer
● Behind the scenes, KafkaListener uses a MessageListener
Container for accomplishing various tasks such as polling,
committing, error handling etc.
● KafkaMessageListenerContainer - Single listener thread
● ConcurrentKafkaMessageListenerContainer - Multiple listeners
● Spring Boot auto-configures a factory for creating a message listener
container
Confidential │ ©2022 VMware, Inc. 14
MessageListeners
● Spring for Apache Kafka provides various types of message listeners
● Record and Batch based Message Listeners
● onMessage(...) method that gives access to the consumer record
● You can provide your own implementation to the container
● MessageListener
● AcknowledgingMessageListener
● ConsumerAwareMessageListener
● AcknowledgingConsumerAwareMessageListener
● BatchMessageListener
● BatchAcknowledgingMessageListener
● BatchConsumerAwareMessageListener
● BatchAcknowledgingConsumerAwareMessageListener
Confidential │ ©2022 VMware, Inc. 15
Committing the Offsets
● By default, enable.auto.commit is disabled in Spring for Apache
Kafka and the framework is responsible for committing the offsets
● A number of options are available for commits which the applications
can configure on the container through the AckMode property of
ContainerProperties
– RECORD, BATCH, TIME, COUNT, MANUAL,
MANUAL_IMMEDIATE
● Default AckMode is the BATCH
Confidential │ ©2022 VMware, Inc. 16
KafkaListener Example of Manually Acknowledging
@KafkaListener(id = "cat",
topics = "myTopic",
containerFactory =
"kafkaManualAckListenerContainerFactory")
public void listen(String data, Acknowledgment ack) {
…
ack.acknowledge();
}
Confidential │ ©2022 VMware, Inc. 17
Seeking Offsets
● Listener must implement the ConsumerSeekAware interface
● Framework provides a convenient AbstractConsumerSeekAware
● Using the callbacks, the application can do arbitrary seeks such as
seekToBeginning, seekToEnd, seekToRelative, seekToTimestamp
etc.
void registerSeekCallback(ConsumerSeekCallback callback);
void onPartitionsAssigned(Map<TopicPartition, Long> assignments,
ConsumerSeekCallback callback);
void onPartitionsRevoked(Collection<TopicPartition> partitions);
void onIdleContainer(Map<TopicPartition, Long> assignments,
ConsumerSeekCallback callback);
Confidential │ ©2022 VMware, Inc. 18
Container Error Handling
● Container Error Handler - CommonErrorHandler interface
● Default implementation - DefaultErrorHandler
● By default, DefaultErrorHandler provides 10 max attempts and no backoff
● You can provide a custom bean that overrides these defaults
@Bean
public DefaultErrorHandler errorHandler(
DeadletterPublishingRecoverer recoverer) {
DefaultErrorHandler handler = new DefaultErrorHandler(recoverer,
new FixedBackOff(2_000, 2));
handler.addNotRetryableExceptions(IllegalArgumentException.class);
return handler;
}
Confidential │ ©2022 VMware, Inc. 19
ErrorHandlingDeserializer
● Convenient way to catch deserialization errors
● ErrorHandlingDeserializer has delegates to the actual key/value
deserializers
● When deserialization fails, the proper headers are populated with
the exception and the raw data that failed
● Then normal container error handling rules apply
Confidential │ ©2022 VMware, Inc. 20
Extensive Error Handling Options
● Spring for Apache Kafka provides much more features, options and flexibility for
error handling. See the reference documentation for more information.
https://docs.spring.io/spring-kafka/docs/current/reference/html/#annotation-error-handling
● Non Blocking Retries - A feature in active development. See the documentation
section for more details.
https://docs.spring.io/spring-kafka/docs/current/reference/html/#retry-topic
Confidential │ ©2022 VMware, Inc. 21
Testing using EmbeddedKafka Broker
● spring-kafka-test provides a convenient way to test with an embedded broker
● EmbeddedKafka annotation and EmbeddedKafkaBroker for Spring test context with JUnit 5
● For non-spring test context, use the EmbeddedKafkaCondition with JUnit 5
● For JUnit4, use EmbeddedKafkaRule, which is a JUnit4 ClassRule
● More details at:
https://docs.spring.io/spring-kafka/docs/current/reference/html/#embedded-kafka-annotation
@SpringBootTest
@EmbeddedKafka(topics = "my-topic", bootstrapServersProperty =
"spring.kafka.bootstrap-servers")
class SpringKafkaApp1Tests {
@Test
void test(EmbeddedKafkaBroker broker) {
// ...
}
}
Confidential │ ©2022 VMware, Inc. 22
Advanced Spring for Apache Kafka features
● Request/Reply Semantics -
https://docs.spring.io/spring-kafka/docs/current/reference/html/#replyi
ng-template
● Transactions -
https://docs.spring.io/spring-kafka/docs/current/reference/html/#trans
actions
● Kafka Streams Support -
https://docs.spring.io/spring-kafka/docs/current/reference/html/#strea
ms-kafka-streams
Confidential │ ©2022 VMware, Inc. 23
Spring Integration Overview
● Messages and Channels
● Integration Flows
- Spring Integration is used to create complex EIP flows
- Kafka flows can tap into larger flows
● Provides Java Config and DSL
Confidential │ ©2022 VMware, Inc. 24
Spring Integration Kafka Support
● Outbound Channel Adapter – KafkaProducerMessageHandler
● Kafka Message Driven Channel Adapter
● Inbound Channel Adapter - Provides a KafkaMessageSource which
is a pollable channel adapter implementation.
● Outbound Gateway - for request/reply operations
● Inbound Gateway - for request/reply operations
See more details in the reference docs for Spring Integration at
https://docs.spring.io/spring-integration/docs/current/reference/html/kafka.html#kafka
Confidential │ ©2022 VMware, Inc. 25
Spring Cloud Stream Overview
● General purpose framework for writing event driven/stream
processing micro services
● Programming model based on Spring Cloud Function - Java
8 Functions/Function Composition
● Apache Kafka support is built on top of Spring Boot, Spring
for Apache Kafka, Spring Integration and Kafka Streams
Confidential │ ©2022 VMware, Inc. 26
Spring Cloud Stream Kafka Application Architecture
Spring Boot Application
Spring Cloud Stream App Core
Kafka/Kafka Streams Binder
Inputs (Message
Channel/Kafka
Streams)
Outputs(Message
Channel/Kafka
Streams)
Kafka Broker
Spring Integration/Kafka Streams
Spring for Apache Kafka
Confidential │ ©2022 VMware, Inc. 27
Spring Cloud Stream - Kafka Binder
● Auto provisioning of topics
● Producer/Consumer Binding
● Message Converters/Native Serialization
● DLQ Support
● Health checks for the binder
Confidential │ ©2022 VMware, Inc. 28
Spring Cloud Stream Kafka Binder Programming Model
@SpringBootApplication
…
@Bean
public Supplier<Long> currentTime() {
return System::currentTimeMillis;
}
@Bean
public Function<Long, String> convertToUTC() {
return localTime -> formatUTC();
}
@Bean
public Consumer<String> print() {
System.out::println;
}
Confidential │ ©2022 VMware, Inc. 29
Spring Cloud Stream - Kafka Streams Binder
● Built on the Kafka Streams foundations from Spring for Apache
Kafka - StreamsBuilderFactoryBean
● Kafka Streams processors written as Java functions
● KStream, KTable and GlobalKTable bindings
● Serde inference on input/output
● Interactive Query Support
● Multiple functions in the same application
● Function composition
Confidential │ ©2022 VMware, Inc. 30
Examples of Spring Cloud Stream Kafka Streams Functions
@Bean
public Function<KStream<Object, String>, KStream<String, Long>> wordCount()
{
return input -> input
.flatMapValues(value ->
Arrays.asList(value.toLowerCase().split("W+")))
.map((key, value) -> new KeyValue<>(value, value))
.groupByKey()
.windowedBy(TimeWindows.of(Duration.ofSeconds(WINDOW_SIZE_SECONDS)))
.count()
.toStream()
.map((key, value) -> new KeyValue<>(key.key(), value));
}
@Bean
public Consumer<KStream<Object, Long>> logWordCounts() {
return kstream -> kstream.foreach((key, value) -> {
logger.info(String.format("Word counts for: %s t %d", key, value));
});
}
Confidential │ ©2022 VMware, Inc. 31
Resources
Spring for Apache Kafka
Project Site Reference Docs GitHub StackOverflow
Spring Integration Kafka
Project Site Reference Docs GitHub StackOverflow
Spring Cloud Stream
Project Site Reference Docs GitHub StackOverflow
Spring Boot Kafka Docs
Confidential │ ©2021 VMware, Inc. 32
Demo
https://github.com/schacko-samples/kafka-summit-london-2022
Confidential │ ©2021 VMware, Inc. 33

More Related Content

What's hot (20)

PPTX
Microservices in the Apache Kafka Ecosystem
confluent
 
PDF
IBM MQ and Kafka, what is the difference?
David Ware
 
PDF
An Introduction to Apache Kafka
Amir Sedighi
 
PPTX
Apache kafka
Ramakrishna kapa
 
PDF
Distributed Tracing for Kafka with OpenTelemetry with Daniel Kim | Kafka Summ...
HostedbyConfluent
 
PPSX
Event Sourcing & CQRS, Kafka, Rabbit MQ
Araf Karsh Hamid
 
PDF
Building an Interactive Query Service in Kafka Streams With Bill Bejeck | Cur...
HostedbyConfluent
 
PDF
Getting Started with Confluent Schema Registry
confluent
 
PPTX
Kafka
shrenikp
 
PPSX
Microservices Docker Kubernetes Istio Kanban DevOps SRE
Araf Karsh Hamid
 
PDF
Securing Kafka
confluent
 
PDF
Schema Registry 101 with Bill Bejeck | Kafka Summit London 2022
HostedbyConfluent
 
PDF
Apache kafka
NexThoughts Technologies
 
PDF
Introducing Vault
Ramit Surana
 
PDF
Apache Kafka - Martin Podval
Martin Podval
 
PDF
Introduction to Vault
Knoldus Inc.
 
PPTX
Managing multiple event types in a single topic with Schema Registry | Bill B...
HostedbyConfluent
 
PDF
ksqlDB - Stream Processing simplified!
Guido Schmutz
 
PDF
Credential store using HashiCorp Vault
Mayank Patel
 
PDF
Fundamentals of Apache Kafka
Chhavi Parasher
 
Microservices in the Apache Kafka Ecosystem
confluent
 
IBM MQ and Kafka, what is the difference?
David Ware
 
An Introduction to Apache Kafka
Amir Sedighi
 
Apache kafka
Ramakrishna kapa
 
Distributed Tracing for Kafka with OpenTelemetry with Daniel Kim | Kafka Summ...
HostedbyConfluent
 
Event Sourcing & CQRS, Kafka, Rabbit MQ
Araf Karsh Hamid
 
Building an Interactive Query Service in Kafka Streams With Bill Bejeck | Cur...
HostedbyConfluent
 
Getting Started with Confluent Schema Registry
confluent
 
Kafka
shrenikp
 
Microservices Docker Kubernetes Istio Kanban DevOps SRE
Araf Karsh Hamid
 
Securing Kafka
confluent
 
Schema Registry 101 with Bill Bejeck | Kafka Summit London 2022
HostedbyConfluent
 
Introducing Vault
Ramit Surana
 
Apache Kafka - Martin Podval
Martin Podval
 
Introduction to Vault
Knoldus Inc.
 
Managing multiple event types in a single topic with Schema Registry | Bill B...
HostedbyConfluent
 
ksqlDB - Stream Processing simplified!
Guido Schmutz
 
Credential store using HashiCorp Vault
Mayank Patel
 
Fundamentals of Apache Kafka
Chhavi Parasher
 

Similar to Walking through the Spring Stack for Apache Kafka with Soby Chacko | Kafka Summit London 2022 (20)

PDF
Testing Kafka components with Kafka for JUnit
Markus Günther
 
PDF
Event Streaming with Kafka Streams and Spring Cloud Stream | Soby Chacko, VMware
HostedbyConfluent
 
PDF
Apache Kafka - Scalable Message Processing and more!
Guido Schmutz
 
PDF
Apache Kafka - Scalable Message-Processing and more !
Guido Schmutz
 
PDF
Virtual Bash! A Lunchtime Introduction to Kafka
Jason Bell
 
PDF
Apache Kafka Scalable Message Processing and more!
Guido Schmutz
 
PDF
What is Apache Kafka and What is an Event Streaming Platform?
confluent
 
PDF
Apache Kafka - Scalable Message-Processing and more !
Guido Schmutz
 
PPTX
Real-time streaming and data pipelines with Apache Kafka
Joe Stein
 
PPTX
Introduction Apache Kafka
Joe Stein
 
PPTX
Apache kafka
Kumar Shivam
 
PDF
Developing Realtime Data Pipelines With Apache Kafka
Joe Stein
 
PDF
Building High-Throughput, Low-Latency Pipelines in Kafka
confluent
 
PPTX
Being Ready for Apache Kafka - Apache: Big Data Europe 2015
Michael Noll
 
PDF
Kafka 101 and Developer Best Practices
confluent
 
PDF
Event streaming: A paradigm shift in enterprise software architecture
Sina Sojoodi
 
PPTX
Westpac Bank Tech Talk 1: Dive into Apache Kafka
confluent
 
PPTX
Apache Kafka 0.8 basic training - Verisign
Michael Noll
 
PDF
A la rencontre de Kafka, le log distribué par Florian GARCIA
La Cuisine du Web
 
PDF
Kafka Deep Dive
Knoldus Inc.
 
Testing Kafka components with Kafka for JUnit
Markus Günther
 
Event Streaming with Kafka Streams and Spring Cloud Stream | Soby Chacko, VMware
HostedbyConfluent
 
Apache Kafka - Scalable Message Processing and more!
Guido Schmutz
 
Apache Kafka - Scalable Message-Processing and more !
Guido Schmutz
 
Virtual Bash! A Lunchtime Introduction to Kafka
Jason Bell
 
Apache Kafka Scalable Message Processing and more!
Guido Schmutz
 
What is Apache Kafka and What is an Event Streaming Platform?
confluent
 
Apache Kafka - Scalable Message-Processing and more !
Guido Schmutz
 
Real-time streaming and data pipelines with Apache Kafka
Joe Stein
 
Introduction Apache Kafka
Joe Stein
 
Apache kafka
Kumar Shivam
 
Developing Realtime Data Pipelines With Apache Kafka
Joe Stein
 
Building High-Throughput, Low-Latency Pipelines in Kafka
confluent
 
Being Ready for Apache Kafka - Apache: Big Data Europe 2015
Michael Noll
 
Kafka 101 and Developer Best Practices
confluent
 
Event streaming: A paradigm shift in enterprise software architecture
Sina Sojoodi
 
Westpac Bank Tech Talk 1: Dive into Apache Kafka
confluent
 
Apache Kafka 0.8 basic training - Verisign
Michael Noll
 
A la rencontre de Kafka, le log distribué par Florian GARCIA
La Cuisine du Web
 
Kafka Deep Dive
Knoldus Inc.
 

More from HostedbyConfluent (20)

PDF
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
HostedbyConfluent
 
PDF
Renaming a Kafka Topic | Kafka Summit London
HostedbyConfluent
 
PDF
Evolution of NRT Data Ingestion Pipeline at Trendyol
HostedbyConfluent
 
PDF
Ensuring Kafka Service Resilience: A Dive into Health-Checking Techniques
HostedbyConfluent
 
PDF
Exactly-once Stream Processing with Arroyo and Kafka
HostedbyConfluent
 
PDF
Fish Plays Pokemon | Kafka Summit London
HostedbyConfluent
 
PDF
Tiered Storage 101 | Kafla Summit London
HostedbyConfluent
 
PDF
Building a Self-Service Stream Processing Portal: How And Why
HostedbyConfluent
 
PDF
From the Trenches: Improving Kafka Connect Source Connector Ingestion from 7 ...
HostedbyConfluent
 
PDF
Future with Zero Down-Time: End-to-end Resiliency with Chaos Engineering and ...
HostedbyConfluent
 
PDF
Navigating Private Network Connectivity Options for Kafka Clusters
HostedbyConfluent
 
PDF
Apache Flink: Building a Company-wide Self-service Streaming Data Platform
HostedbyConfluent
 
PDF
Explaining How Real-Time GenAI Works in a Noisy Pub
HostedbyConfluent
 
PDF
TL;DR Kafka Metrics | Kafka Summit London
HostedbyConfluent
 
PDF
A Window Into Your Kafka Streams Tasks | KSL
HostedbyConfluent
 
PDF
Mastering Kafka Producer Configs: A Guide to Optimizing Performance
HostedbyConfluent
 
PDF
Data Contracts Management: Schema Registry and Beyond
HostedbyConfluent
 
PDF
Code-First Approach: Crafting Efficient Flink Apps
HostedbyConfluent
 
PDF
Debezium vs. the World: An Overview of the CDC Ecosystem
HostedbyConfluent
 
PDF
Beyond Tiered Storage: Serverless Kafka with No Local Disks
HostedbyConfluent
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
HostedbyConfluent
 
Renaming a Kafka Topic | Kafka Summit London
HostedbyConfluent
 
Evolution of NRT Data Ingestion Pipeline at Trendyol
HostedbyConfluent
 
Ensuring Kafka Service Resilience: A Dive into Health-Checking Techniques
HostedbyConfluent
 
Exactly-once Stream Processing with Arroyo and Kafka
HostedbyConfluent
 
Fish Plays Pokemon | Kafka Summit London
HostedbyConfluent
 
Tiered Storage 101 | Kafla Summit London
HostedbyConfluent
 
Building a Self-Service Stream Processing Portal: How And Why
HostedbyConfluent
 
From the Trenches: Improving Kafka Connect Source Connector Ingestion from 7 ...
HostedbyConfluent
 
Future with Zero Down-Time: End-to-end Resiliency with Chaos Engineering and ...
HostedbyConfluent
 
Navigating Private Network Connectivity Options for Kafka Clusters
HostedbyConfluent
 
Apache Flink: Building a Company-wide Self-service Streaming Data Platform
HostedbyConfluent
 
Explaining How Real-Time GenAI Works in a Noisy Pub
HostedbyConfluent
 
TL;DR Kafka Metrics | Kafka Summit London
HostedbyConfluent
 
A Window Into Your Kafka Streams Tasks | KSL
HostedbyConfluent
 
Mastering Kafka Producer Configs: A Guide to Optimizing Performance
HostedbyConfluent
 
Data Contracts Management: Schema Registry and Beyond
HostedbyConfluent
 
Code-First Approach: Crafting Efficient Flink Apps
HostedbyConfluent
 
Debezium vs. the World: An Overview of the CDC Ecosystem
HostedbyConfluent
 
Beyond Tiered Storage: Serverless Kafka with No Local Disks
HostedbyConfluent
 

Recently uploaded (20)

PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
PDF
Biography of Daniel Podor.pdf
Daniel Podor
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PDF
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
PDF
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
PDF
Blockchain Transactions Explained For Everyone
CIFDAQ
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PDF
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
Biography of Daniel Podor.pdf
Daniel Podor
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
Blockchain Transactions Explained For Everyone
CIFDAQ
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 

Walking through the Spring Stack for Apache Kafka with Soby Chacko | Kafka Summit London 2022

  • 1. Confidential │ ©2021 VMware, Inc. Walking through the Spring Stack for Apache Kafka Kafka Summit London April 25-26, 2022
  • 2. Confidential │ ©2022 VMware, Inc. 2 About me... Soby Chacko Committer - Spring Cloud Stream/Spring Cloud Data Flow at VMware Tech lead for Spring Cloud Stream Kafka/Kafka Streams binders Twitter: @sobychacko Github: github.com/sobychacko
  • 3. Confidential │ ©2022 VMware, Inc. 3 Agenda ● Comprehensive look at the Apache Kafka ecosystem in Spring ● Demo Applications ● Questions
  • 4. Confidential │ ©2022 VMware, Inc. 4 Spring Stack for Apache Kafka Spring Boot Spring for Apache Kafka Spring Integration Kafka Spring Cloud Stream Binders for Kafka and Kafka Streams Reactor Kafka Apache Kafka Java Client / Kafka Streams Client
  • 5. Confidential │ ©2022 VMware, Inc. 5 Spring for Apache Kafka - Top Level Ideas ➔ Foundational library for Apache Kafka in Spring ➔ Spring friendly programming abstractions ➔ Many features that allow developers to focus on the business logic ➔ Provides abstractions for low-level infrastructure concerns ➔ Leverages and builds upon Apache Kafka Java Client / Kafka Streams
  • 6. Confidential │ ©2022 VMware, Inc. 6 Spring for Apache Kafka - Topic Management @Bean public KafkaAdmin admin() { Map<String, Object> configs = new HashMap<>(); configs.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092"); return new KafkaAdmin(configs); } @Bean public NewTopic myTopic() { return TopicBuilder.name("my-topic") .partitions(10) .replicas(3) .compact() .build(); } @Bean public KafkaAdmin.NewTopics bunchOfTopics() { return new KafkaAdmin.NewTopics( TopicBuilder.name("topic1") .build(), TopicBuilder.name("topic2") .replicas(1) .build(), TopicBuilder.name("topic3") .partitions(3) .build()); }
  • 7. Confidential │ ©2022 VMware, Inc. 7 Publishing Records using KafkaTemplate API KafkaTemplate provides a wide variety of template methods to publish records ListenableFuture<SendResult<K, V>> sendDefault(K key, V data); ListenableFuture<SendResult<K, V>> send(String topic, K key, V data); ListenableFuture<SendResult<K, V>> send(String topic, Integer partition, K key, V data);
  • 8. Confidential │ ©2022 VMware, Inc. 8 Async Send Example ListenableFuture<SendResult<Integer, String>> future = template.send("topic", 1, "data"); future.addCallback(new KafkaSendCallback<Integer, String>() { @Override public void onSuccess(SendResult<Integer, String> result) {...} @Override public void onFailure(KafkaProducerException ex) { ProducerRecord<Integer, String> failed = ex.getFailedProducerRecord(); ... } });
  • 9. Confidential │ ©2022 VMware, Inc. 9 Producer Factory ● KafkaTemplate uses the producer factory for creating the producer ● Spring Boot auto-configures a producer factory bean ● Applications can provide custom producer factories @Bean public ProducerFactory<?, ?> kafkaProducerFactory() { DefaultKafkaProducerFactory<?, ?> factory = new DefaultKafkaProducerFactory<>( Map.of(...)) … return factory; }
  • 10. Confidential │ ©2022 VMware, Inc. 10 Consuming Records using KafkaListener ● @KafkaListener annotation provides a lot of flexible options to consume records from Kafka topics ● @EnableKafka annotation @KafkaListener(id = "my-group", topics = "my-topic") public void listen(String in) { logger.info("Data Received : " + in); } Basic KafkaListener
  • 11. Confidential │ ©2022 VMware, Inc. 11 KafkaListener with More Options See the reference docs for all the available options of KafkaListener. @KafkaListener(id = "my-group", topicPartitions = @TopicPartition(topic = "my-topic", partitionOffsets = { @PartitionOffset(partition = "0-2", initialOffset = "0"), @PartitionOffset(partition = "6-9", initialOffset = "0")}), concurrency = ${config.concurrency} properties = "key.deserializer:LongDeserializer") public void listen(String in, @Header(KafkaHeaders.RECEIVED_MESSAGE_KEY) Long key, @Header(KafkaHeaders.RECEIVED_PARTITION_ID) int part, @Header(KafkaHeaders.OFFSET) int offset) { logger.info( "Data Received : {} with key {} from partition {} and offset {}.", in, key,part,offset); }
  • 12. Confidential │ ©2022 VMware, Inc. 12 KafkaListener on a Class @KafkaListener(id = "multi", topics = "myTopic") static class MultiListenerBean { @KafkaHandler public void listen1(String foo) { ... } @KafkaHandler public void listen2(Integer bar) { ... } @KafkaHandler(isDefault = true) public void listenDefault(Object object) { ... } }
  • 13. Confidential │ ©2022 VMware, Inc. 13 MessageListenerContainer ● Behind the scenes, KafkaListener uses a MessageListener Container for accomplishing various tasks such as polling, committing, error handling etc. ● KafkaMessageListenerContainer - Single listener thread ● ConcurrentKafkaMessageListenerContainer - Multiple listeners ● Spring Boot auto-configures a factory for creating a message listener container
  • 14. Confidential │ ©2022 VMware, Inc. 14 MessageListeners ● Spring for Apache Kafka provides various types of message listeners ● Record and Batch based Message Listeners ● onMessage(...) method that gives access to the consumer record ● You can provide your own implementation to the container ● MessageListener ● AcknowledgingMessageListener ● ConsumerAwareMessageListener ● AcknowledgingConsumerAwareMessageListener ● BatchMessageListener ● BatchAcknowledgingMessageListener ● BatchConsumerAwareMessageListener ● BatchAcknowledgingConsumerAwareMessageListener
  • 15. Confidential │ ©2022 VMware, Inc. 15 Committing the Offsets ● By default, enable.auto.commit is disabled in Spring for Apache Kafka and the framework is responsible for committing the offsets ● A number of options are available for commits which the applications can configure on the container through the AckMode property of ContainerProperties – RECORD, BATCH, TIME, COUNT, MANUAL, MANUAL_IMMEDIATE ● Default AckMode is the BATCH
  • 16. Confidential │ ©2022 VMware, Inc. 16 KafkaListener Example of Manually Acknowledging @KafkaListener(id = "cat", topics = "myTopic", containerFactory = "kafkaManualAckListenerContainerFactory") public void listen(String data, Acknowledgment ack) { … ack.acknowledge(); }
  • 17. Confidential │ ©2022 VMware, Inc. 17 Seeking Offsets ● Listener must implement the ConsumerSeekAware interface ● Framework provides a convenient AbstractConsumerSeekAware ● Using the callbacks, the application can do arbitrary seeks such as seekToBeginning, seekToEnd, seekToRelative, seekToTimestamp etc. void registerSeekCallback(ConsumerSeekCallback callback); void onPartitionsAssigned(Map<TopicPartition, Long> assignments, ConsumerSeekCallback callback); void onPartitionsRevoked(Collection<TopicPartition> partitions); void onIdleContainer(Map<TopicPartition, Long> assignments, ConsumerSeekCallback callback);
  • 18. Confidential │ ©2022 VMware, Inc. 18 Container Error Handling ● Container Error Handler - CommonErrorHandler interface ● Default implementation - DefaultErrorHandler ● By default, DefaultErrorHandler provides 10 max attempts and no backoff ● You can provide a custom bean that overrides these defaults @Bean public DefaultErrorHandler errorHandler( DeadletterPublishingRecoverer recoverer) { DefaultErrorHandler handler = new DefaultErrorHandler(recoverer, new FixedBackOff(2_000, 2)); handler.addNotRetryableExceptions(IllegalArgumentException.class); return handler; }
  • 19. Confidential │ ©2022 VMware, Inc. 19 ErrorHandlingDeserializer ● Convenient way to catch deserialization errors ● ErrorHandlingDeserializer has delegates to the actual key/value deserializers ● When deserialization fails, the proper headers are populated with the exception and the raw data that failed ● Then normal container error handling rules apply
  • 20. Confidential │ ©2022 VMware, Inc. 20 Extensive Error Handling Options ● Spring for Apache Kafka provides much more features, options and flexibility for error handling. See the reference documentation for more information. https://docs.spring.io/spring-kafka/docs/current/reference/html/#annotation-error-handling ● Non Blocking Retries - A feature in active development. See the documentation section for more details. https://docs.spring.io/spring-kafka/docs/current/reference/html/#retry-topic
  • 21. Confidential │ ©2022 VMware, Inc. 21 Testing using EmbeddedKafka Broker ● spring-kafka-test provides a convenient way to test with an embedded broker ● EmbeddedKafka annotation and EmbeddedKafkaBroker for Spring test context with JUnit 5 ● For non-spring test context, use the EmbeddedKafkaCondition with JUnit 5 ● For JUnit4, use EmbeddedKafkaRule, which is a JUnit4 ClassRule ● More details at: https://docs.spring.io/spring-kafka/docs/current/reference/html/#embedded-kafka-annotation @SpringBootTest @EmbeddedKafka(topics = "my-topic", bootstrapServersProperty = "spring.kafka.bootstrap-servers") class SpringKafkaApp1Tests { @Test void test(EmbeddedKafkaBroker broker) { // ... } }
  • 22. Confidential │ ©2022 VMware, Inc. 22 Advanced Spring for Apache Kafka features ● Request/Reply Semantics - https://docs.spring.io/spring-kafka/docs/current/reference/html/#replyi ng-template ● Transactions - https://docs.spring.io/spring-kafka/docs/current/reference/html/#trans actions ● Kafka Streams Support - https://docs.spring.io/spring-kafka/docs/current/reference/html/#strea ms-kafka-streams
  • 23. Confidential │ ©2022 VMware, Inc. 23 Spring Integration Overview ● Messages and Channels ● Integration Flows - Spring Integration is used to create complex EIP flows - Kafka flows can tap into larger flows ● Provides Java Config and DSL
  • 24. Confidential │ ©2022 VMware, Inc. 24 Spring Integration Kafka Support ● Outbound Channel Adapter – KafkaProducerMessageHandler ● Kafka Message Driven Channel Adapter ● Inbound Channel Adapter - Provides a KafkaMessageSource which is a pollable channel adapter implementation. ● Outbound Gateway - for request/reply operations ● Inbound Gateway - for request/reply operations See more details in the reference docs for Spring Integration at https://docs.spring.io/spring-integration/docs/current/reference/html/kafka.html#kafka
  • 25. Confidential │ ©2022 VMware, Inc. 25 Spring Cloud Stream Overview ● General purpose framework for writing event driven/stream processing micro services ● Programming model based on Spring Cloud Function - Java 8 Functions/Function Composition ● Apache Kafka support is built on top of Spring Boot, Spring for Apache Kafka, Spring Integration and Kafka Streams
  • 26. Confidential │ ©2022 VMware, Inc. 26 Spring Cloud Stream Kafka Application Architecture Spring Boot Application Spring Cloud Stream App Core Kafka/Kafka Streams Binder Inputs (Message Channel/Kafka Streams) Outputs(Message Channel/Kafka Streams) Kafka Broker Spring Integration/Kafka Streams Spring for Apache Kafka
  • 27. Confidential │ ©2022 VMware, Inc. 27 Spring Cloud Stream - Kafka Binder ● Auto provisioning of topics ● Producer/Consumer Binding ● Message Converters/Native Serialization ● DLQ Support ● Health checks for the binder
  • 28. Confidential │ ©2022 VMware, Inc. 28 Spring Cloud Stream Kafka Binder Programming Model @SpringBootApplication … @Bean public Supplier<Long> currentTime() { return System::currentTimeMillis; } @Bean public Function<Long, String> convertToUTC() { return localTime -> formatUTC(); } @Bean public Consumer<String> print() { System.out::println; }
  • 29. Confidential │ ©2022 VMware, Inc. 29 Spring Cloud Stream - Kafka Streams Binder ● Built on the Kafka Streams foundations from Spring for Apache Kafka - StreamsBuilderFactoryBean ● Kafka Streams processors written as Java functions ● KStream, KTable and GlobalKTable bindings ● Serde inference on input/output ● Interactive Query Support ● Multiple functions in the same application ● Function composition
  • 30. Confidential │ ©2022 VMware, Inc. 30 Examples of Spring Cloud Stream Kafka Streams Functions @Bean public Function<KStream<Object, String>, KStream<String, Long>> wordCount() { return input -> input .flatMapValues(value -> Arrays.asList(value.toLowerCase().split("W+"))) .map((key, value) -> new KeyValue<>(value, value)) .groupByKey() .windowedBy(TimeWindows.of(Duration.ofSeconds(WINDOW_SIZE_SECONDS))) .count() .toStream() .map((key, value) -> new KeyValue<>(key.key(), value)); } @Bean public Consumer<KStream<Object, Long>> logWordCounts() { return kstream -> kstream.foreach((key, value) -> { logger.info(String.format("Word counts for: %s t %d", key, value)); }); }
  • 31. Confidential │ ©2022 VMware, Inc. 31 Resources Spring for Apache Kafka Project Site Reference Docs GitHub StackOverflow Spring Integration Kafka Project Site Reference Docs GitHub StackOverflow Spring Cloud Stream Project Site Reference Docs GitHub StackOverflow Spring Boot Kafka Docs
  • 32. Confidential │ ©2021 VMware, Inc. 32 Demo https://github.com/schacko-samples/kafka-summit-london-2022
  • 33. Confidential │ ©2021 VMware, Inc. 33