63
63
public abstract class AbstractConnectionFactory implements ConnectionFactory , DisposableBean , BeanNameAware ,
64
64
ApplicationContextAware , ApplicationEventPublisherAware , ApplicationListener <ContextClosedEvent > {
65
65
66
+ private static final String PUBLISHER_SUFFIX = ".publisher" ;
67
+
66
68
public static final int DEFAULT_CLOSE_TIMEOUT = 30000 ;
67
69
68
70
private static final String BAD_URI = "setUri() was passed an invalid URI; it is ignored" ;
@@ -77,6 +79,8 @@ public abstract class AbstractConnectionFactory implements ConnectionFactory, Di
77
79
78
80
private final AtomicInteger defaultConnectionNameStrategyCounter = new AtomicInteger ();
79
81
82
+ private AbstractConnectionFactory publisherConnectionFactory ;
83
+
80
84
private RecoveryListener recoveryListener = new RecoveryListener () {
81
85
82
86
@ Override
@@ -115,17 +119,26 @@ public void handleRecovery(Recoverable recoverable) {
115
119
private volatile boolean contextStopped ;
116
120
117
121
/**
118
- * Create a new AbstractConnectionFactory for the given target ConnectionFactory.
122
+ * Create a new AbstractConnectionFactory for the given target ConnectionFactory,
123
+ * with no publisher connection factory.
119
124
* @param rabbitConnectionFactory the target ConnectionFactory
120
125
*/
121
126
public AbstractConnectionFactory (com .rabbitmq .client .ConnectionFactory rabbitConnectionFactory ) {
122
127
Assert .notNull (rabbitConnectionFactory , "Target ConnectionFactory must not be null" );
123
128
this .rabbitConnectionFactory = rabbitConnectionFactory ;
124
129
}
125
130
131
+ protected final void setPublisherConnectionFactory (
132
+ AbstractConnectionFactory publisherConnectionFactory ) {
133
+ this .publisherConnectionFactory = publisherConnectionFactory ;
134
+ }
135
+
126
136
@ Override
127
137
public void setApplicationContext (ApplicationContext applicationContext ) {
128
138
this .applicationContext = applicationContext ;
139
+ if (this .publisherConnectionFactory != null ) {
140
+ this .publisherConnectionFactory .setApplicationContext (applicationContext );
141
+ }
129
142
}
130
143
131
144
protected ApplicationContext getApplicationContext () {
@@ -135,6 +148,9 @@ protected ApplicationContext getApplicationContext() {
135
148
@ Override
136
149
public void setApplicationEventPublisher (ApplicationEventPublisher applicationEventPublisher ) {
137
150
this .applicationEventPublisher = applicationEventPublisher ;
151
+ if (this .publisherConnectionFactory != null ) {
152
+ this .publisherConnectionFactory .setApplicationEventPublisher (applicationEventPublisher );
153
+ }
138
154
}
139
155
140
156
protected ApplicationEventPublisher getApplicationEventPublisher () {
@@ -146,6 +162,9 @@ public void onApplicationEvent(ContextClosedEvent event) {
146
162
if (getApplicationContext () == event .getApplicationContext ()) {
147
163
this .contextStopped = true ;
148
164
}
165
+ if (this .publisherConnectionFactory != null ) {
166
+ this .publisherConnectionFactory .onApplicationEvent (event );
167
+ }
149
168
}
150
169
151
170
protected boolean getContextStopped () {
@@ -261,6 +280,9 @@ public void setAddresses(String addresses) {
261
280
Address [] addressArray = Address .parseAddresses (addresses );
262
281
if (addressArray .length > 0 ) {
263
282
this .addresses = addressArray ;
283
+ if (this .publisherConnectionFactory != null ) {
284
+ this .publisherConnectionFactory .setAddresses (addresses );
285
+ }
264
286
return ;
265
287
}
266
288
}
@@ -288,21 +310,34 @@ protected ChannelListener getChannelListener() {
288
310
289
311
public void setConnectionListeners (List <? extends ConnectionListener > listeners ) {
290
312
this .connectionListener .setDelegates (listeners );
313
+ if (this .publisherConnectionFactory != null ) {
314
+ this .publisherConnectionFactory .setConnectionListeners (listeners );
315
+ }
291
316
}
292
317
293
318
@ Override
294
319
public void addConnectionListener (ConnectionListener listener ) {
295
320
this .connectionListener .addDelegate (listener );
321
+ if (this .publisherConnectionFactory != null ) {
322
+ this .publisherConnectionFactory .addConnectionListener (listener );
323
+ }
296
324
}
297
325
298
326
@ Override
299
327
public boolean removeConnectionListener (ConnectionListener listener ) {
300
- return this .connectionListener .removeDelegate (listener );
328
+ boolean result = this .connectionListener .removeDelegate (listener );
329
+ if (this .publisherConnectionFactory != null ) {
330
+ this .publisherConnectionFactory .removeConnectionListener (listener ); // NOSONAR
331
+ }
332
+ return result ;
301
333
}
302
334
303
335
@ Override
304
336
public void clearConnectionListeners () {
305
337
this .connectionListener .clearDelegates ();
338
+ if (this .publisherConnectionFactory != null ) {
339
+ this .publisherConnectionFactory .clearConnectionListeners ();
340
+ }
306
341
}
307
342
308
343
public void setChannelListeners (List <? extends ChannelListener > listeners ) {
@@ -316,10 +351,16 @@ public void setChannelListeners(List<? extends ChannelListener> listeners) {
316
351
*/
317
352
public void setRecoveryListener (RecoveryListener recoveryListener ) {
318
353
this .recoveryListener = recoveryListener ;
354
+ if (this .publisherConnectionFactory != null ) {
355
+ this .publisherConnectionFactory .setRecoveryListener (recoveryListener );
356
+ }
319
357
}
320
358
321
359
public void addChannelListener (ChannelListener listener ) {
322
360
this .channelListener .addDelegate (listener );
361
+ if (this .publisherConnectionFactory != null ) {
362
+ this .publisherConnectionFactory .addChannelListener (listener );
363
+ }
323
364
}
324
365
325
366
/**
@@ -340,6 +381,9 @@ public void setExecutor(Executor executor) {
340
381
else {
341
382
this .executorService = ((ThreadPoolTaskExecutor ) executor ).getThreadPoolExecutor ();
342
383
}
384
+ if (this .publisherConnectionFactory != null ) {
385
+ this .publisherConnectionFactory .setExecutor (executor );
386
+ }
343
387
}
344
388
345
389
protected ExecutorService getExecutorService () {
@@ -353,6 +397,9 @@ protected ExecutorService getExecutorService() {
353
397
*/
354
398
public void setCloseTimeout (int closeTimeout ) {
355
399
this .closeTimeout = closeTimeout ;
400
+ if (this .publisherConnectionFactory != null ) {
401
+ this .publisherConnectionFactory .setCloseTimeout (closeTimeout );
402
+ }
356
403
}
357
404
358
405
public int getCloseTimeout () {
@@ -367,11 +414,27 @@ public int getCloseTimeout() {
367
414
*/
368
415
public void setConnectionNameStrategy (ConnectionNameStrategy connectionNameStrategy ) {
369
416
this .connectionNameStrategy = connectionNameStrategy ;
417
+ if (this .publisherConnectionFactory != null ) {
418
+ this .publisherConnectionFactory .setConnectionNameStrategy (
419
+ cf -> connectionNameStrategy .obtainNewConnectionName (cf ) + PUBLISHER_SUFFIX );
420
+ }
370
421
}
371
422
372
423
@ Override
373
424
public void setBeanName (String name ) {
374
425
this .beanName = name ;
426
+ if (this .publisherConnectionFactory != null ) {
427
+ this .publisherConnectionFactory .setBeanName (name + PUBLISHER_SUFFIX );
428
+ }
429
+ }
430
+
431
+ public boolean hasPublisherConnectionFactory () {
432
+ return this .publisherConnectionFactory != null ;
433
+ }
434
+
435
+ @ Override
436
+ public ConnectionFactory getPublisherConnectionFactory () {
437
+ return this .publisherConnectionFactory ;
375
438
}
376
439
377
440
protected final Connection createBareConnection () {
@@ -430,6 +493,9 @@ protected final String getDefaultHostName() {
430
493
431
494
@ Override
432
495
public void destroy () {
496
+ if (this .publisherConnectionFactory != null ) {
497
+ this .publisherConnectionFactory .destroy ();
498
+ }
433
499
}
434
500
435
501
@ Override
0 commit comments