Skip to content

Instantly share code, notes, and snippets.

View kirshiyin89's full-sized avatar

Kirshi Yin kirshiyin89

View GitHub Profile
@kirshiyin89
kirshiyin89 / RandomNumberGenerator.java
Created November 18, 2022 17:43
Random number generator method
public static int generateNumberFromRange(int maxRange) throws NoSuchAlgorithmException {
Random rand = SecureRandom.getInstanceStrong();
int randomNum;
if (maxRange == 100) {
randomNum = rand.nextInt(maxRange / 2) * 2;
} else {
randomNum = rand.nextInt(maxRange / 2) * 2 + 1;
}
return randomNum;
}
@SpringBootApplication
public class KafkaErrorHandlingApplication {
public static void main(String[] args) {
SpringApplication.run(KafkaErrorHandlingApplication.class, args);
}
}
@kirshiyin89
kirshiyin89 / ProducerController.java
Created October 25, 2022 12:40
produce messages
@RestController
@RequiredArgsConstructor
@Slf4j
public class ProducerController {
private final KafkaTemplate<String, String> kafkaTemplate;
@Value("${topic}")
private String topic;
@kirshiyin89
kirshiyin89 / MyKafkaListener.java
Created October 25, 2022 12:28
the kafka listener implementation
@Component
@Slf4j
public class MyKafkaListener {
@RetryableTopic(
attempts = "5",
topicSuffixingStrategy = TopicSuffixingStrategy.SUFFIX_WITH_INDEX_VALUE,
backoff = @Backoff(delay = 1000, multiplier = 2.0),
exclude = {SerializationException.class, DeserializationException.class}
@kirshiyin89
kirshiyin89 / application.yml
Created October 25, 2022 12:24
app properties
topic: my-topic
server:
port: 8090
spring:
kafka:
bootstrap-servers: http://localhost:29092
properties:
spring.deserializer.value.delegate.class: org.springframework.kafka.support.serializer.JsonDeserializer
security.protocol: PLAINTEXT
@kirshiyin89
kirshiyin89 / docker-compose.yml
Created October 25, 2022 12:22
kafka docker compose
version: '2.1'
networks:
app-tier:
driver: bridge
services:
zookeeper:
image: confluentinc/cp-zookeeper:latest
environment:
ZOOKEEPER_CLIENT_PORT: 2181
@kirshiyin89
kirshiyin89 / pom.xml
Created October 25, 2022 12:19
dependencies for kafka retryabletopic
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
<dependency>
@kirshiyin89
kirshiyin89 / KafkaConsumerConfiguration.java
Created October 25, 2022 12:15
Example KafkaConsumerConfiguration for FixedBackOff strategy
@Configuration
public class KafkaConsumerConfiguration {
@Bean
ConcurrentKafkaListenerContainerFactory<String, String> kafkaBlockingRetryContainerFactory() {
ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>();
factory.setConsumerFactory(consumerFactory);
factory.setCommonErrorHandler(new DefaultErrorHandler(
new DeadLetterPublishingRecoverer(template), new FixedBackOff(5000, 3))
);
@kirshiyin89
kirshiyin89 / services.json
Created October 18, 2022 13:08
Service data response
[
{
"id":"1",
"serviceName":"SERVICE1",
"serviceCase":"TEST1",
"serviceClient":null
},
{
"id":"12",
"serviceName":"SERVICE12",
@kirshiyin89
kirshiyin89 / health.json
Last active October 11, 2022 11:55
Health information with details
{
"status":"UP",
"components":{
"db":{
"status":"UP",
"details":{
"database":"H2",
"validationQuery":"isValid()"
}
},