Skip to content

Commit 99ccb1b

Browse files
authored
Fix Sonar issues and other refactoring
- Allow the publishing connection factory to be explicitly set and be any type - Do not propagate properties when the factory is explicitly configured * Reset flag when this is a publisher CF.
1 parent 39c9c44 commit 99ccb1b

File tree

7 files changed

+128
-60
lines changed

7 files changed

+128
-60
lines changed

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/connection/AbstractConnectionFactory.java

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ public enum AddressShuffleMode {
113113

114114
private final AtomicInteger defaultConnectionNameStrategyCounter = new AtomicInteger();
115115

116+
private ConditionalExceptionLogger closeExceptionLogger = new DefaultChannelCloseLogger();
117+
116118
private AbstractConnectionFactory publisherConnectionFactory;
117119

118120
private RecoveryListener recoveryListener = new RecoveryListener() {
@@ -156,8 +158,6 @@ public void handleRecovery(Recoverable recoverable) {
156158

157159
private volatile boolean contextStopped;
158160

159-
protected ConditionalExceptionLogger closeExceptionLogger = new DefaultChannelCloseLogger();
160-
161161
/**
162162
* Create a new AbstractConnectionFactory for the given target ConnectionFactory,
163163
* with no publisher connection factory.
@@ -168,8 +168,21 @@ public AbstractConnectionFactory(com.rabbitmq.client.ConnectionFactory rabbitCon
168168
this.rabbitConnectionFactory = rabbitConnectionFactory;
169169
}
170170

171-
protected final void setPublisherConnectionFactory(
172-
AbstractConnectionFactory publisherConnectionFactory) {
171+
/**
172+
* Set a custom publisher connection factory; the type does not need to be the same
173+
* as this factory.
174+
* @param publisherConnectionFactory the factory.
175+
* @since 2.3.2
176+
*/
177+
public void setPublisherConnectionFactory(
178+
@Nullable AbstractConnectionFactory publisherConnectionFactory) {
179+
180+
doSetPublisherConnectionFactory(publisherConnectionFactory);
181+
}
182+
183+
protected final void doSetPublisherConnectionFactory(
184+
@Nullable AbstractConnectionFactory publisherConnectionFactory) {
185+
173186
this.publisherConnectionFactory = publisherConnectionFactory;
174187
}
175188

@@ -472,6 +485,26 @@ public void setConnectionNameStrategy(ConnectionNameStrategy connectionNameStrat
472485
}
473486
}
474487

488+
/**
489+
* Set the strategy for logging close exceptions; by default, if a channel is closed due to a failed
490+
* passive queue declaration, it is logged at debug level. Normal channel closes (200 OK) are not
491+
* logged. All others are logged at ERROR level (unless access is refused due to an exclusive consumer
492+
* condition, in which case, it is logged at INFO level).
493+
* @param closeExceptionLogger the {@link ConditionalExceptionLogger}.
494+
* @since 1.5
495+
*/
496+
public void setCloseExceptionLogger(ConditionalExceptionLogger closeExceptionLogger) {
497+
Assert.notNull(closeExceptionLogger, "'closeExceptionLogger' cannot be null");
498+
this.closeExceptionLogger = closeExceptionLogger;
499+
if (this.publisherConnectionFactory != null) {
500+
this.publisherConnectionFactory.setCloseExceptionLogger(closeExceptionLogger);
501+
}
502+
}
503+
504+
protected ConnectionNameStrategy getConnectionNameStrategy() {
505+
return this.connectionNameStrategy;
506+
}
507+
475508
@Override
476509
public void setBeanName(String name) {
477510
this.beanName = name;

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/connection/CachingConnectionFactory.java

Lines changed: 35 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
import org.springframework.amqp.AmqpException;
5050
import org.springframework.amqp.AmqpTimeoutException;
5151
import org.springframework.amqp.rabbit.support.ActiveObjectCounter;
52-
import org.springframework.amqp.support.ConditionalExceptionLogger;
5352
import org.springframework.beans.factory.InitializingBean;
5453
import org.springframework.jmx.export.annotation.ManagedAttribute;
5554
import 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
}

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/connection/PooledChannelConnectionFactory.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.springframework.amqp.rabbit.support.RabbitExceptionTranslator;
3737
import org.springframework.aop.framework.ProxyFactory;
3838
import org.springframework.aop.support.NameMatchMethodPointcutAdvisor;
39+
import org.springframework.lang.Nullable;
3940
import org.springframework.util.Assert;
4041

4142
import com.rabbitmq.client.Channel;
@@ -60,6 +61,8 @@ public class PooledChannelConnectionFactory extends AbstractConnectionFactory im
6061

6162
private BiConsumer<GenericObjectPool<Channel>, Boolean> poolConfigurer = (pool, tx) -> { };
6263

64+
private boolean defaultPublisherFactory = true;
65+
6366
/**
6467
* Construct an instance.
6568
* @param rabbitConnectionFactory the rabbitmq connection factory.
@@ -78,6 +81,15 @@ private PooledChannelConnectionFactory(ConnectionFactory rabbitConnectionFactory
7881
if (!isPublisher) {
7982
setPublisherConnectionFactory(new PooledChannelConnectionFactory(rabbitConnectionFactory, true));
8083
}
84+
else {
85+
this.defaultPublisherFactory = false;
86+
}
87+
}
88+
89+
@Override
90+
public void setPublisherConnectionFactory(@Nullable AbstractConnectionFactory publisherConnectionFactory) {
91+
super.setPublisherConnectionFactory(publisherConnectionFactory);
92+
this.defaultPublisherFactory = false;
8193
}
8294

8395
/**
@@ -88,6 +100,9 @@ private PooledChannelConnectionFactory(ConnectionFactory rabbitConnectionFactory
88100
public void setPoolConfigurer(BiConsumer<GenericObjectPool<Channel>, Boolean> poolConfigurer) {
89101
Assert.notNull(poolConfigurer, "'poolConfigurer' cannot be null");
90102
this.poolConfigurer = poolConfigurer; // NOSONAR - sync inconsistency
103+
if (this.defaultPublisherFactory) {
104+
((PooledChannelConnectionFactory) getPublisherConnectionFactory()).setPoolConfigurer(poolConfigurer);
105+
}
91106
}
92107

93108
@Override
@@ -101,6 +116,10 @@ public boolean isSimplePublisherConfirms() {
101116
*/
102117
public void setSimplePublisherConfirms(boolean simplePublisherConfirms) {
103118
this.simplePublisherConfirms = simplePublisherConfirms;
119+
if (this.defaultPublisherFactory) {
120+
((ThreadChannelConnectionFactory) getPublisherConnectionFactory())
121+
.setSimplePublisherConfirms(simplePublisherConfirms);
122+
}
104123
}
105124

106125
@Override
@@ -117,7 +136,7 @@ public synchronized Connection createConnection() throws AmqpException {
117136
if (this.connection == null || !this.connection.isOpen()) {
118137
Connection bareConnection = createBareConnection(); // NOSONAR - see destroy()
119138
this.connection = new ConnectionWrapper(bareConnection.getDelegate(), getCloseTimeout(), // NOSONAR
120-
this.simplePublisherConfirms, this.poolConfigurer, getChannelListener());
139+
this.simplePublisherConfirms, this.poolConfigurer, getChannelListener()); // NOSONAR
121140
getConnectionListener().onCreate(this.connection);
122141
}
123142
return this.connection;

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/connection/ThreadChannelConnectionFactory.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.springframework.amqp.rabbit.support.RabbitExceptionTranslator;
2828
import org.springframework.aop.framework.ProxyFactory;
2929
import org.springframework.aop.support.NameMatchMethodPointcutAdvisor;
30+
import org.springframework.lang.Nullable;
3031

3132
import com.rabbitmq.client.Channel;
3233
import com.rabbitmq.client.ConnectionFactory;
@@ -47,6 +48,8 @@ public class ThreadChannelConnectionFactory extends AbstractConnectionFactory im
4748

4849
private boolean simplePublisherConfirms;
4950

51+
private boolean defaultPublisherFactory = true;
52+
5053
/**
5154
* Construct an instance.
5255
* @param rabbitConnectionFactory the rabbitmq connection factory.
@@ -65,6 +68,15 @@ private ThreadChannelConnectionFactory(ConnectionFactory rabbitConnectionFactory
6568
if (!isPublisher) {
6669
setPublisherConnectionFactory(new ThreadChannelConnectionFactory(rabbitConnectionFactory, true));
6770
}
71+
else {
72+
this.defaultPublisherFactory = false;
73+
}
74+
}
75+
76+
@Override
77+
public void setPublisherConnectionFactory(@Nullable AbstractConnectionFactory publisherConnectionFactory) {
78+
super.setPublisherConnectionFactory(publisherConnectionFactory);
79+
this.defaultPublisherFactory = false;
6880
}
6981

7082
@Override
@@ -78,6 +90,10 @@ public boolean isSimplePublisherConfirms() {
7890
*/
7991
public void setSimplePublisherConfirms(boolean simplePublisherConfirms) {
8092
this.simplePublisherConfirms = simplePublisherConfirms;
93+
if (this.defaultPublisherFactory) {
94+
((ThreadChannelConnectionFactory) getPublisherConnectionFactory())
95+
.setSimplePublisherConfirms(simplePublisherConfirms);
96+
}
8197
}
8298

8399
@Override

0 commit comments

Comments
 (0)