18
18
19
19
import static org .assertj .core .api .Assertions .assertThat ;
20
20
21
+ import java .util .ArrayList ;
21
22
import java .util .List ;
22
23
import java .util .Map ;
23
24
import java .util .concurrent .CountDownLatch ;
24
25
import java .util .concurrent .TimeUnit ;
25
26
import java .util .concurrent .atomic .AtomicBoolean ;
26
27
import java .util .concurrent .atomic .AtomicReference ;
28
+ import java .util .stream .Collectors ;
27
29
28
30
import org .apache .kafka .clients .consumer .Consumer ;
29
31
import org .apache .kafka .clients .consumer .ConsumerConfig ;
32
34
import org .junit .jupiter .api .BeforeAll ;
33
35
import org .junit .jupiter .api .Test ;
34
36
37
+ import org .springframework .context .ApplicationEvent ;
38
+ import org .springframework .context .ApplicationEventPublisher ;
35
39
import org .springframework .kafka .core .DefaultKafkaConsumerFactory ;
36
40
import org .springframework .kafka .core .DefaultKafkaProducerFactory ;
37
41
import org .springframework .kafka .core .KafkaOperations ;
38
42
import org .springframework .kafka .core .KafkaTemplate ;
43
+ import org .springframework .kafka .event .ConsumerPausedEvent ;
44
+ import org .springframework .kafka .event .ConsumerResumedEvent ;
39
45
import org .springframework .kafka .event .ConsumerStoppedEvent ;
40
46
import org .springframework .kafka .test .EmbeddedKafkaBroker ;
41
47
import org .springframework .kafka .test .condition .EmbeddedKafkaCondition ;
@@ -88,8 +94,8 @@ public void testRetriesAndDlt() throws InterruptedException {
88
94
throw new ListenerExecutionFailedException ("fail for retry batch" );
89
95
});
90
96
91
- KafkaMessageListenerContainer <Integer , String > container =
92
- new KafkaMessageListenerContainer <>(cf , containerProps );
97
+ ConcurrentMessageListenerContainer <Integer , String > container =
98
+ new ConcurrentMessageListenerContainer <>(cf , containerProps );
93
99
container .setBeanName ("retryBatch" );
94
100
final CountDownLatch recoverLatch = new CountDownLatch (1 );
95
101
final AtomicReference <String > failedGroupId = new AtomicReference <>();
@@ -110,10 +116,21 @@ public void accept(ConsumerRecord<?, ?> record, Exception exception) {
110
116
FallbackBatchErrorHandler errorHandler = new FallbackBatchErrorHandler (new FixedBackOff (0L , 3 ), recoverer );
111
117
container .setCommonErrorHandler (errorHandler );
112
118
final CountDownLatch stopLatch = new CountDownLatch (1 );
113
- container .setApplicationEventPublisher (e -> {
114
- if (e instanceof ConsumerStoppedEvent ) {
115
- stopLatch .countDown ();
119
+ List <ApplicationEvent > events = new ArrayList <>();
120
+ container .setApplicationEventPublisher (new ApplicationEventPublisher () {
121
+
122
+ @ Override
123
+ public void publishEvent (ApplicationEvent e ) {
124
+ events .add (e );
125
+ if (e instanceof ConsumerStoppedEvent ) {
126
+ stopLatch .countDown ();
127
+ }
128
+ }
129
+
130
+ @ Override
131
+ public void publishEvent (Object event ) {
116
132
}
133
+
117
134
});
118
135
container .start ();
119
136
@@ -134,6 +151,14 @@ public void accept(ConsumerRecord<?, ?> record, Exception exception) {
134
151
pf .destroy ();
135
152
consumer .close ();
136
153
assertThat (stopLatch .await (10 , TimeUnit .SECONDS )).isTrue ();
154
+ assertThat (events .stream ()
155
+ .filter (ev -> ev instanceof ConsumerPausedEvent )
156
+ .collect (Collectors .toList ()))
157
+ .hasSize (1 );
158
+ assertThat (events .stream ()
159
+ .filter (ev -> ev instanceof ConsumerResumedEvent )
160
+ .collect (Collectors .toList ()))
161
+ .hasSize (1 );
137
162
}
138
163
139
164
@ Test
0 commit comments