16
16
import com .rabbitmq .stream .BackOffDelayPolicy ;
17
17
import com .rabbitmq .stream .Consumer ;
18
18
import com .rabbitmq .stream .ConsumerUpdateListener ;
19
- import com .rabbitmq .stream .ConsumerUpdateListener .Status ;
20
19
import com .rabbitmq .stream .MessageHandler ;
21
20
import com .rabbitmq .stream .MessageHandler .Context ;
22
21
import com .rabbitmq .stream .OffsetSpecification ;
22
+ import com .rabbitmq .stream .StreamException ;
23
23
import com .rabbitmq .stream .SubscriptionListener ;
24
+ import com .rabbitmq .stream .impl .Client .QueryOffsetResponse ;
24
25
import com .rabbitmq .stream .impl .StreamConsumerBuilder .TrackingConfiguration ;
25
26
import com .rabbitmq .stream .impl .StreamEnvironment .TrackingConsumerRegistration ;
26
27
import com .rabbitmq .stream .impl .Utils .CompositeConsumerUpdateListener ;
@@ -131,7 +132,7 @@ class StreamConsumer implements Consumer {
131
132
&& context .status () == ConsumerUpdateListener .Status .ACTIVE ) {
132
133
LOGGER .debug ("Looking up offset (stream {})" , this .stream );
133
134
StreamConsumer consumer = (StreamConsumer ) context .consumer ();
134
- long offset = consumer .lastStoredOffset ();
135
+ long offset = consumer .storedOffset ();
135
136
LOGGER .debug (
136
137
"Stored offset is {}, returning the value + 1 to the server" , offset );
137
138
return OffsetSpecification .offset (offset + 1 );
@@ -169,8 +170,7 @@ class StreamConsumer implements Consumer {
169
170
if (context .previousStatus () == ConsumerUpdateListener .Status .PASSIVE
170
171
&& context .status () == ConsumerUpdateListener .Status .ACTIVE ) {
171
172
LOGGER .debug ("Going from passive to active, looking up offset" );
172
- StreamConsumer consumer = (StreamConsumer ) context .consumer ();
173
- long offset = consumer .lastStoredOffset ();
173
+ long offset = context .consumer ().storedOffset ();
174
174
LOGGER .debug (
175
175
"Stored offset is {}, returning the value + 1 to the server" , offset );
176
176
result = OffsetSpecification .offset (offset + 1 );
@@ -221,8 +221,9 @@ void waitForOffsetToBeStored(long expectedStoredOffset) {
221
221
CompletableFuture <Boolean > storedTask =
222
222
AsyncRetry .asyncRetry (
223
223
() -> {
224
- long lastStoredOffset = lastStoredOffset ();
225
- boolean stored = lastStoredOffset == expectedStoredOffset ;
224
+ long lastStoredOffset = storedOffset ();
225
+ boolean stored =
226
+ Long .compareUnsigned (lastStoredOffset , expectedStoredOffset ) == 0 ;
226
227
LOGGER .debug (
227
228
"Last stored offset from consumer {} on {} is {}, expecting {}" ,
228
229
this .id ,
@@ -316,56 +317,6 @@ OffsetSpecification consumerUpdate(boolean active) {
316
317
return result ;
317
318
}
318
319
319
- private static class DefaultConsumerUpdateContext implements ConsumerUpdateListener .Context {
320
-
321
- private final StreamConsumer consumer ;
322
- private final ConsumerUpdateListener .Status status ;
323
- private final ConsumerUpdateListener .Status previousStatus ;
324
-
325
- private DefaultConsumerUpdateContext (
326
- StreamConsumer consumer ,
327
- ConsumerUpdateListener .Status status ,
328
- ConsumerUpdateListener .Status previousStatus ) {
329
- this .consumer = consumer ;
330
- this .status = status ;
331
- this .previousStatus = previousStatus ;
332
- }
333
-
334
- @ Override
335
- public Consumer consumer () {
336
- return this .consumer ;
337
- }
338
-
339
- @ Override
340
- public String stream () {
341
- return this .consumer .stream ;
342
- }
343
-
344
- @ Override
345
- public ConsumerUpdateListener .Status status () {
346
- return this .status ;
347
- }
348
-
349
- @ Override
350
- public ConsumerUpdateListener .Status previousStatus () {
351
- return this .previousStatus ;
352
- }
353
-
354
- @ Override
355
- public String toString () {
356
- return "DefaultConsumerUpdateContext{"
357
- + "consumer="
358
- + consumer
359
- + ", stream="
360
- + stream ()
361
- + ", status="
362
- + status
363
- + ", previousStatus="
364
- + previousStatus
365
- + '}' ;
366
- }
367
- }
368
-
369
320
boolean isSac () {
370
321
return this .sacStatus != null ;
371
322
}
@@ -418,30 +369,35 @@ void running() {
418
369
this .status = Status .RUNNING ;
419
370
}
420
371
421
- long lastStoredOffset () {
372
+ @ Override
373
+ public long storedOffset () {
422
374
if (canTrack ()) {
423
- try {
424
- // the client can be null by now, but we catch the exception and return 0
425
- // callers should know how to deal with a stored offset of 0
426
- return this .trackingClient .queryOffset (this .name , this .stream );
427
- } catch (Exception e ) {
428
- return 0 ;
375
+ // the client can be null by now, but we catch the exception and return 0
376
+ // callers should know how to deal with a stored offset of 0
377
+ QueryOffsetResponse response = this .trackingClient .queryOffset (this .name , this .stream );
378
+ if (!response .isOk ()) {
379
+ throw new StreamException (
380
+ String .format (
381
+ "QueryOffset for consumer %s on stream %s returned an error" ,
382
+ this .name , this .stream ),
383
+ response .getResponseCode ());
429
384
}
385
+ return response .getOffset ();
386
+ } else if (this .name == null ) {
387
+ throw new UnsupportedOperationException (
388
+ "Not possible to query stored offset for a consumer without a name" );
430
389
} else {
431
- return 0 ;
390
+ throw new IllegalStateException (
391
+ String .format (
392
+ "Not possible to query offset for consumer %s on stream %s for now" ,
393
+ this .name , this .stream ));
432
394
}
433
395
}
434
396
435
397
String stream () {
436
398
return this .stream ;
437
399
}
438
400
439
- enum Status {
440
- RUNNING ,
441
- NOT_AVAILABLE ,
442
- CLOSED
443
- }
444
-
445
401
@ Override
446
402
public boolean equals (Object o ) {
447
403
if (this == o ) {
@@ -463,4 +419,60 @@ public int hashCode() {
463
419
public String toString () {
464
420
return "StreamConsumer{" + "id=" + id + ", stream='" + stream + '\'' + '}' ;
465
421
}
422
+
423
+ enum Status {
424
+ RUNNING ,
425
+ NOT_AVAILABLE ,
426
+ CLOSED
427
+ }
428
+
429
+ private static class DefaultConsumerUpdateContext implements ConsumerUpdateListener .Context {
430
+
431
+ private final StreamConsumer consumer ;
432
+ private final ConsumerUpdateListener .Status status ;
433
+ private final ConsumerUpdateListener .Status previousStatus ;
434
+
435
+ private DefaultConsumerUpdateContext (
436
+ StreamConsumer consumer ,
437
+ ConsumerUpdateListener .Status status ,
438
+ ConsumerUpdateListener .Status previousStatus ) {
439
+ this .consumer = consumer ;
440
+ this .status = status ;
441
+ this .previousStatus = previousStatus ;
442
+ }
443
+
444
+ @ Override
445
+ public Consumer consumer () {
446
+ return this .consumer ;
447
+ }
448
+
449
+ @ Override
450
+ public String stream () {
451
+ return this .consumer .stream ;
452
+ }
453
+
454
+ @ Override
455
+ public ConsumerUpdateListener .Status status () {
456
+ return this .status ;
457
+ }
458
+
459
+ @ Override
460
+ public ConsumerUpdateListener .Status previousStatus () {
461
+ return this .previousStatus ;
462
+ }
463
+
464
+ @ Override
465
+ public String toString () {
466
+ return "DefaultConsumerUpdateContext{"
467
+ + "consumer="
468
+ + consumer
469
+ + ", stream="
470
+ + stream ()
471
+ + ", status="
472
+ + status
473
+ + ", previousStatus="
474
+ + previousStatus
475
+ + '}' ;
476
+ }
477
+ }
466
478
}
0 commit comments