Fix some Sonar smells

This commit is contained in:
Artem Bilan
2020-02-14 16:26:37 -05:00
parent 236ded9ea1
commit 6d936d9774
3 changed files with 51 additions and 48 deletions

View File

@@ -17,12 +17,13 @@
package org.springframework.integration.amqp.dsl;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.messaging.Message;
/**
* Spec for an outbound AMQP channel adapter
*
* @author Gary Russell
* @author Artme Bilan
*
* @since 5.3
*
*/
@@ -33,8 +34,8 @@ public class AmqpOutboundChannelAdapterSpec extends AmqpOutboundEndpointSpec<Amq
}
/**
* If true, and the message payload is an {@link Iterable} of {@link Message}, send the
* messages in a single invocation of the template (same channel) and optionally
* If true, and the message payload is an {@link Iterable} of {@link org.springframework.messaging.Message},
* send the messages in a single invocation of the template (same channel) and optionally
* wait for the confirms or die.
* @param multiSend true to send multiple messages.
* @return the spec.

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.
@@ -67,6 +67,11 @@ import reactor.core.scheduler.Schedulers;
*/
public abstract class AbstractPollingEndpoint extends AbstractEndpoint implements BeanClassLoaderAware {
/**
* A default polling period for {@link PeriodicTrigger}.
*/
public static final long DEFAULT_POLLING_PERIOD = 10;
private final Object initializationMonitor = new Object();
private Executor taskExecutor = new SyncTaskExecutor();
@@ -75,7 +80,7 @@ public abstract class AbstractPollingEndpoint extends AbstractEndpoint implement
private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader();
private Trigger trigger = new PeriodicTrigger(10);
private Trigger trigger = new PeriodicTrigger(DEFAULT_POLLING_PERIOD);
private long maxMessagesPerPoll = -1;
@@ -117,7 +122,7 @@ public abstract class AbstractPollingEndpoint extends AbstractEndpoint implement
}
public void setTrigger(Trigger trigger) {
this.trigger = (trigger != null ? trigger : new PeriodicTrigger(10));
this.trigger = (trigger != null ? trigger : new PeriodicTrigger(DEFAULT_POLLING_PERIOD));
}
public void setAdviceChain(List<Advice> adviceChain) {
@@ -193,19 +198,16 @@ public abstract class AbstractPollingEndpoint extends AbstractEndpoint implement
return;
}
Assert.notNull(this.trigger, "Trigger is required");
if (this.taskExecutor != null) {
if (!(this.taskExecutor instanceof ErrorHandlingTaskExecutor)) {
if (this.errorHandler == null) {
this.errorHandler = ChannelUtils.getErrorHandler(getBeanFactory());
this.errorHandlerIsDefault = true;
}
this.taskExecutor = new ErrorHandlingTaskExecutor(this.taskExecutor, this.errorHandler);
if (this.taskExecutor != null && !(this.taskExecutor instanceof ErrorHandlingTaskExecutor)) {
if (this.errorHandler == null) {
this.errorHandler = ChannelUtils.getErrorHandler(getBeanFactory());
this.errorHandlerIsDefault = true;
}
this.taskExecutor = new ErrorHandlingTaskExecutor(this.taskExecutor, this.errorHandler);
}
if (this.transactionSynchronizationFactory == null && this.adviceChain != null) {
if (this.adviceChain.stream().anyMatch(TransactionInterceptor.class::isInstance)) {
this.transactionSynchronizationFactory = new PassThroughTransactionSynchronizationFactory();
}
if (this.transactionSynchronizationFactory == null && this.adviceChain != null &&
this.adviceChain.stream().anyMatch(TransactionInterceptor.class::isInstance)) {
this.transactionSynchronizationFactory = new PassThroughTransactionSynchronizationFactory();
}
this.initialized = true;
}
@@ -328,7 +330,7 @@ public abstract class AbstractPollingEndpoint extends AbstractEndpoint implement
return this.pollingTask.call();
}
catch (Exception e) {
if (e instanceof MessagingException) {
if (e instanceof MessagingException) { // NOSONAR
throw (MessagingException) e;
}
else {
@@ -395,13 +397,11 @@ public abstract class AbstractPollingEndpoint extends AbstractEndpoint implement
try {
handleMessage(message);
}
catch (Exception e) {
if (e instanceof MessagingException) {
throw new MessagingExceptionWrapper(message, (MessagingException) e);
}
else {
throw new MessagingException(message, e);
}
catch (MessagingException ex) {
throw new MessagingExceptionWrapper(message, ex);
}
catch (Exception ex) {
throw new MessagingException(message, ex);
}
}
}

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.
@@ -24,6 +24,7 @@ import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Properties;
import java.util.stream.Collectors;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -35,22 +36,23 @@ import org.springframework.integration.support.MessageBuilderFactory;
import org.springframework.integration.support.MutableMessage;
import org.springframework.integration.support.MutableMessageBuilderFactory;
import org.springframework.integration.support.context.NamedComponent;
import org.springframework.lang.Nullable;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.ErrorMessage;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* @author Mark Fisher
* @author Artem Bilan
* @author Gary Russell
*
* @since 2.0
*/
@SuppressWarnings("serial")
public final class MessageHistory implements List<Properties>, Serializable {
private static final Log logger = LogFactory.getLog(MessageHistory.class);
private static final Log LOGGER = LogFactory.getLog(MessageHistory.class);
private static final UnsupportedOperationException UNSUPPORTED_OPERATION_EXCEPTION_IMMUTABLE =
new UnsupportedOperationException("MessageHistory is immutable.");
@@ -68,9 +70,11 @@ public final class MessageHistory implements List<Properties>, Serializable {
private final List<Properties> components;
public static MessageHistory read(Message<?> message) {
return message != null ? message.getHeaders().get(HEADER_NAME, MessageHistory.class) : null;
@Nullable
public static MessageHistory read(@Nullable Message<?> message) {
return message != null
? message.getHeaders().get(HEADER_NAME, MessageHistory.class)
: null;
}
public static <T> Message<T> write(Message<T> message, NamedComponent component) {
@@ -87,8 +91,10 @@ public final class MessageHistory implements List<Properties>, Serializable {
Properties metadata = extractMetadata(component);
if (!metadata.isEmpty()) {
MessageHistory previousHistory = message.getHeaders().get(HEADER_NAME, MessageHistory.class);
List<Properties> components = (previousHistory != null) ?
new ArrayList<Properties>(previousHistory) : new ArrayList<Properties>();
List<Properties> components =
previousHistory != null
? new ArrayList<>(previousHistory)
: new ArrayList<>();
components.add(metadata);
MessageHistory history = new MessageHistory(components);
@@ -111,13 +117,13 @@ public final class MessageHistory implements List<Properties>, Serializable {
else {
if (!(message instanceof GenericMessage) &&
(messageBuilderFactory instanceof DefaultMessageBuilderFactory ||
messageBuilderFactory instanceof MutableMessageBuilderFactory)) {
if (logger.isWarnEnabled()) {
logger.warn("MessageHistory rebuilds the message and produces the result of the [" +
messageBuilderFactory + "], not an instance of the provided type [" +
message.getClass() + "]. Consider to supply a custom MessageBuilderFactory " +
"to retain custom messages during MessageHistory tracking.");
}
messageBuilderFactory instanceof MutableMessageBuilderFactory)
&& LOGGER.isWarnEnabled()) {
LOGGER.warn("MessageHistory rebuilds the message and produces the result of the [" +
messageBuilderFactory + "], not an instance of the provided type [" +
message.getClass() + "]. Consider to supply a custom MessageBuilderFactory " +
"to retain custom messages during MessageHistory tracking.");
}
message = messageBuilderFactory.fromMessage(message)
.setHeader(HEADER_NAME, history)
@@ -201,14 +207,10 @@ public final class MessageHistory implements List<Properties>, Serializable {
@Override
public String toString() {
List<String> names = new ArrayList<String>();
for (Properties p : this.components) {
String name = p.getProperty(NAME_PROPERTY);
if (name != null) {
names.add(name);
}
}
return StringUtils.collectionToCommaDelimitedString(names);
return this.components
.stream()
.map((props) -> props.getProperty(NAME_PROPERTY))
.collect(Collectors.joining(","));
}