29
29
import com .rabbitmq .stream .StreamCreator ;
30
30
import com .rabbitmq .stream .StreamException ;
31
31
import com .rabbitmq .stream .compression .CompressionCodecFactory ;
32
+ import com .rabbitmq .stream .impl .Client .ClientParameters ;
32
33
import com .rabbitmq .stream .impl .OffsetTrackingCoordinator .Registration ;
33
34
import com .rabbitmq .stream .impl .StreamConsumerBuilder .TrackingConfiguration ;
34
35
import com .rabbitmq .stream .impl .StreamEnvironmentBuilder .DefaultTlsConfiguration ;
@@ -83,6 +84,8 @@ class StreamEnvironment implements Environment {
83
84
private final Clock clock = new Clock ();
84
85
private final ScheduledFuture <?> clockRefreshFuture ;
85
86
private final ByteBufAllocator byteBufAllocator ;
87
+ private final AtomicBoolean locatorInitialized = new AtomicBoolean (false );
88
+ private final Runnable locatorInitializationSequence ;
86
89
private volatile Client locator ;
87
90
88
91
StreamEnvironment (
@@ -96,7 +99,8 @@ class StreamEnvironment implements Environment {
96
99
int maxTrackingConsumersByConnection ,
97
100
int maxConsumersByConnection ,
98
101
DefaultTlsConfiguration tlsConfiguration ,
99
- ByteBufAllocator byteBufAllocator ) {
102
+ ByteBufAllocator byteBufAllocator ,
103
+ boolean lazyInit ) {
100
104
this (
101
105
scheduledExecutorService ,
102
106
clientParametersPrototype ,
@@ -109,6 +113,7 @@ class StreamEnvironment implements Environment {
109
113
maxConsumersByConnection ,
110
114
tlsConfiguration ,
111
115
byteBufAllocator ,
116
+ lazyInit ,
112
117
cp -> new Client (cp ));
113
118
}
114
119
@@ -124,6 +129,7 @@ class StreamEnvironment implements Environment {
124
129
int maxConsumersByConnection ,
125
130
DefaultTlsConfiguration tlsConfiguration ,
126
131
ByteBufAllocator byteBufAllocator ,
132
+ boolean lazyInit ,
127
133
Function <Client .ClientParameters , Client > clientFactory ) {
128
134
this .recoveryBackOffDelayPolicy = recoveryBackOffDelayPolicy ;
129
135
this .topologyUpdateBackOffDelayPolicy = topologyBackOffDelayPolicy ;
@@ -235,29 +241,41 @@ class StreamEnvironment implements Environment {
235
241
}
236
242
};
237
243
shutdownListenerReference .set (shutdownListener );
238
- RuntimeException lastException = null ;
239
- for (Address address : addresses ) {
240
- address = addressResolver .resolve (address );
241
- Client .ClientParameters locatorParameters =
242
- clientParametersPrototype
243
- .duplicate ()
244
- .host (address .host ())
245
- .port (address .port ())
246
- .clientProperty ("connection_name" , "rabbitmq-stream-locator" )
247
- .shutdownListener (shutdownListenerReference .get ());
248
- try {
249
- this .locator = clientFactory .apply (locatorParameters );
250
- LOGGER .debug ("Locator connected to {}" , address );
251
- break ;
252
- } catch (RuntimeException e ) {
253
- LOGGER .debug ("Error while try to connect to {}: {}" , address , e .getMessage ());
254
- lastException = e ;
255
- }
256
- }
257
- if (this .locator == null ) {
258
- throw lastException ;
244
+ ClientParameters clientParametersForInit = clientParametersPrototype .duplicate ();
245
+ Runnable locatorInitSequence =
246
+ () -> {
247
+ RuntimeException lastException = null ;
248
+ for (Address address : addresses ) {
249
+ address = addressResolver .resolve (address );
250
+ Client .ClientParameters locatorParameters =
251
+ clientParametersForInit
252
+ .duplicate ()
253
+ .host (address .host ())
254
+ .port (address .port ())
255
+ .clientProperty ("connection_name" , "rabbitmq-stream-locator" )
256
+ .shutdownListener (shutdownListenerReference .get ());
257
+ try {
258
+ this .locator = clientFactory .apply (locatorParameters );
259
+ LOGGER .debug ("Locator connected to {}" , address );
260
+ break ;
261
+ } catch (RuntimeException e ) {
262
+ LOGGER .debug ("Error while try to connect to {}: {}" , address , e .getMessage ());
263
+ lastException = e ;
264
+ }
265
+ }
266
+ if (this .locator == null ) {
267
+ throw lastException ;
268
+ }
269
+ };
270
+ if (lazyInit ) {
271
+ this .locatorInitializationSequence = locatorInitSequence ;
272
+ } else {
273
+ locatorInitSequence .run ();
274
+ locatorInitialized .set (true );
275
+ this .locatorInitializationSequence = () -> {};
259
276
}
260
- this .codec = locator .codec ();
277
+ this .codec =
278
+ clientParametersPrototype .codec == null ? Codecs .DEFAULT : clientParametersPrototype .codec ;
261
279
this .clockRefreshFuture =
262
280
this .scheduledExecutorService .scheduleAtFixedRate (
263
281
() -> this .clock .refresh (), 1 , 1 , SECONDS );
@@ -318,13 +336,26 @@ public ByteBufAllocator byteBufAllocator() {
318
336
return byteBufAllocator ;
319
337
}
320
338
339
+ private void maybeInitializeLocator () {
340
+ if (this .locatorInitialized .compareAndSet (false , true )) {
341
+ try {
342
+ this .locatorInitializationSequence .run ();
343
+ } catch (RuntimeException e ) {
344
+ this .locatorInitialized .set (false );
345
+ throw e ;
346
+ }
347
+ }
348
+ }
349
+
321
350
@ Override
322
351
public StreamCreator streamCreator () {
352
+ maybeInitializeLocator ();
323
353
return new StreamStreamCreator (this );
324
354
}
325
355
326
356
@ Override
327
357
public void deleteStream (String stream ) {
358
+ maybeInitializeLocator ();
328
359
Client .Response response = this .locator ().delete (stream );
329
360
if (!response .isOk ()) {
330
361
throw new StreamException (
@@ -339,6 +370,7 @@ public void deleteStream(String stream) {
339
370
340
371
@ Override
341
372
public ProducerBuilder producerBuilder () {
373
+ maybeInitializeLocator ();
342
374
return new StreamProducerBuilder (this );
343
375
}
344
376
@@ -360,6 +392,7 @@ void removeConsumer(StreamConsumer consumer) {
360
392
361
393
@ Override
362
394
public ConsumerBuilder consumerBuilder () {
395
+ maybeInitializeLocator ();
363
396
return new StreamConsumerBuilder (this );
364
397
}
365
398
0 commit comments