Fix new and some old Sonar smells

This commit is contained in:
Artem Bilan
2020-07-14 10:48:23 -04:00
parent df1f260a59
commit a19e37237b
5 changed files with 189 additions and 179 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -44,6 +44,7 @@ import javax.jms.Topic;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.Lifecycle;
import org.springframework.core.convert.ConversionService;
import org.springframework.expression.Expression;
@@ -53,6 +54,7 @@ import org.springframework.integration.handler.AbstractReplyProducingMessageHand
import org.springframework.integration.handler.ExpressionEvaluatingMessageProcessor;
import org.springframework.integration.jms.util.JmsAdapterUtils;
import org.springframework.integration.support.AbstractIntegrationMessageBuilder;
import org.springframework.integration.util.JavaUtils;
import org.springframework.jms.connection.ConnectionFactoryUtils;
import org.springframework.jms.listener.DefaultMessageListenerContainer;
import org.springframework.jms.support.JmsUtils;
@@ -520,61 +522,67 @@ public class JmsOutboundGateway extends AbstractReplyProducingMessageHandler imp
"Exactly one of 'requestDestination', 'requestDestinationName', " +
"or 'requestDestinationExpression' is required.");
ConversionService conversionService = getConversionService();
BeanFactory beanFactory = getBeanFactory();
if (this.requestDestinationExpressionProcessor != null) {
this.requestDestinationExpressionProcessor.setBeanFactory(getBeanFactory());
this.requestDestinationExpressionProcessor.setBeanFactory(beanFactory);
if (conversionService != null) {
this.requestDestinationExpressionProcessor.setConversionService(conversionService);
}
}
if (this.replyDestinationExpressionProcessor != null) {
this.replyDestinationExpressionProcessor.setBeanFactory(getBeanFactory());
this.replyDestinationExpressionProcessor.setBeanFactory(beanFactory);
if (conversionService != null) {
this.replyDestinationExpressionProcessor.setConversionService(conversionService);
}
}
/*
* This is needed because there is no way to detect 2 or more gateways using the same reply queue
* with no correlation key.
*/
boolean hasAReplyDest = this.replyDestination != null || this.replyDestinationName != null
|| this.replyDestinationExpressionProcessor != null;
if (this.useReplyContainer && (this.correlationKey == null && hasAReplyDest)) {
logger.warn("The gateway cannot use a reply listener container with a specified " +
"destination(Name/Expression) " +
"without a 'correlation-key'; " +
"a container will NOT be used; " +
"to avoid this problem, set the 'correlation-key' attribute; " +
"some consumers, including the Spring Integration <jms:inbound-gateway/>, " +
"support the use of the value 'JMSCorrelationID' " +
"for this purpose. Alternatively, do not specify a reply destination " +
"and a temporary queue will be used for replies.");
this.useReplyContainer = false;
}
if (this.useReplyContainer) {
Assert.state(!"JMSCorrelationID*".equals(this.correlationKey),
"Using an existing 'JMSCorrelationID' mapped from the 'requestMessage' ('JMSCorrelationID*') " +
"can't be used when using a 'reply-container'");
GatewayReplyListenerContainer container = new GatewayReplyListenerContainer();
setContainerProperties(container);
container.afterPropertiesSet();
this.replyContainer = container;
if (isAsync() && this.correlationKey == null) {
logger.warn("'async=true' requires a correlationKey; ignored");
setAsync(false);
}
}
else {
if (isAsync()) {
logger.warn("'async=true' is ignored when a reply container is not being used");
setAsync(false);
}
}
initializeReplyContainer();
this.initialized = true;
}
}
private void initializeReplyContainer() {
/*
* This is needed because there is no way to detect 2 or more gateways using the same reply queue
* with no correlation key.
*/
boolean hasAReplyDest = this.replyDestination != null || this.replyDestinationName != null
|| this.replyDestinationExpressionProcessor != null;
if (this.useReplyContainer && (this.correlationKey == null && hasAReplyDest)) {
logger.warn("The gateway cannot use a reply listener container with a specified " +
"destination(Name/Expression) " +
"without a 'correlation-key'; " +
"a container will NOT be used; " +
"to avoid this problem, set the 'correlation-key' attribute; " +
"some consumers, including the Spring Integration <jms:inbound-gateway/>, " +
"support the use of the value 'JMSCorrelationID' " +
"for this purpose. Alternatively, do not specify a reply destination " +
"and a temporary queue will be used for replies.");
this.useReplyContainer = false;
}
if (this.useReplyContainer) {
Assert.state(!"JMSCorrelationID*".equals(this.correlationKey),
"Using an existing 'JMSCorrelationID' mapped from the 'requestMessage' ('JMSCorrelationID*') " +
"can't be used when using a 'reply-container'");
GatewayReplyListenerContainer container = new GatewayReplyListenerContainer();
setContainerProperties(container);
container.afterPropertiesSet();
this.replyContainer = container;
if (isAsync() && this.correlationKey == null) {
logger.warn("'async=true' requires a correlationKey; ignored");
setAsync(false);
}
}
else {
if (isAsync()) {
logger.warn("'async=true' is ignored when a reply container is not being used");
setAsync(false);
}
}
}
private void setContainerProperties(GatewayReplyListenerContainer container) {
container.setConnectionFactory(this.connectionFactory);
if (this.replyDestination != null) {
container.setDestination(this.replyDestination);
}
@@ -594,61 +602,57 @@ public class JmsOutboundGateway extends AbstractReplyProducingMessageHandler imp
container.setMessageSelector(messageSelector);
}
container.setMessageListener(this);
applyReplyContainerProperties(container);
}
private void applyReplyContainerProperties(GatewayReplyListenerContainer container) {
if (this.replyContainerProperties != null) {
if (this.replyContainerProperties.isSessionTransacted() != null) {
container.setSessionTransacted(this.replyContainerProperties.isSessionTransacted());
}
if (this.replyContainerProperties.getCacheLevel() != null) {
container.setCacheLevel(this.replyContainerProperties.getCacheLevel());
}
if (this.replyContainerProperties.getConcurrentConsumers() != null) {
container.setConcurrentConsumers(this.replyContainerProperties.getConcurrentConsumers());
}
if (this.replyContainerProperties.getIdleConsumerLimit() != null) {
container.setIdleConsumerLimit(this.replyContainerProperties.getIdleConsumerLimit());
}
if (this.replyContainerProperties.getIdleTaskExecutionLimit() != null) {
container.setIdleTaskExecutionLimit(this.replyContainerProperties.getIdleTaskExecutionLimit());
}
if (this.replyContainerProperties.getMaxConcurrentConsumers() != null) {
container.setMaxConcurrentConsumers(this.replyContainerProperties.getMaxConcurrentConsumers());
}
if (this.replyContainerProperties.getMaxMessagesPerTask() != null) {
container.setMaxMessagesPerTask(this.replyContainerProperties.getMaxMessagesPerTask());
}
if (this.replyContainerProperties.getReceiveTimeout() != null) {
container.setReceiveTimeout(this.replyContainerProperties.getReceiveTimeout());
}
if (this.replyContainerProperties.getRecoveryInterval() != null) {
container.setRecoveryInterval(this.replyContainerProperties.getRecoveryInterval());
}
if (StringUtils.hasText(this.replyContainerProperties.getSessionAcknowledgeModeName())) {
Integer acknowledgeMode = JmsAdapterUtils.parseAcknowledgeMode(
this.replyContainerProperties.getSessionAcknowledgeModeName());
if (acknowledgeMode != null) {
if (JmsAdapterUtils.SESSION_TRANSACTED == acknowledgeMode) {
container.setSessionTransacted(true);
}
else {
container.setSessionAcknowledgeMode(acknowledgeMode);
}
}
}
else if (this.replyContainerProperties.getSessionAcknowledgeMode() != null) {
Integer sessionAcknowledgeMode = this.replyContainerProperties.getSessionAcknowledgeMode();
if (Session.SESSION_TRANSACTED == sessionAcknowledgeMode) {
container.setSessionTransacted(true);
}
else {
container.setSessionAcknowledgeMode(sessionAcknowledgeMode);
}
JavaUtils.INSTANCE
.acceptIfNotNull(this.replyContainerProperties.isSessionTransacted(),
container::setSessionTransacted)
.acceptIfNotNull(this.replyContainerProperties.getCacheLevel(),
container::setCacheLevel)
.acceptIfNotNull(this.replyContainerProperties.getConcurrentConsumers(),
container::setConcurrentConsumers)
.acceptIfNotNull(this.replyContainerProperties.getIdleConsumerLimit(),
container::setIdleConsumerLimit)
.acceptIfNotNull(this.replyContainerProperties.getIdleTaskExecutionLimit(),
container::setIdleTaskExecutionLimit)
.acceptIfNotNull(this.replyContainerProperties.getMaxConcurrentConsumers(),
container::setMaxConcurrentConsumers)
.acceptIfNotNull(this.replyContainerProperties.getMaxMessagesPerTask(),
container::setMaxMessagesPerTask)
.acceptIfNotNull(this.replyContainerProperties.getReceiveTimeout(),
container::setReceiveTimeout)
.acceptIfNotNull(this.replyContainerProperties.getRecoveryInterval(),
container::setRecoveryInterval)
.acceptIfHasText(this.replyContainerProperties.getSessionAcknowledgeModeName(),
acknowledgeModeName -> {
Integer acknowledgeMode = JmsAdapterUtils.parseAcknowledgeMode(
this.replyContainerProperties.getSessionAcknowledgeModeName());
if (acknowledgeMode != null) {
if (JmsAdapterUtils.SESSION_TRANSACTED == acknowledgeMode) {
container.setSessionTransacted(true);
}
else {
container.setSessionAcknowledgeMode(acknowledgeMode);
}
}
})
.acceptIfNotNull(this.replyContainerProperties.getSessionAcknowledgeMode(),
acknowledgeMode -> {
if (Session.SESSION_TRANSACTED == acknowledgeMode) {
container.setSessionTransacted(true);
}
else {
container.setSessionAcknowledgeMode(acknowledgeMode);
}
})
.acceptIfNotNull(this.replyContainerProperties.getTaskExecutor(),
container::setTaskExecutor);
}
if (this.replyContainerProperties.getTaskExecutor() != null) {
container.setTaskExecutor(this.replyContainerProperties.getTaskExecutor());
}
else {
if (this.replyContainerProperties.getTaskExecutor() == null) {
// set the beanName so the default TE threads get a meaningful name
String containerBeanName = this.getComponentName();
containerBeanName = ((!StringUtils.hasText(containerBeanName)
@@ -985,8 +989,8 @@ public class JmsOutboundGateway extends AbstractReplyProducingMessageHandler imp
* If the replyTo is not temporary, and the connection is lost while waiting for a reply, reconnect for
* up to receiveTimeout.
*/
private javax.jms.Message retryableReceiveReply(Session session, Destination replyTo, String messageSelector)
throws JMSException {
private javax.jms.Message retryableReceiveReply(Session session, Destination replyTo, // NOSONAR
String messageSelector) throws JMSException {
Connection consumerConnection = null; //NOSONAR
Session consumerSession = session;
@@ -996,17 +1000,15 @@ public class JmsOutboundGateway extends AbstractReplyProducingMessageHandler imp
long replyTimeout = isTemporaryReplyTo
? Long.MIN_VALUE
: this.receiveTimeout < 0
? Long.MAX_VALUE
: System.currentTimeMillis() + this.receiveTimeout;
? Long.MAX_VALUE
: System.currentTimeMillis() + this.receiveTimeout;
try {
do {
try {
messageConsumer = consumerSession.createConsumer(replyTo, messageSelector);
javax.jms.Message reply = receiveReplyMessage(messageConsumer);
if (reply == null) {
if (replyTimeout > System.currentTimeMillis()) {
throw new JMSException("Consumer closed before timeout");
}
if (reply == null && replyTimeout > System.currentTimeMillis()) {
throw new JMSException("Consumer closed before timeout");
}
return reply;
}
@@ -1027,7 +1029,7 @@ public class JmsOutboundGateway extends AbstractReplyProducingMessageHandler imp
logger.debug("Could not reconnect, retrying: " + ee.getMessage());
}
try {
Thread.sleep(1000);
Thread.sleep(1000); // NOSONAR
}
catch (@SuppressWarnings("unused") InterruptedException e1) {
Thread.currentThread().interrupt();
@@ -1377,10 +1379,10 @@ public class JmsOutboundGateway extends AbstractReplyProducingMessageHandler imp
}
else {
int n = 0;
while (this.replyDestination == null && n++ < 100) {
while (this.replyDestination == null && n++ < 100) { // NOSONAR
logger.debug("Waiting for container to create destination");
try {
Thread.sleep(100);
Thread.sleep(100); // NOSONAR
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
@@ -1488,16 +1490,15 @@ public class JmsOutboundGateway extends AbstractReplyProducingMessageHandler imp
synchronized (JmsOutboundGateway.this.lifeCycleMonitor) {
if (System.currentTimeMillis() - JmsOutboundGateway.this.lastSend >
JmsOutboundGateway.this.idleReplyContainerTimeout
&& JmsOutboundGateway.this.replies.size() == 0) {
&& JmsOutboundGateway.this.replies.size() == 0 &&
JmsOutboundGateway.this.replyContainer.isRunning()) {
if (JmsOutboundGateway.this.replyContainer.isRunning()) {
if (logger.isDebugEnabled()) {
logger.debug(getComponentName() + ": Stopping idle reply container.");
}
JmsOutboundGateway.this.replyContainer.stop();
JmsOutboundGateway.this.idleTask.cancel(false);
JmsOutboundGateway.this.idleTask = null;
if (logger.isDebugEnabled()) {
logger.debug(getComponentName() + ": Stopping idle reply container.");
}
JmsOutboundGateway.this.replyContainer.stop();
JmsOutboundGateway.this.idleTask.cancel(false);
JmsOutboundGateway.this.idleTask = null;
}
}
}