Skip to content

Commit 638d104

Browse files
committed
Fix new Sonar smells
1 parent 280c2b1 commit 638d104

File tree

3 files changed

+61
-53
lines changed

3 files changed

+61
-53
lines changed

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/core/RabbitTemplate.java

Lines changed: 54 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@
148148
*/
149149
public class RabbitTemplate extends RabbitAccessor // NOSONAR type line count
150150
implements BeanFactoryAware, RabbitOperations, MessageListener,
151-
ListenerContainerAware, PublisherCallbackChannel.Listener, Lifecycle, BeanNameAware {
151+
ListenerContainerAware, PublisherCallbackChannel.Listener, Lifecycle, BeanNameAware {
152152

153153
private static final String UNCHECKED = "unchecked";
154154

@@ -894,9 +894,9 @@ protected void doStart() {
894894
public final void stop() {
895895
synchronized (this.directReplyToContainers) {
896896
this.directReplyToContainers.values()
897-
.stream()
898-
.filter(AbstractMessageListenerContainer::isRunning)
899-
.forEach(AbstractMessageListenerContainer::stop);
897+
.stream()
898+
.filter(AbstractMessageListenerContainer::isRunning)
899+
.forEach(AbstractMessageListenerContainer::stop);
900900
this.directReplyToContainers.clear();
901901
}
902902
doStop();
@@ -958,15 +958,13 @@ protected boolean useDirectReplyTo() {
958958
while (cause != null && !(cause instanceof ShutdownSignalException)) {
959959
cause = cause.getCause();
960960
}
961-
if (cause instanceof ShutdownSignalException) {
962-
if (RabbitUtils.isPassiveDeclarationChannelClose((ShutdownSignalException) cause)) {
963-
if (logger.isWarnEnabled()) {
964-
logger.warn("Broker does not support fast replies via 'amq.rabbitmq.reply-to', temporary "
965-
+ "queues will be used: " + cause.getMessage() + ".");
966-
}
967-
this.replyAddress = null;
968-
return false;
961+
if (cause != null && RabbitUtils.isPassiveDeclarationChannelClose((ShutdownSignalException) cause)) {
962+
if (logger.isWarnEnabled()) {
963+
logger.warn("Broker does not support fast replies via 'amq.rabbitmq.reply-to', temporary "
964+
+ "queues will be used: " + cause.getMessage() + ".");
969965
}
966+
this.replyAddress = null;
967+
return false;
970968
}
971969
if (logger.isDebugEnabled()) {
972970
logger.debug("IO error, deferring directReplyTo detection: " + ex.toString());
@@ -1001,7 +999,7 @@ public void send(final String exchange, final String routingKey,
1001999
(RabbitTemplate.this.returnCallback != null
10021000
|| (correlationData != null && StringUtils.hasText(correlationData.getId())))
10031001
&& RabbitTemplate.this.mandatoryExpression.getValue(
1004-
RabbitTemplate.this.evaluationContext, message, Boolean.class),
1002+
RabbitTemplate.this.evaluationContext, message, Boolean.class),
10051003
correlationData);
10061004
return null;
10071005
}, obtainTargetConnectionFactory(this.sendConnectionFactorySelectorExpression, message));
@@ -1262,21 +1260,24 @@ public <R, S> boolean receiveAndReply(final String queueName, ReceiveAndReplyCal
12621260
}
12631261

12641262
@Override
1265-
public <R, S> boolean receiveAndReply(ReceiveAndReplyCallback<R, S> callback, final String exchange, final String routingKey)
1266-
throws AmqpException {
1263+
public <R, S> boolean receiveAndReply(ReceiveAndReplyCallback<R, S> callback, final String exchange,
1264+
final String routingKey) throws AmqpException {
1265+
12671266
return receiveAndReply(this.getRequiredQueue(), callback, exchange, routingKey);
12681267
}
12691268

12701269
@Override
1271-
public <R, S> boolean receiveAndReply(final String queueName, ReceiveAndReplyCallback<R, S> callback, final String replyExchange,
1272-
final String replyRoutingKey) throws AmqpException {
1270+
public <R, S> boolean receiveAndReply(final String queueName, ReceiveAndReplyCallback<R, S> callback,
1271+
final String replyExchange, final String replyRoutingKey) throws AmqpException {
1272+
12731273
return receiveAndReply(queueName, callback,
12741274
(request, reply) -> new Address(replyExchange, replyRoutingKey));
12751275
}
12761276

12771277
@Override
1278-
public <R, S> boolean receiveAndReply(ReceiveAndReplyCallback<R, S> callback, ReplyToAddressCallback<S> replyToAddressCallback)
1279-
throws AmqpException {
1278+
public <R, S> boolean receiveAndReply(ReceiveAndReplyCallback<R, S> callback,
1279+
ReplyToAddressCallback<S> replyToAddressCallback) throws AmqpException {
1280+
12801281
return receiveAndReply(this.getRequiredQueue(), callback, replyToAddressCallback);
12811282
}
12821283

@@ -1290,18 +1291,17 @@ private <R, S> boolean doReceiveAndReply(final String queueName, final ReceiveAn
12901291
final ReplyToAddressCallback<S> replyToAddressCallback) throws AmqpException {
12911292

12921293
Boolean result = execute(channel -> {
1293-
Message receiveMessage = receiveForReply(queueName, channel);
1294-
if (receiveMessage != null) {
1295-
return sendReply(callback, replyToAddressCallback, channel, receiveMessage);
1296-
}
1297-
return false;
1298-
}, obtainTargetConnectionFactory(this.receiveConnectionFactorySelectorExpression, queueName));
1294+
Message receiveMessage = receiveForReply(queueName, channel);
1295+
if (receiveMessage != null) {
1296+
return sendReply(callback, replyToAddressCallback, channel, receiveMessage);
1297+
}
1298+
return false;
1299+
}, obtainTargetConnectionFactory(this.receiveConnectionFactorySelectorExpression, queueName));
12991300
return result == null ? false : result;
13001301
}
13011302

13021303
@Nullable
13031304
private Message receiveForReply(final String queueName, Channel channel) throws IOException {
1304-
13051305
boolean channelTransacted = isChannelTransacted();
13061306
boolean channelLocallyTransacted = isChannelLocallyTransacted(channel);
13071307
Message receiveMessage = null;
@@ -1358,7 +1358,7 @@ private Delivery consumeDelivery(Channel channel, String queueName, long timeout
13581358
DefaultConsumer consumer = null;
13591359
try {
13601360
consumer = createConsumer(queueName, channel, future,
1361-
timeoutMillis < 0 ? DEFAULT_CONSUME_TIMEOUT : timeoutMillis);
1361+
timeoutMillis < 0 ? DEFAULT_CONSUME_TIMEOUT : timeoutMillis);
13621362
if (timeoutMillis < 0) {
13631363
delivery = future.get();
13641364
}
@@ -1404,7 +1404,7 @@ else if (logger.isDebugEnabled()) {
14041404
@SuppressWarnings(UNCHECKED)
14051405
private <R, S> boolean sendReply(final ReceiveAndReplyCallback<R, S> callback,
14061406
final ReplyToAddressCallback<S> replyToAddressCallback, Channel channel, Message receiveMessage)
1407-
throws IOException {
1407+
throws IOException {
14081408

14091409
Object receive = receiveMessage;
14101410
if (!(ReceiveAndReplyMessageCallback.class.isAssignableFrom(callback.getClass()))) {
@@ -1583,8 +1583,10 @@ public Object convertSendAndReceive(final String routingKey, final Object messag
15831583

15841584
@Override
15851585
@Nullable
1586-
public Object convertSendAndReceive(final String routingKey, final Object message, final MessagePostProcessor messagePostProcessor,
1587-
@Nullable CorrelationData correlationData) throws AmqpException {
1586+
public Object convertSendAndReceive(final String routingKey, final Object message,
1587+
final MessagePostProcessor messagePostProcessor, @Nullable CorrelationData correlationData)
1588+
throws AmqpException {
1589+
15881590
return convertSendAndReceive(this.exchange, routingKey, message, messagePostProcessor, correlationData);
15891591
}
15901592

@@ -1637,7 +1639,7 @@ public <T> T convertSendAndReceiveAsType(final String routingKey, final Object m
16371639
@Nullable
16381640
public <T> T convertSendAndReceiveAsType(final String routingKey, final Object message,
16391641
@Nullable CorrelationData correlationData, ParameterizedTypeReference<T> responseType)
1640-
throws AmqpException {
1642+
throws AmqpException {
16411643

16421644
return convertSendAndReceiveAsType(this.exchange, routingKey, message, null, correlationData, responseType);
16431645
}
@@ -1664,7 +1666,7 @@ public <T> T convertSendAndReceiveAsType(final Object message,
16641666
public <T> T convertSendAndReceiveAsType(final Object message,
16651667
@Nullable final MessagePostProcessor messagePostProcessor,
16661668
@Nullable CorrelationData correlationData, ParameterizedTypeReference<T> responseType)
1667-
throws AmqpException {
1669+
throws AmqpException {
16681670

16691671
return convertSendAndReceiveAsType(this.exchange, this.routingKey, message, messagePostProcessor,
16701672
correlationData, responseType);
@@ -1674,7 +1676,7 @@ public <T> T convertSendAndReceiveAsType(final Object message,
16741676
@Nullable
16751677
public <T> T convertSendAndReceiveAsType(final String routingKey, final Object message,
16761678
@Nullable final MessagePostProcessor messagePostProcessor, ParameterizedTypeReference<T> responseType)
1677-
throws AmqpException {
1679+
throws AmqpException {
16781680

16791681
return convertSendAndReceiveAsType(routingKey, message, messagePostProcessor, null, responseType);
16801682
}
@@ -1684,6 +1686,7 @@ public <T> T convertSendAndReceiveAsType(final String routingKey, final Object m
16841686
public <T> T convertSendAndReceiveAsType(final String routingKey, final Object message,
16851687
@Nullable final MessagePostProcessor messagePostProcessor, @Nullable CorrelationData correlationData,
16861688
ParameterizedTypeReference<T> responseType) throws AmqpException {
1689+
16871690
return convertSendAndReceiveAsType(this.exchange, routingKey, message, messagePostProcessor, correlationData,
16881691
responseType);
16891692
}
@@ -1693,6 +1696,7 @@ public <T> T convertSendAndReceiveAsType(final String routingKey, final Object m
16931696
public <T> T convertSendAndReceiveAsType(final String exchange, final String routingKey, final Object message,
16941697
final MessagePostProcessor messagePostProcessor, ParameterizedTypeReference<T> responseType)
16951698
throws AmqpException {
1699+
16961700
return convertSendAndReceiveAsType(exchange, routingKey, message, messagePostProcessor, null, responseType);
16971701
}
16981702

@@ -1754,6 +1758,7 @@ protected Message convertMessageIfNecessary(final Object object) {
17541758
@Nullable
17551759
protected Message doSendAndReceive(final String exchange, final String routingKey, final Message message,
17561760
@Nullable CorrelationData correlationData) {
1761+
17571762
if (!this.evaluatedFastReplyTo) {
17581763
synchronized (this) {
17591764
if (!this.evaluatedFastReplyTo) {
@@ -1776,13 +1781,15 @@ else if (this.replyAddress == null || this.usingFastReplyTo) {
17761781
@Nullable
17771782
protected Message doSendAndReceiveWithTemporary(final String exchange, final String routingKey,
17781783
final Message message, final CorrelationData correlationData) {
1784+
17791785
return execute(channel -> {
17801786
final PendingReply pendingReply = new PendingReply();
17811787
String messageTag = String.valueOf(RabbitTemplate.this.messageTagProvider.incrementAndGet());
17821788
RabbitTemplate.this.replyHolder.putIfAbsent(messageTag, pendingReply);
17831789

17841790
Assert.isNull(message.getMessageProperties().getReplyTo(),
1785-
"Send-and-receive methods can only be used if the Message does not already have a replyTo property.");
1791+
"Send-and-receive methods can only be used " +
1792+
"if the Message does not already have a replyTo property.");
17861793
String replyTo;
17871794
if (RabbitTemplate.this.usingFastReplyTo) {
17881795
replyTo = Address.AMQ_RABBITMQ_REPLY_TO;
@@ -2065,8 +2072,9 @@ private <T> T doExecute(ChannelCallback<T> action, ConnectionFactory connectionF
20652072
Connection connection = null; // NOSONAR (close)
20662073
if (channel == null) {
20672074
if (isChannelTransacted()) {
2068-
resourceHolder = ConnectionFactoryUtils.
2069-
getTransactionalResourceHolder(connectionFactory, true, this.usePublisherConnection);
2075+
resourceHolder =
2076+
ConnectionFactoryUtils.getTransactionalResourceHolder(connectionFactory,
2077+
true, this.usePublisherConnection);
20702078
channel = resourceHolder.getChannel();
20712079
if (channel == null) {
20722080
ConnectionFactoryUtils.releaseResources(resourceHolder);
@@ -2266,8 +2274,7 @@ public void determineConfirmsReturnsCapability(ConnectionFactory connectionFacto
22662274
* @throws IOException If thrown by RabbitMQ API methods.
22672275
*/
22682276
public void doSend(Channel channel, String exchangeArg, String routingKeyArg, Message message, // NOSONAR complexity
2269-
boolean mandatory, @Nullable CorrelationData correlationData)
2270-
throws IOException {
2277+
boolean mandatory, @Nullable CorrelationData correlationData) throws IOException {
22712278

22722279
String exch = exchangeArg;
22732280
String rKey = routingKeyArg;
@@ -2353,7 +2360,8 @@ private Message buildMessageFromDelivery(Delivery delivery) {
23532360
}
23542361

23552362
private Message buildMessageFromResponse(GetResponse response) {
2356-
return buildMessage(response.getEnvelope(), response.getProps(), response.getBody(), response.getMessageCount());
2363+
return buildMessage(response.getEnvelope(), response.getProps(), response.getBody(),
2364+
response.getMessageCount());
23572365
}
23582366

23592367
private Message buildMessage(Envelope envelope, BasicProperties properties, byte[] body, int msgCount) {
@@ -2414,7 +2422,8 @@ private Address getReplyToAddress(Message request) throws AmqpException {
24142422
if (this.exchange == null) {
24152423
throw new AmqpException(
24162424
"Cannot determine ReplyTo message property value: "
2417-
+ "Request message does not contain reply-to property, and no default Exchange was set.");
2425+
+ "Request message does not contain reply-to property, " +
2426+
"and no default Exchange was set.");
24182427
}
24192428
replyTo = new Address(this.exchange, this.routingKey);
24202429
}
@@ -2448,7 +2457,8 @@ public void addListener(Channel channel) {
24482457
@Override
24492458
public void handleConfirm(PendingConfirm pendingConfirm, boolean ack) {
24502459
if (this.confirmCallback != null) {
2451-
this.confirmCallback.confirm(pendingConfirm.getCorrelationData(), ack, pendingConfirm.getCause()); // NOSONAR never null
2460+
this.confirmCallback
2461+
.confirm(pendingConfirm.getCorrelationData(), ack, pendingConfirm.getCause()); // NOSONAR never null
24522462
}
24532463
}
24542464

@@ -2569,7 +2579,7 @@ private void restoreProperties(Message message, PendingReply pendingReply) {
25692579

25702580
private DefaultConsumer createConsumer(final String queueName, Channel channel,
25712581
CompletableFuture<Delivery> future, long timeoutMillis) throws IOException, TimeoutException,
2572-
InterruptedException {
2582+
InterruptedException {
25732583

25742584
channel.basicQos(1);
25752585
final CountDownLatch latch = new CountDownLatch(1);
@@ -2717,8 +2727,8 @@ public interface ReturnCallback {
27172727
* @param exchange the exchange.
27182728
* @param routingKey the routing key.
27192729
*/
2720-
void returnedMessage(Message message, int replyCode, String replyText,
2721-
String exchange, String routingKey);
2730+
void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey);
2731+
27222732
}
27232733

27242734
}

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/listener/DirectMessageListenerContainer.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,7 @@ private SimpleConsumer consume(String queue, Connection connection) {
720720
}
721721

722722
@Nullable
723-
private SimpleConsumer handleConsumeException(String queue, SimpleConsumer consumerArg, Exception e) {
723+
private SimpleConsumer handleConsumeException(String queue, @Nullable SimpleConsumer consumerArg, Exception e) {
724724

725725
SimpleConsumer consumer = consumerArg;
726726
if (e.getCause() instanceof ShutdownSignalException
@@ -732,7 +732,7 @@ private SimpleConsumer handleConsumeException(String queue, SimpleConsumer consu
732732
else if (e.getCause() instanceof ShutdownSignalException
733733
&& RabbitUtils.isPassiveDeclarationChannelClose((ShutdownSignalException) e.getCause())) {
734734
this.logger.error("Queue not present, scheduling consumer "
735-
+ (consumer == null ? "for queue " + queue : consumer) + " for restart", e);
735+
+ (consumer == null ? "for queue " + queue : consumer) + " for restart", e);
736736
}
737737
else if (this.logger.isWarnEnabled()) {
738738
this.logger.warn("basicConsume failed, scheduling consumer "
@@ -755,7 +755,8 @@ protected void doShutdown() {
755755
boolean waitForConsumers = false;
756756
synchronized (this.consumersMonitor) {
757757
if (this.started || this.aborted) {
758-
// Copy in the same order to avoid ConcurrentModificationException during remove in the cancelConsumer().
758+
// Copy in the same order to avoid ConcurrentModificationException during remove in the
759+
// cancelConsumer().
759760
canceledConsumers = new LinkedList<>(this.consumers);
760761
actualShutDown(canceledConsumers);
761762
waitForConsumers = true;
@@ -879,8 +880,6 @@ final class SimpleConsumer extends DefaultConsumer {
879880

880881
private long lastAck = System.currentTimeMillis();
881882

882-
private long lastDeliveryComplete = this.lastAck;
883-
884883
private long latestDeferredDeliveryTag;
885884

886885
private volatile String consumerTag;
@@ -893,7 +892,7 @@ final class SimpleConsumer extends DefaultConsumer {
893892

894893
private volatile boolean ackFailed;
895894

896-
SimpleConsumer(Connection connection, Channel channel, String queue) {
895+
SimpleConsumer(@Nullable Connection connection, @Nullable Channel channel, String queue) {
897896
super(channel);
898897
this.connection = connection;
899898
this.queue = queue;
@@ -1094,8 +1093,7 @@ private void handleAck(long deliveryTag, boolean channelLocallyTransacted) {
10941093
synchronized (this) {
10951094
this.latestDeferredDeliveryTag = deliveryTag;
10961095
this.pendingAcks++;
1097-
this.lastDeliveryComplete = System.currentTimeMillis();
1098-
ackIfNecessary(this.lastDeliveryComplete);
1096+
ackIfNecessary(this.lastAck);
10991097
}
11001098
}
11011099
else if (!isChannelTransacted() || isLocallyTransacted) {

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/listener/adapter/AbstractAdaptableMessageListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ public Object getResult() {
598598

599599
}
600600

601-
private static class MonoHandler {
601+
private static class MonoHandler { // NOSONAR - pointless to name it ..Utils|Helper
602602

603603
static boolean isMono(Object result) {
604604
return result instanceof Mono;

0 commit comments

Comments
 (0)