Skip to content

Instantly share code, notes, and snippets.

View schroedermatt's full-sized avatar

Matt Schroeder schroedermatt

View GitHub Profile
@schroedermatt
schroedermatt / SpringRetry.groovy
Last active November 16, 2018 15:06
A simple Spring Retry configuration example
@Bean
RetryTemplate retryTemplate() {
RetryTemplate retryTemplate = new RetryTemplate()
FixedBackOffPolicy fixedBackOffPolicy = new FixedBackOffPolicy()
fixedBackOffPolicy.setBackOffPeriod(1000l)
retryTemplate.setBackOffPolicy(fixedBackOffPolicy)
SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy()
retryPolicy.setMaxAttempts(2)
@schroedermatt
schroedermatt / build.gradle
Last active December 27, 2019 17:58
Spotless Configuration
plugins {
id 'java'
id 'com.diffplug.gradle.spotless' version '3.26.1'
// other plugins
}
repositories { .. }
dependencies { .. }
@schroedermatt
schroedermatt / SpringContext.java
Created January 7, 2020 22:04
Spring utility class to bridge between Spring and Non-Spring things.
@Component
public class SpringContext implements ApplicationContextAware {
private static ApplicationContext context;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
context = applicationContext;
}
public static <T> T getBean(Class<T> beanClass) {
@schroedermatt
schroedermatt / CustomExceptionHandler.java
Created January 7, 2020 22:05
A custom implementation of a Kafka Streams ProductionExceptionHandler that uses a SpringContext bridge to access beans.
public class CustomExceptionHandler implements ProductionExceptionHandler {
private ExceptionService exceptionService;
public ProductionExceptionHandlerResponse handle(final ProducerRecord<byte[], byte[]> record, final Exception exception) {
boolean shouldContinue = exceptionService.handleProductionException(record, exception);
return shouldContinue ? CONTINUE : FAIL;
}
@schroedermatt
schroedermatt / UncaughtExceptionHandlerExample.java
Created February 13, 2020 14:25
Example showing how to configure the uncaught exception handler.
KafkaStreams streams = new KafkaStreams(topology, config);
// other configurations
streams.setUncaughtExceptionHandler((Thread thread, Throwable throwable) -> {
   // examine the throwable and do something
});
streams.start();
@schroedermatt
schroedermatt / ContainerKafkaTestResource.java
Created November 11, 2021 17:09
Dockerized Quarkus Test Resource
public class ContainerKafkaTestResource implements QuarkusTestResourceLifecycleManager {
private static final KafkaContainer kafka = new KafkaContainer();
/**
* @return A map of system properties that should be set for the running tests
*/
@Override
public Map<String, String> start() {
kafka.start();
@schroedermatt
schroedermatt / QuarkusContainerKafkaTest.java
Created November 11, 2021 17:10
Custom annotation for ContainerKafkaTestResource Quarkus tests
@QuarkusTest
@QuarkusTestResource(ContainerKafkaTestResource.class)
@Stereotype
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@DisabledIfSystemProperty(named = "container-enabled", matches = "false")
public @interface QuarkusContainerKafkaTest {}
@schroedermatt
schroedermatt / ExampleTest.java
Created November 11, 2021 17:11
ExampleTest using QuarkusContainerKafkaTest annotation
@QuarkusContainerKafkaTest
public class ExampleTest {
...
}
@schroedermatt
schroedermatt / EmbeddedKafkaTestResource.java
Created November 11, 2021 17:13
EmbeddedKafkaTestResource for Quarkus tests
import org.springframework.kafka.test.EmbeddedKafkaBroker;
public class EmbeddedKafkaTestResource implements QuarkusTestResourceLifecycleManager {
public EmbeddedKafkaBroker embeddedBroker;
/**
* @return A map of system properties that should be set for the running test
*/
@Override
@schroedermatt
schroedermatt / QuarkusEmbeddedKafkaTest.java
Created November 11, 2021 17:14
Custom annotation for QuarkusEmbeddedKafkaTest
@QuarkusTest
@QuarkusTestResource(EmbeddedKafkaTestResource.class)
@Stereotype
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface QuarkusEmbeddedKafkaTest {}