@@ -134,11 +134,13 @@ public class KafkaMessageListenerContainerTests {
134
134
135
135
private static String topic18 = "testTopic18" ;
136
136
137
+ private static String topic19 = "testTopic19" ;
138
+
137
139
138
140
@ ClassRule
139
141
public static KafkaEmbedded embeddedKafka = new KafkaEmbedded (1 , true , topic1 , topic2 , topic3 , topic4 , topic5 ,
140
142
topic6 , topic7 , topic8 , topic9 , topic10 , topic11 , topic12 , topic13 , topic14 , topic15 , topic16 , topic17 ,
141
- topic18 );
143
+ topic18 , topic19 );
142
144
143
145
@ Rule
144
146
public TestName testName = new TestName ();
@@ -1718,6 +1720,69 @@ public void testInitialSeek() throws Exception {
1718
1720
container .stop ();
1719
1721
}
1720
1722
1723
+ @ Test
1724
+ public void testExceptionWhenCommitAfterRebalance () throws Exception {
1725
+ final CountDownLatch rebalanceLatch = new CountDownLatch (2 );
1726
+ final CountDownLatch consumeLatch = new CountDownLatch (7 );
1727
+
1728
+ Map <String , Object > props = KafkaTestUtils .consumerProps ("test19" , "false" , embeddedKafka );
1729
+ props .put (ConsumerConfig .AUTO_OFFSET_RESET_CONFIG , "latest" );
1730
+ props .put (ConsumerConfig .MAX_POLL_INTERVAL_MS_CONFIG , 15000 );
1731
+ DefaultKafkaConsumerFactory <Integer , String > cf = new DefaultKafkaConsumerFactory <>(props );
1732
+ ContainerProperties containerProps = new ContainerProperties (topic19 );
1733
+ containerProps .setMessageListener ((MessageListener <Integer , String >) messages -> {
1734
+ logger .info ("listener: " + messages );
1735
+ consumeLatch .countDown ();
1736
+ try {
1737
+ Thread .sleep (3000 );
1738
+ }
1739
+ catch (InterruptedException e ) {
1740
+ e .printStackTrace ();
1741
+ }
1742
+ });
1743
+ containerProps .setSyncCommits (true );
1744
+ containerProps .setAckMode (AckMode .BATCH );
1745
+ containerProps .setPollTimeout (100 );
1746
+ containerProps .setAckOnError (false );
1747
+ containerProps .setErrorHandler (new SeekToCurrentErrorHandler ());
1748
+
1749
+ Map <String , Object > senderProps = KafkaTestUtils .producerProps (embeddedKafka );
1750
+ ProducerFactory <Integer , String > pf = new DefaultKafkaProducerFactory <>(senderProps );
1751
+ KafkaTemplate <Integer , String > template = new KafkaTemplate <>(pf );
1752
+ template .setDefaultTopic (topic19 );
1753
+
1754
+ containerProps .setConsumerRebalanceListener (new ConsumerRebalanceListener () {
1755
+
1756
+ @ Override
1757
+ public void onPartitionsRevoked (Collection <TopicPartition > partitions ) {
1758
+ }
1759
+
1760
+ @ Override
1761
+ public void onPartitionsAssigned (Collection <TopicPartition > partitions ) {
1762
+ logger .info ("rebalance occurred." );
1763
+ rebalanceLatch .countDown ();
1764
+ }
1765
+ });
1766
+
1767
+ KafkaMessageListenerContainer <Integer , String > container =
1768
+ new KafkaMessageListenerContainer <>(cf , containerProps );
1769
+ container .setBeanName ("testContainerException" );
1770
+ container .start ();
1771
+ ContainerTestUtils .waitForAssignment (container , embeddedKafka .getPartitionsPerTopic ());
1772
+ container .pause ();
1773
+
1774
+ for (int i = 0 ; i < 6 ; i ++) {
1775
+ template .sendDefault (0 , 0 , "a" );
1776
+ }
1777
+ template .flush ();
1778
+
1779
+ container .resume ();
1780
+ // should be rebalanced and consume again
1781
+ assertThat (rebalanceLatch .await (60 , TimeUnit .SECONDS )).isTrue ();
1782
+ assertThat (consumeLatch .await (60 , TimeUnit .SECONDS )).isTrue ();
1783
+ container .stop ();
1784
+ }
1785
+
1721
1786
private Consumer <?, ?> spyOnConsumer (KafkaMessageListenerContainer <Integer , String > container ) {
1722
1787
Consumer <?, ?> consumer = spy (
1723
1788
KafkaTestUtils .getPropertyValue (container , "listenerConsumer.consumer" , Consumer .class ));
0 commit comments