Skip to content

Commit ed6f76b

Browse files
garyrussellartembilan
authored andcommitted
More Sonar Fixes
* More * Deprecate template.setQueue() in favor of setDefaultReceiveQueue() Causes confusion since it has no relevance to send methods. * Fix deprecations; test. * Fix test waiting on wrong latch. * Increase 10k confirms wait time (test).
1 parent 50d6265 commit ed6f76b

20 files changed

+428
-328
lines changed

spring-amqp/src/main/java/org/springframework/amqp/core/MessageProperties.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -531,8 +531,8 @@ public void setTargetBean(Object targetBean) {
531531
}
532532
}
533533

534-
@Override
535-
public int hashCode() { // NOSONAR complexity
534+
@Override // NOSONAR complexity
535+
public int hashCode() {
536536
final int prime = 31;
537537
int result = 1;
538538
result = prime * result + ((this.appId == null) ? 0 : this.appId.hashCode());
@@ -558,8 +558,8 @@ public int hashCode() { // NOSONAR complexity
558558
return result;
559559
}
560560

561-
@Override
562-
public boolean equals(Object obj) { // NOSONAR complexity
561+
@Override // NOSONAR complexity
562+
public boolean equals(Object obj) { // NOSONAR line count
563563
if (this == obj) {
564564
return true;
565565
}
@@ -715,8 +715,8 @@ else if (!this.userId.equals(other.userId)) {
715715
return true;
716716
}
717717

718-
@Override
719-
public String toString() { // NOSONAR complexity
718+
@Override // NOSONAR complexity
719+
public String toString() {
720720
return "MessageProperties [headers=" + this.headers
721721
+ (this.timestamp == null ? "" : ", timestamp=" + this.timestamp)
722722
+ (this.messageId == null ? "" : ", messageId=" + this.messageId)

spring-amqp/src/main/java/org/springframework/amqp/remoting/service/AmqpInvokerServiceExporter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -71,7 +71,7 @@ public void onMessage(Message message) {
7171
Object invocationRaw = this.messageConverter.fromMessage(message);
7272

7373
RemoteInvocationResult remoteInvocationResult;
74-
if (invocationRaw == null || !(invocationRaw instanceof RemoteInvocation)) {
74+
if (!(invocationRaw instanceof RemoteInvocation)) {
7575
remoteInvocationResult = new RemoteInvocationResult(
7676
new IllegalArgumentException("The message does not contain a RemoteInvocation payload"));
7777
}

spring-amqp/src/main/java/org/springframework/amqp/support/AmqpMessageHeaderAccessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public Long getContentLength() {
8787
@Override
8888
public MimeType getContentType() {
8989
Object value = getHeader(AmqpHeaders.CONTENT_TYPE);
90-
if (value != null && value instanceof String) {
90+
if (value instanceof String) {
9191
return MimeType.valueOf((String) value);
9292
}
9393
else {

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/config/AbstractRabbitListenerContainerFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ public void setContainerConfigurer(Consumer<C> configurer) {
342342
this.containerConfigurer = configurer;
343343
}
344344

345-
@SuppressWarnings("deprecation")
345+
@SuppressWarnings("deprecation") // NOSONAR complexity - mostly null checks
346346
@Override
347347
public C createListenerContainer(RabbitListenerEndpoint endpoint) { // NOSONAR complexity
348348
C instance = createContainerInstance();

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/config/ListenerContainerFactoryBean.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ public Class<?> getObjectType() {
404404
return this.listenerContainer == null ? AbstractMessageListenerContainer.class : this.listenerContainer.getClass();
405405
}
406406

407-
@SuppressWarnings("deprecation")
407+
@SuppressWarnings("deprecation") // NOSONAR complexity - mostly null checks
408408
@Override
409409
protected AbstractMessageListenerContainer createInstance() { // NOSONAR complexity
410410
if (this.listenerContainer == null) {
@@ -529,7 +529,7 @@ protected AbstractMessageListenerContainer createInstance() { // NOSONAR complex
529529
return this.listenerContainer;
530530
}
531531

532-
private AbstractMessageListenerContainer createContainer() {
532+
private AbstractMessageListenerContainer createContainer() { // NOSONAR complexity - mostly null checks
533533
if (this.type.equals(Type.simple)) {
534534
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(this.connectionFactory);
535535
if (this.concurrentConsumers != null) {

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/config/SimpleRabbitListenerContainerFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ protected SimpleMessageListenerContainer createContainerInstance() {
134134
return new SimpleMessageListenerContainer();
135135
}
136136

137-
@Override
137+
@Override // NOSONAR complexity = mostly null checks
138138
protected void initializeContainer(SimpleMessageListenerContainer instance, RabbitListenerEndpoint endpoint) {
139139
super.initializeContainer(instance, endpoint);
140140

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

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -480,22 +480,11 @@ else if (protocolClassId == 10) {
480480
}
481481

482482
private Channel getChannel(ChannelCachingConnectionProxy connection, boolean transactional) {
483-
Semaphore checkoutPermits = null;
483+
Semaphore permits = null;
484484
if (this.channelCheckoutTimeout > 0) {
485-
checkoutPermits = obtainPermit(connection);
486-
}
487-
LinkedList<ChannelProxy> channelList;
488-
if (this.cacheMode == CacheMode.CHANNEL) {
489-
channelList = transactional ? this.cachedChannelsTransactional
490-
: this.cachedChannelsNonTransactional;
491-
}
492-
else {
493-
channelList = transactional ? this.allocatedConnectionTransactionalChannels.get(connection)
494-
: this.allocatedConnectionNonTransactionalChannels.get(connection);
495-
}
496-
if (channelList == null) {
497-
throw new IllegalStateException("No channel list for connection " + connection);
485+
permits = obtainPermits(connection);
498486
}
487+
LinkedList<ChannelProxy> channelList = determineChannelList(connection, transactional);
499488
ChannelProxy channel = null;
500489
if (connection.isOpen()) {
501490
channel = findOpenChannel(channelList, channel);
@@ -510,11 +499,11 @@ private Channel getChannel(ChannelCachingConnectionProxy connection, boolean tra
510499
channel = getCachedChannelProxy(connection, channelList, transactional);
511500
}
512501
catch (RuntimeException e) {
513-
if (checkoutPermits != null) {
514-
checkoutPermits.release();
502+
if (permits != null) {
503+
permits.release();
515504
if (logger.isDebugEnabled()) {
516505
logger.debug("Could not get channel; released permit for " + connection + ", remaining:"
517-
+ checkoutPermits.availablePermits());
506+
+ permits.availablePermits());
518507
}
519508
}
520509
throw e;
@@ -523,7 +512,7 @@ private Channel getChannel(ChannelCachingConnectionProxy connection, boolean tra
523512
return channel;
524513
}
525514

526-
private Semaphore obtainPermit(ChannelCachingConnectionProxy connection) {
515+
private Semaphore obtainPermits(ChannelCachingConnectionProxy connection) {
527516
Semaphore permits;
528517
permits = this.checkoutPermits.get(connection);
529518
if (permits != null) {
@@ -548,7 +537,8 @@ private Semaphore obtainPermit(ChannelCachingConnectionProxy connection) {
548537
}
549538

550539
private ChannelProxy findOpenChannel(LinkedList<ChannelProxy> channelList, // NOSONAR LinkedList.removeFirst()
551-
ChannelProxy channel) {
540+
ChannelProxy channelArg) {
541+
ChannelProxy channel = channelArg;
552542
synchronized (channelList) {
553543
while (!channelList.isEmpty()) {
554544
channel = channelList.removeFirst();
@@ -595,6 +585,23 @@ private void cleanUpClosedChannel(ChannelProxy channel) {
595585
}
596586
}
597587

588+
private LinkedList<ChannelProxy> determineChannelList(ChannelCachingConnectionProxy connection, // NOSONAR LL
589+
boolean transactional) {
590+
LinkedList<ChannelProxy> channelList; // NOSONAR must be LinkedList
591+
if (this.cacheMode == CacheMode.CHANNEL) {
592+
channelList = transactional ? this.cachedChannelsTransactional
593+
: this.cachedChannelsNonTransactional;
594+
}
595+
else {
596+
channelList = transactional ? this.allocatedConnectionTransactionalChannels.get(connection)
597+
: this.allocatedConnectionNonTransactionalChannels.get(connection);
598+
}
599+
if (channelList == null) {
600+
throw new IllegalStateException("No channel list for connection " + connection);
601+
}
602+
return channelList;
603+
}
604+
598605
private ChannelProxy getCachedChannelProxy(ChannelCachingConnectionProxy connection,
599606
LinkedList<ChannelProxy> channelList, boolean transactional) { //NOSONAR LinkedList for addLast()
600607

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

Lines changed: 56 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,18 @@
1818

1919
import java.net.MalformedURLException;
2020
import java.net.URISyntaxException;
21+
import java.util.AbstractMap.SimpleImmutableEntry;
2122
import java.util.Arrays;
2223
import java.util.HashMap;
2324
import java.util.Map;
25+
import java.util.stream.Collectors;
26+
import java.util.stream.IntStream;
2427

2528
import org.apache.commons.logging.Log;
2629
import org.apache.commons.logging.LogFactory;
2730

2831
import org.springframework.amqp.AmqpException;
32+
import org.springframework.amqp.rabbit.support.RabbitExceptionTranslator;
2933
import org.springframework.beans.factory.DisposableBean;
3034
import org.springframework.core.io.Resource;
3135
import org.springframework.lang.Nullable;
@@ -34,7 +38,6 @@
3438
import com.rabbitmq.http.client.Client;
3539
import com.rabbitmq.http.client.domain.QueueInfo;
3640

37-
3841
/**
3942
* A {@link RoutingConnectionFactory} that determines the node on which a queue is located and
4043
* returns a factory that connects directly to that node.
@@ -96,19 +99,9 @@ public class LocalizedQueueConnectionFactory implements ConnectionFactory, Routi
9699
public LocalizedQueueConnectionFactory(ConnectionFactory defaultConnectionFactory,
97100
Map<String, String> nodeToAddress, String[] adminUris, String vhost, String username, String password,
98101
boolean useSSL, Resource sslPropertiesLocation) {
99-
Assert.notNull(defaultConnectionFactory, "'defaultConnectionFactory' cannot be null");
100-
this.defaultConnectionFactory = defaultConnectionFactory;
101-
this.adminUris = Arrays.copyOf(adminUris, adminUris.length);
102-
this.nodeToAddress.putAll(nodeToAddress);
103-
this.vhost = vhost;
104-
this.username = username;
105-
this.password = password;
106-
this.useSSL = useSSL;
107-
this.sslPropertiesLocation = sslPropertiesLocation;
108-
this.keyStore = null;
109-
this.trustStore = null;
110-
this.keyStorePassPhrase = null;
111-
this.trustStorePassPhrase = null;
102+
103+
this(defaultConnectionFactory, adminUris, nodeToAddress, vhost, username, password, useSSL,
104+
sslPropertiesLocation, null, null, null, null);
112105
}
113106

114107
/**
@@ -129,19 +122,9 @@ public LocalizedQueueConnectionFactory(ConnectionFactory defaultConnectionFactor
129122
Map<String, String> nodeToAddress, String[] adminUris, String vhost, String username, String password,
130123
boolean useSSL, String keyStore, String trustStore,
131124
String keyStorePassPhrase, String trustStorePassPhrase) {
132-
Assert.notNull(defaultConnectionFactory, "'defaultConnectionFactory' cannot be null");
133-
this.defaultConnectionFactory = defaultConnectionFactory;
134-
this.adminUris = Arrays.copyOf(adminUris, adminUris.length);
135-
this.nodeToAddress.putAll(nodeToAddress);
136-
this.vhost = vhost;
137-
this.username = username;
138-
this.password = password;
139-
this.useSSL = useSSL;
140-
this.sslPropertiesLocation = null;
141-
this.keyStore = keyStore;
142-
this.trustStore = trustStore;
143-
this.keyStorePassPhrase = keyStorePassPhrase;
144-
this.trustStorePassPhrase = trustStorePassPhrase;
125+
126+
this(defaultConnectionFactory, adminUris, nodeToAddress, vhost, username, password, useSSL, null,
127+
keyStore, trustStore, keyStorePassPhrase, trustStorePassPhrase);
145128
}
146129

147130
/**
@@ -159,24 +142,10 @@ public LocalizedQueueConnectionFactory(ConnectionFactory defaultConnectionFactor
159142
*/
160143
public LocalizedQueueConnectionFactory(ConnectionFactory defaultConnectionFactory, String[] addresses,
161144
String[] adminUris, String[] nodes, String vhost, String username, String password, boolean useSSL,
162-
Resource sslPropertiesLocation) {
163-
Assert.notNull(defaultConnectionFactory, "'defaultConnectionFactory' cannot be null");
164-
Assert.isTrue(addresses.length == nodes.length,
165-
"'addresses', 'adminAddresses', and 'nodes' properties must have equal length");
166-
this.defaultConnectionFactory = defaultConnectionFactory;
167-
this.adminUris = Arrays.copyOf(adminUris, adminUris.length);
168-
for (int i = 0; i < addresses.length; i++) {
169-
this.nodeToAddress.put(nodes[i], addresses[i]);
170-
}
171-
this.vhost = vhost;
172-
this.username = username;
173-
this.password = password;
174-
this.useSSL = useSSL;
175-
this.sslPropertiesLocation = sslPropertiesLocation;
176-
this.keyStore = null;
177-
this.trustStore = null;
178-
this.keyStorePassPhrase = null;
179-
this.trustStorePassPhrase = null;
145+
@Nullable Resource sslPropertiesLocation) {
146+
147+
this(defaultConnectionFactory, adminUris, nodesAddressesToMap(nodes, addresses), vhost, username, password,
148+
useSSL, sslPropertiesLocation, null, null, null, null);
180149
}
181150

182151
/**
@@ -198,25 +167,40 @@ public LocalizedQueueConnectionFactory(ConnectionFactory defaultConnectionFactor
198167
String[] addresses, String[] adminUris, String[] nodes, String vhost,
199168
String username, String password, boolean useSSL, String keyStore, String trustStore,
200169
String keyStorePassPhrase, String trustStorePassPhrase) {
170+
171+
this(defaultConnectionFactory, adminUris, nodesAddressesToMap(nodes, addresses), vhost, username, password,
172+
useSSL, null, keyStore, trustStore, keyStorePassPhrase, trustStorePassPhrase);
173+
}
174+
175+
private LocalizedQueueConnectionFactory(ConnectionFactory defaultConnectionFactory, String[] adminUris,
176+
Map<String, String> nodeToAddress, String vhost, String username, String password, boolean useSSL,
177+
@Nullable Resource sslPropertiesLocation, @Nullable String keyStore, @Nullable String trustStore,
178+
@Nullable String keyStorePassPhrase, @Nullable String trustStorePassPhrase) {
179+
201180
Assert.notNull(defaultConnectionFactory, "'defaultConnectionFactory' cannot be null");
202-
Assert.isTrue(addresses.length == nodes.length,
203-
"'addresses', 'adminAddresses', and 'nodes' properties must have equal length");
204181
this.defaultConnectionFactory = defaultConnectionFactory;
205182
this.adminUris = Arrays.copyOf(adminUris, adminUris.length);
206-
for (int i = 0; i < addresses.length; i++) {
207-
this.nodeToAddress.put(nodes[i], addresses[i]);
208-
}
183+
this.nodeToAddress.putAll(nodeToAddress);
209184
this.vhost = vhost;
210185
this.username = username;
211186
this.password = password;
212187
this.useSSL = useSSL;
213-
this.sslPropertiesLocation = null;
188+
this.sslPropertiesLocation = sslPropertiesLocation;
214189
this.keyStore = keyStore;
215190
this.trustStore = trustStore;
216191
this.keyStorePassPhrase = keyStorePassPhrase;
217192
this.trustStorePassPhrase = trustStorePassPhrase;
218193
}
219194

195+
private static Map<String, String> nodesAddressesToMap(String[] nodes, String[] addresses) {
196+
Assert.isTrue(addresses.length == nodes.length,
197+
"'addresses' and 'nodes' properties must have equal length");
198+
return IntStream.range(0, addresses.length)
199+
.mapToObj(i -> new SimpleImmutableEntry<>(nodes[i], addresses[i]))
200+
.collect(Collectors.toMap(SimpleImmutableEntry::getKey, SimpleImmutableEntry::getValue,
201+
(u, v) -> v)); // TODO in 2.2 use default throwingMerger() (to catch dups)
202+
}
203+
220204
@Override
221205
public Connection createConnection() throws AmqpException {
222206
return this.defaultConnectionFactory.createConnection();
@@ -320,8 +304,7 @@ protected Client createClient(String adminUri, String username, String password)
320304
return new Client(adminUri, username, password);
321305
}
322306

323-
private synchronized ConnectionFactory nodeConnectionFactory(String queue, String node, String address)
324-
throws Exception {
307+
private synchronized ConnectionFactory nodeConnectionFactory(String queue, String node, String address) {
325308
if (this.logger.isInfoEnabled()) {
326309
this.logger.info("Queue: " + queue + " is on node: " + node + " at: " + address);
327310
}
@@ -341,9 +324,8 @@ private synchronized ConnectionFactory nodeConnectionFactory(String queue, Strin
341324
* @param address the address to which the factory should connect.
342325
* @param node the node.
343326
* @return the connection factory.
344-
* @throws Exception if errors occur during creation.
345327
*/
346-
protected ConnectionFactory createConnectionFactory(String address, String node) throws Exception {
328+
protected ConnectionFactory createConnectionFactory(String address, String node) {
347329
RabbitConnectionFactoryBean rcfb = new RabbitConnectionFactoryBean();
348330
rcfb.setUseSSL(this.useSSL);
349331
rcfb.setSslPropertiesLocation(this.sslPropertiesLocation);
@@ -352,7 +334,14 @@ protected ConnectionFactory createConnectionFactory(String address, String node)
352334
rcfb.setKeyStorePassphrase(this.keyStorePassPhrase);
353335
rcfb.setTrustStorePassphrase(this.trustStorePassPhrase);
354336
rcfb.afterPropertiesSet();
355-
CachingConnectionFactory ccf = new CachingConnectionFactory(rcfb.getObject()); // NOSONAR never null
337+
com.rabbitmq.client.ConnectionFactory rcf;
338+
try {
339+
rcf = rcfb.getObject();
340+
}
341+
catch (Exception e) {
342+
throw RabbitExceptionTranslator.convertRabbitAccessException(e);
343+
}
344+
CachingConnectionFactory ccf = new CachingConnectionFactory(rcf); // NOSONAR never null
356345
ccf.setAddresses(address);
357346
ccf.setUsername(this.username);
358347
ccf.setPassword(this.password);
@@ -362,12 +351,21 @@ protected ConnectionFactory createConnectionFactory(String address, String node)
362351
}
363352

364353
@Override
365-
public void destroy() throws Exception {
354+
public void destroy() {
355+
Exception lastException = null;
366356
for (ConnectionFactory connectionFactory : this.nodeFactories.values()) {
367357
if (connectionFactory instanceof DisposableBean) {
368-
((DisposableBean) connectionFactory).destroy();
358+
try {
359+
((DisposableBean) connectionFactory).destroy();
360+
}
361+
catch (Exception e) {
362+
lastException = e;
363+
}
369364
}
370365
}
366+
if (lastException != null) {
367+
throw RabbitExceptionTranslator.convertRabbitAccessException(lastException);
368+
}
371369
}
372370

373371
}

0 commit comments

Comments
 (0)