4949import org .springframework .amqp .AmqpException ;
5050import org .springframework .amqp .AmqpTimeoutException ;
5151import org .springframework .amqp .rabbit .support .ActiveObjectCounter ;
52- import org .springframework .amqp .support .ConditionalExceptionLogger ;
5352import org .springframework .beans .factory .InitializingBean ;
5453import org .springframework .jmx .export .annotation .ManagedAttribute ;
5554import org .springframework .jmx .export .annotation .ManagedResource ;
@@ -184,8 +183,6 @@ public enum ConfirmType {
184183
185184 private final AtomicInteger connectionHighWaterMark = new AtomicInteger ();
186185
187- private final CachingConnectionFactory publisherConnectionFactory ;
188-
189186 /** Synchronization monitor for the shared Connection. */
190187 private final Object connectionMonitor = new Object ();
191188
@@ -207,6 +204,8 @@ public enum ConfirmType {
207204
208205 private PublisherCallbackChannelFactory publisherChannelFactory = PublisherCallbackChannelImpl .factory ();
209206
207+ private boolean defaultPublisherFactory = true ;
208+
210209 private volatile boolean active = true ;
211210
212211 private volatile boolean initialized ;
@@ -257,8 +256,7 @@ public CachingConnectionFactory(@Nullable String hostNameArg, int port) {
257256 }
258257 setHost (hostname );
259258 setPort (port );
260- this .publisherConnectionFactory = new CachingConnectionFactory (getRabbitConnectionFactory (), true );
261- setPublisherConnectionFactory (this .publisherConnectionFactory );
259+ doSetPublisherConnectionFactory (new CachingConnectionFactory (getRabbitConnectionFactory (), true ));
262260 }
263261
264262 /**
@@ -269,8 +267,7 @@ public CachingConnectionFactory(@Nullable String hostNameArg, int port) {
269267 public CachingConnectionFactory (URI uri ) {
270268 super (newRabbitConnectionFactory ());
271269 setUri (uri );
272- this .publisherConnectionFactory = new CachingConnectionFactory (getRabbitConnectionFactory (), true );
273- setPublisherConnectionFactory (this .publisherConnectionFactory );
270+ doSetPublisherConnectionFactory (new CachingConnectionFactory (getRabbitConnectionFactory (), true ));
274271 }
275272
276273 /**
@@ -288,6 +285,7 @@ public CachingConnectionFactory(com.rabbitmq.client.ConnectionFactory rabbitConn
288285 */
289286 private CachingConnectionFactory (com .rabbitmq .client .ConnectionFactory rabbitConnectionFactory ,
290287 boolean isPublisherFactory ) {
288+
291289 super (rabbitConnectionFactory );
292290 if (!isPublisherFactory ) {
293291 if (rabbitConnectionFactory .isAutomaticRecoveryEnabled ()) {
@@ -301,11 +299,11 @@ private CachingConnectionFactory(com.rabbitmq.client.ConnectionFactory rabbitCon
301299 + "'getRabbitConnectionFactory().setAutomaticRecoveryEnabled(true)',\n "
302300 + "but this is discouraged." );
303301 }
304- this .publisherConnectionFactory = new CachingConnectionFactory (getRabbitConnectionFactory (), true );
305- setPublisherConnectionFactory (this .publisherConnectionFactory );
302+ super .setPublisherConnectionFactory (new CachingConnectionFactory (getRabbitConnectionFactory (), true ));
306303 }
307304 else {
308- this .publisherConnectionFactory = null ;
305+ super .setPublisherConnectionFactory (null );
306+ this .defaultPublisherFactory = false ;
309307 }
310308 }
311309
@@ -315,6 +313,12 @@ private static com.rabbitmq.client.ConnectionFactory newRabbitConnectionFactory(
315313 return connectionFactory ;
316314 }
317315
316+ @ Override
317+ public void setPublisherConnectionFactory (@ Nullable AbstractConnectionFactory publisherConnectionFactory ) {
318+ super .setPublisherConnectionFactory (publisherConnectionFactory );
319+ this .defaultPublisherFactory = false ;
320+ }
321+
318322 /**
319323 * The number of channels to maintain in the cache. By default, channels are allocated on
320324 * demand (unbounded) and this represents the maximum cache size. To limit the available
@@ -325,8 +329,8 @@ private static com.rabbitmq.client.ConnectionFactory newRabbitConnectionFactory(
325329 public void setChannelCacheSize (int sessionCacheSize ) {
326330 Assert .isTrue (sessionCacheSize >= 1 , "Channel cache size must be 1 or higher" );
327331 this .channelCacheSize = sessionCacheSize ;
328- if (this .publisherConnectionFactory != null ) {
329- this . publisherConnectionFactory .setChannelCacheSize (sessionCacheSize );
332+ if (this .defaultPublisherFactory ) {
333+ (( CachingConnectionFactory ) getPublisherConnectionFactory ()) .setChannelCacheSize (sessionCacheSize );
330334 }
331335 }
332336
@@ -342,8 +346,8 @@ public void setCacheMode(CacheMode cacheMode) {
342346 Assert .isTrue (!this .initialized , "'cacheMode' cannot be changed after initialization." );
343347 Assert .notNull (cacheMode , "'cacheMode' must not be null." );
344348 this .cacheMode = cacheMode ;
345- if (this .publisherConnectionFactory != null ) {
346- this . publisherConnectionFactory .setCacheMode (cacheMode );
349+ if (this .defaultPublisherFactory ) {
350+ (( CachingConnectionFactory ) getPublisherConnectionFactory ()) .setCacheMode (cacheMode );
347351 }
348352 }
349353
@@ -354,8 +358,8 @@ public int getConnectionCacheSize() {
354358 public void setConnectionCacheSize (int connectionCacheSize ) {
355359 Assert .isTrue (connectionCacheSize >= 1 , "Connection cache size must be 1 or higher." );
356360 this .connectionCacheSize = connectionCacheSize ;
357- if (this .publisherConnectionFactory != null ) {
358- this . publisherConnectionFactory .setConnectionCacheSize (connectionCacheSize );
361+ if (this .defaultPublisherFactory ) {
362+ (( CachingConnectionFactory ) getPublisherConnectionFactory ()) .setConnectionCacheSize (connectionCacheSize );
359363 }
360364 }
361365
@@ -370,8 +374,8 @@ public void setConnectionCacheSize(int connectionCacheSize) {
370374 public void setConnectionLimit (int connectionLimit ) {
371375 Assert .isTrue (connectionLimit >= 1 , "Connection limit must be 1 or higher." );
372376 this .connectionLimit = connectionLimit ;
373- if (this .publisherConnectionFactory != null ) {
374- this . publisherConnectionFactory .setConnectionLimit (connectionLimit );
377+ if (this .defaultPublisherFactory ) {
378+ (( CachingConnectionFactory ) getPublisherConnectionFactory ()) .setConnectionLimit (connectionLimit );
375379 }
376380 }
377381
@@ -387,8 +391,8 @@ public boolean isPublisherReturns() {
387391
388392 public void setPublisherReturns (boolean publisherReturns ) {
389393 this .publisherReturns = publisherReturns ;
390- if (this .publisherConnectionFactory != null ) {
391- this . publisherConnectionFactory .setPublisherReturns (publisherReturns );
394+ if (this .defaultPublisherFactory ) {
395+ (( CachingConnectionFactory ) getPublisherConnectionFactory ()) .setPublisherReturns (publisherReturns );
392396 }
393397 }
394398
@@ -444,8 +448,8 @@ public boolean isSimplePublisherConfirms() {
444448 public void setPublisherConfirmType (ConfirmType confirmType ) {
445449 Assert .notNull (confirmType , "'confirmType' cannot be null" );
446450 this .confirmType = confirmType ;
447- if (this .publisherConnectionFactory != null ) {
448- this . publisherConnectionFactory .setPublisherConfirmType (confirmType );
451+ if (this .defaultPublisherFactory ) {
452+ (( CachingConnectionFactory ) getPublisherConnectionFactory ()) .setPublisherConfirmType (confirmType );
449453 }
450454 }
451455
@@ -463,24 +467,9 @@ public void setPublisherConfirmType(ConfirmType confirmType) {
463467 */
464468 public void setChannelCheckoutTimeout (long channelCheckoutTimeout ) {
465469 this .channelCheckoutTimeout = channelCheckoutTimeout ;
466- if (this .publisherConnectionFactory != null ) {
467- this .publisherConnectionFactory .setChannelCheckoutTimeout (channelCheckoutTimeout );
468- }
469- }
470-
471- /**
472- * Set the strategy for logging close exceptions; by default, if a channel is closed due to a failed
473- * passive queue declaration, it is logged at debug level. Normal channel closes (200 OK) are not
474- * logged. All others are logged at ERROR level (unless access is refused due to an exclusive consumer
475- * condition, in which case, it is logged at INFO level).
476- * @param closeExceptionLogger the {@link ConditionalExceptionLogger}.
477- * @since 1.5
478- */
479- public void setCloseExceptionLogger (ConditionalExceptionLogger closeExceptionLogger ) {
480- Assert .notNull (closeExceptionLogger , "'closeExceptionLogger' cannot be null" );
481- this .closeExceptionLogger = closeExceptionLogger ;
482- if (this .publisherConnectionFactory != null ) {
483- this .publisherConnectionFactory .setCloseExceptionLogger (closeExceptionLogger );
470+ if (this .defaultPublisherFactory ) {
471+ ((CachingConnectionFactory ) getPublisherConnectionFactory ())
472+ .setChannelCheckoutTimeout (channelCheckoutTimeout );
484473 }
485474 }
486475
@@ -502,8 +491,8 @@ public void afterPropertiesSet() {
502491 "When the cache mode is 'CHANNEL', the connection cache size cannot be configured." );
503492 }
504493 initCacheWaterMarks ();
505- if (this .publisherConnectionFactory != null ) {
506- this . publisherConnectionFactory .afterPropertiesSet ();
494+ if (this .defaultPublisherFactory ) {
495+ (( CachingConnectionFactory ) getPublisherConnectionFactory ()) .afterPropertiesSet ();
507496 }
508497 }
509498
@@ -901,8 +890,8 @@ public void resetConnection() {
901890 this .channelHighWaterMarks .values ().forEach (count -> count .set (0 ));
902891 this .connectionHighWaterMark .set (0 );
903892 }
904- if (this .publisherConnectionFactory != null ) {
905- this . publisherConnectionFactory .resetConnection ();
893+ if (this .defaultPublisherFactory ) {
894+ (( CachingConnectionFactory ) getPublisherConnectionFactory ()) .resetConnection ();
906895 }
907896 }
908897
@@ -995,8 +984,8 @@ public Properties getCacheProperties() {
995984 */
996985 @ ManagedAttribute
997986 public Properties getPublisherConnectionFactoryCacheProperties () {
998- if (this .publisherConnectionFactory != null ) {
999- return this . publisherConnectionFactory .getCacheProperties ();
987+ if (this .defaultPublisherFactory ) {
988+ return (( CachingConnectionFactory ) getPublisherConnectionFactory ()) .getCacheProperties ();
1000989 }
1001990 return new Properties ();
1002991 }
0 commit comments