Skip to content

Instantly share code, notes, and snippets.

View schroedermatt's full-sized avatar

Matt Schroeder schroedermatt

View GitHub Profile
@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 / 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 / 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 / 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 / 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 / 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 / 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 / BackoffPolicies.txt
Created November 16, 2018 04:21
A list of the available Spring Retry backoff policies.
ExponentialBackOffPolicy: increases back off period exponentially. The initial interval and multiplier are configurable.
ExponentialRandomBackoffPolicy: chooses a random multiple of the interval that would come from a simple deterministic exponential. This has shown to at least be useful in testing scenarios where excessive contention is generated by the test needing many retries.
FixedBackOffPolicy: pauses for a fixed period of time (using Sleeper.sleep(long)) before continuing.
NoBackOffPolicy: performs all retries one after the other without pause
UniformRandomBackOffPolicy: pauses for random period of time before continuing
@schroedermatt
schroedermatt / RetryPolicies.txt
Last active November 16, 2018 04:22
A list of the available retry policies.
AlwaysRetryPolicy: A subclass of NeverRetryPolicy that is mainly used as a base for other policies (e.g. a test stub)
CircuitBreakerRetryPolicy: Trips circuit open after a given number of failures and stays open until a set timeout elapses
CompositeRetryPolicy: mix and match multiple policies (they will be called in the order given)
ExceptionClassifierRetryPolicy: specify different policies for different exception types
ExpressionRetryPolicy: subclass of SimpleRetryPolicy that evaluates an expression against the last thrown exception
NeverRetryPolicy: allows the first attempt but never permits a retry
SimpleRetryPolicy: retry a fixed number of times for a set of named exceptions (and subclasses)
TimeoutRetryPolicy: allows a retry as long as it hasn’t timed out (clock is started on a call to RetryContext)
@schroedermatt
schroedermatt / RetryConfig.groovy
Last active November 16, 2018 15:13
A snippet showing how to configure a RetryTemplate on a KafkaListenerContainer
@Bean
public KafkaListenerContainerFactory kafkaListenerContainerFactory(RetryTemplate retryTemplate) {
def factory = /** configure factory **/
// configure the listener container factory with retry support
factory.setRetryTemplate(retryTemplate);
factory.setRecoveryCallback(context -> {
log.error("RetryPolicy limit has been exceeded! You should really handle this better.");
return null;
});