23
23
import com .rabbitmq .stream .OffsetSpecification ;
24
24
import com .rabbitmq .stream .StreamDoesNotExistException ;
25
25
import com .rabbitmq .stream .StreamException ;
26
+ import com .rabbitmq .stream .SubscriptionListener ;
27
+ import com .rabbitmq .stream .SubscriptionListener .SubscriptionContext ;
26
28
import com .rabbitmq .stream .impl .Client .ChunkListener ;
27
29
import com .rabbitmq .stream .impl .Client .CreditNotification ;
28
30
import com .rabbitmq .stream .impl .Client .MessageListener ;
@@ -83,6 +85,7 @@ Runnable subscribe(
83
85
String stream ,
84
86
OffsetSpecification offsetSpecification ,
85
87
String trackingReference ,
88
+ SubscriptionListener subscriptionListener ,
86
89
MessageHandler messageHandler ) {
87
90
// FIXME fail immediately if there's no locator (can provide a supplier that does not retry)
88
91
List <Client .Broker > candidates = findBrokersForStream (stream );
@@ -95,7 +98,12 @@ Runnable subscribe(
95
98
// we keep this instance when we move the subscription from a client to another one
96
99
SubscriptionTracker subscriptionTracker =
97
100
new SubscriptionTracker (
98
- consumer , stream , offsetSpecification , trackingReference , messageHandler );
101
+ consumer ,
102
+ stream ,
103
+ offsetSpecification ,
104
+ trackingReference ,
105
+ subscriptionListener ,
106
+ messageHandler );
99
107
100
108
String key = keyForClientSubscription (newNode );
101
109
@@ -212,6 +220,7 @@ private static class SubscriptionTracker {
212
220
private final String offsetTrackingReference ;
213
221
private final MessageHandler messageHandler ;
214
222
private final StreamConsumer consumer ;
223
+ private final SubscriptionListener subscriptionListener ;
215
224
private volatile long offset ;
216
225
private volatile boolean hasReceivedSomething = false ;
217
226
private volatile byte subscriptionIdInClient ;
@@ -223,11 +232,13 @@ private SubscriptionTracker(
223
232
String stream ,
224
233
OffsetSpecification initialOffsetSpecification ,
225
234
String offsetTrackingReference ,
235
+ SubscriptionListener subscriptionListener ,
226
236
MessageHandler messageHandler ) {
227
237
this .consumer = consumer ;
228
238
this .stream = stream ;
229
239
this .initialOffsetSpecification = initialOffsetSpecification ;
230
240
this .offsetTrackingReference = offsetTrackingReference ;
241
+ this .subscriptionListener = subscriptionListener ;
231
242
this .messageHandler = messageHandler ;
232
243
}
233
244
@@ -635,7 +646,7 @@ synchronized void add(
635
646
update (previousSubscriptions , subscriptionId , subscriptionTracker );
636
647
637
648
String offsetTrackingReference = subscriptionTracker .offsetTrackingReference ;
638
- if (subscriptionTracker . offsetTrackingReference != null ) {
649
+ if (offsetTrackingReference != null ) {
639
650
long trackedOffset =
640
651
client .queryOffset (offsetTrackingReference , subscriptionTracker .stream );
641
652
if (trackedOffset != 0 ) {
@@ -666,12 +677,20 @@ synchronized void add(
666
677
subscriptionProperties .put ("name" , subscriptionTracker .offsetTrackingReference );
667
678
}
668
679
680
+ SubscriptionContext subscriptionContext =
681
+ new DefaultSubscriptionContext (offsetSpecification );
682
+ subscriptionTracker .subscriptionListener .preSubscribe (subscriptionContext );
683
+ LOGGER .info (
684
+ "Computed offset specification {}, offset specification used after subscription listener {}" ,
685
+ offsetSpecification ,
686
+ subscriptionContext .offsetSpecification ());
687
+
669
688
// FIXME consider using fewer initial credits
670
689
Client .Response subscribeResponse =
671
690
client .subscribe (
672
691
subscriptionId ,
673
692
subscriptionTracker .stream ,
674
- offsetSpecification ,
693
+ subscriptionContext . offsetSpecification () ,
675
694
10 ,
676
695
subscriptionProperties );
677
696
if (!subscribeResponse .isOk ()) {
@@ -767,4 +786,28 @@ synchronized void close() {
767
786
}
768
787
}
769
788
}
789
+
790
+ private static final class DefaultSubscriptionContext implements SubscriptionContext {
791
+
792
+ private volatile OffsetSpecification offsetSpecification ;
793
+
794
+ private DefaultSubscriptionContext (OffsetSpecification computedOffsetSpecification ) {
795
+ this .offsetSpecification = computedOffsetSpecification ;
796
+ }
797
+
798
+ @ Override
799
+ public OffsetSpecification offsetSpecification () {
800
+ return this .offsetSpecification ;
801
+ }
802
+
803
+ @ Override
804
+ public void offsetSpecification (OffsetSpecification offsetSpecification ) {
805
+ this .offsetSpecification = offsetSpecification ;
806
+ }
807
+
808
+ @ Override
809
+ public String toString () {
810
+ return "SubscriptionContext{" + "offsetSpecification=" + offsetSpecification + '}' ;
811
+ }
812
+ }
770
813
}
0 commit comments