Spring Cloud Stream & Kafka Retry

public class RetryableKafkaListener {

@RetryableTopic(
attempts = "4",
backoff = @Backoff(delay = 1000, multiplier = 2.0),
autoCreateTopics = "false",
topicSuffixingStrategy = TopicSuffixingStrategy.SUFFIX_WITH_INDEX_VALUE)
@KafkaListener(topics = "orders")
public void listen(String in, @Header(KafkaHeaders.RECEIVED_TOPIC) String topic) {
log.info(in + " from " + topic);
throw new RuntimeException("test");
}

@DltHandler
public void dlt(String in, @Header(KafkaHeaders.RECEIVED_TOPIC) String topic) {
log.info(in + " from " + topic);
}
}

Cloud stream is like any other Spring concept, says, talk abstract to me and I will figure out the implementation.

Think of it as Inversion of Control for message brokers.

spring:
cloud:
stream:
default:
group: your-group
consumer:
partitioned: true
concurrency: 1
kafka:
binder:
configuration:
max.poll.interval.ms: 300000
max.poll.records: 300
brokers: 127.0.0.1
defaultBrokerPort: 9092
autoCreateTopics: true
autoAddPartitions: true
replicationFactor: 1
function:
definition: yourFunction
bindings:
yourFunction-in-0:
destination: your.topic.name
function:
definition: yourFunction
bindings:
yourFunction-in-0:
destination: your.topic.name
consumer:
maxAttempts: 3
backOffInitialInterval: 150
backOffMaxInterval: 1050
backOffMultiplier: 2.0
defaultRetryable: true
retryable-exceptions:
com.company.appname.exception.ConsumerRetryException: true
com.company.appname.exception.OtherException: false

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store