@@ -95,6 +95,7 @@ Runnable subscribe(
95
95
OffsetSpecification offsetSpecification ,
96
96
String trackingReference ,
97
97
SubscriptionListener subscriptionListener ,
98
+ Runnable trackingClosingCallback ,
98
99
MessageHandler messageHandler ,
99
100
Map <String , String > subscriptionProperties ) {
100
101
// FIXME fail immediately if there's no locator (can provide a supplier that does not retry)
@@ -113,6 +114,7 @@ Runnable subscribe(
113
114
offsetSpecification ,
114
115
trackingReference ,
115
116
subscriptionListener ,
117
+ trackingClosingCallback ,
116
118
messageHandler ,
117
119
subscriptionProperties );
118
120
@@ -235,6 +237,7 @@ private static class SubscriptionTracker {
235
237
private final MessageHandler messageHandler ;
236
238
private final StreamConsumer consumer ;
237
239
private final SubscriptionListener subscriptionListener ;
240
+ private final Runnable trackingClosingCallback ;
238
241
private final Map <String , String > subscriptionProperties ;
239
242
private volatile long offset ;
240
243
private volatile boolean hasReceivedSomething = false ;
@@ -247,13 +250,15 @@ private SubscriptionTracker(
247
250
OffsetSpecification initialOffsetSpecification ,
248
251
String offsetTrackingReference ,
249
252
SubscriptionListener subscriptionListener ,
253
+ Runnable trackingClosingCallback ,
250
254
MessageHandler messageHandler ,
251
255
Map <String , String > subscriptionProperties ) {
252
256
this .consumer = consumer ;
253
257
this .stream = stream ;
254
258
this .initialOffsetSpecification = initialOffsetSpecification ;
255
259
this .offsetTrackingReference = offsetTrackingReference ;
256
260
this .subscriptionListener = subscriptionListener ;
261
+ this .trackingClosingCallback = trackingClosingCallback ;
257
262
this .messageHandler = messageHandler ;
258
263
if (this .offsetTrackingReference == null ) {
259
264
this .subscriptionProperties = subscriptionProperties ;
@@ -267,6 +272,11 @@ private SubscriptionTracker(
267
272
}
268
273
269
274
synchronized void cancel () {
275
+ // the flow of messages in the user message handler should stop, we can call the tracking
276
+ // closing callback
277
+ // with automatic offset tracking, it will store the last dispatched offset
278
+ LOGGER .debug ("Calling tracking consumer closing callback (may be no-op)" );
279
+ this .trackingClosingCallback .run ();
270
280
if (this .manager != null ) {
271
281
LOGGER .debug ("Removing consumer from manager " + this .consumer );
272
282
this .manager .remove (this );
@@ -298,10 +308,10 @@ private static final class MessageHandlerContext implements Context {
298
308
private final long offset ;
299
309
private final long timestamp ;
300
310
private final long committedOffset ;
301
- private final Consumer consumer ;
311
+ private final StreamConsumer consumer ;
302
312
303
313
private MessageHandlerContext (
304
- long offset , long timestamp , long committedOffset , Consumer consumer ) {
314
+ long offset , long timestamp , long committedOffset , StreamConsumer consumer ) {
305
315
this .offset = offset ;
306
316
this .timestamp = timestamp ;
307
317
this .committedOffset = committedOffset ;
@@ -328,6 +338,10 @@ public long committedChunkId() {
328
338
return committedOffset ;
329
339
}
330
340
341
+ public String stream () {
342
+ return this .consumer .stream ();
343
+ }
344
+
331
345
@ Override
332
346
public Consumer consumer () {
333
347
return this .consumer ;
@@ -455,14 +469,14 @@ private ClientSubscriptionsManager(
455
469
(subscriptionId , offset , chunkTimestamp , committedOffset , message ) -> {
456
470
SubscriptionTracker subscriptionTracker =
457
471
subscriptionTrackers .get (subscriptionId & 0xFF );
458
- if (subscriptionTracker != null ) {
472
+ if (subscriptionTracker != null && subscriptionTracker . consumer . isOpen () ) {
459
473
subscriptionTracker .offset = offset ;
460
474
subscriptionTracker .hasReceivedSomething = true ;
461
475
subscriptionTracker .messageHandler .handle (
462
476
new MessageHandlerContext (
463
477
offset , chunkTimestamp , committedOffset , subscriptionTracker .consumer ),
464
478
message );
465
- // FIXME set offset here as well, best effort to avoid duplicates
479
+ // FIXME set offset here as well, best effort to avoid duplicates?
466
480
} else {
467
481
LOGGER .debug ("Could not find stream subscription {}" , subscriptionId );
468
482
}
@@ -741,6 +755,8 @@ void add(
741
755
offsetSpecification =
742
756
offsetSpecification == null ? DEFAULT_OFFSET_SPECIFICATION : offsetSpecification ;
743
757
758
+ // TODO consider using/emulating ConsumerUpdateListener, to have only one API, not 2
759
+ // even when the consumer is not a SAC.
744
760
SubscriptionContext subscriptionContext =
745
761
new DefaultSubscriptionContext (offsetSpecification );
746
762
subscriptionTracker .subscriptionListener .preSubscribe (subscriptionContext );
0 commit comments