In my Spring Boot application I implemented the following error handling logic when processing event from a Kafka topic:
- Retry failed messages defined number of times.
- Stop MessageListenerContainer if message couldn't be processed after X number of attempts.
To stop the container I use the approach described in this answer: https://stackoverflow.com/a/67077461/4800887
But in short the container is stopped like this:
new CommonContainerStoppingErrorHandler().handleOtherException(ex, null, registry.getListenerContainer(listenerContainerId), false);
Now I'm trying to test this functionality in my @SpringBootTest
test. For that I'm checking container running status like this:
var container = kafkaListenerEndpointRegistry.getListenerContainer(listenerContainerId);
assertThat(container.isRunning()).isFalse();
This works fine. But the issue I'm having is in the other test which is trying to read current offset and send a message to the same EmbeddedKafkaBroker
.
The error I see in the logs is this:
o.a.k.c.c.internals.ConsumerCoordinator : [Consumer clientId=consumer-domain_consumer_test-1, groupId=domain_consumer_test] Synchronous auto-commit of offsets
{my-topic-0=OffsetAndMetadata{offset=0, leaderEpoch=null, metadata=''}} failed: Offset
commit cannot be completed since the consumer is not part of an active group for auto
partition assignment; it is likely that the consumer was kicked out of the group.
My question is this: how I can restart normal container operation after it was stopped the way I showed above, so that other tests in the same class can operate normally? Or maybe I can restart EmbeddedKafkaBroker
itself, that would be fine as well.
I tried to use advice from this answer: https://stackoverflow.com/a/64157233/4800887
embeddedKafkaBroker.destroy();
embeddedKafkaBroker.afterPropertiesSet();
But that doesn't seem to be working.