Cleanup code smells reported by Sonar

This is a first pass are removing some of the code smells.
Many reported code smells were ignored in this effort
This commit is contained in:
Glenn Renfro
2018-11-15 12:57:58 -05:00
committed by Mark Pollack
parent fa3e4e55c6
commit 8e1d38a76a
23 changed files with 50 additions and 46 deletions

View File

@@ -50,10 +50,8 @@ public class TaskBatchExecutionListenerBeanPostProcessor implements BeanPostProc
@Override
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
if(jobNames.size() > 0) {
if(!jobNames.contains(beanName)) {
if(jobNames.size() > 0 && !jobNames.contains(beanName)) {
return bean;
}
}
int length = this.applicationContext
.getBeanNamesForType(TaskBatchExecutionListener.class).length;

View File

@@ -30,6 +30,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "spring.cloud.task.batch")
public class TaskBatchProperties {
private static final long DEFAULT_POLL_INTERVAL = 5000L;
/**
* Comma-separated list of job names to execute on startup (for instance,
* `job1,job2`). By default, all Jobs found in the context are executed.
@@ -49,7 +50,7 @@ public class TaskBatchProperties {
* when spring.cloud.task.batch.failOnJobFailure is set to true. Defaults
* to 5000.
*/
private long failOnJobFailurePollInterval = 5000l;
private long failOnJobFailurePollInterval = DEFAULT_POLL_INTERVAL;
public String getJobNames() {
return this.jobNames;

View File

@@ -18,19 +18,14 @@ package org.springframework.cloud.task.batch.configuration;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.configuration.JobRegistry;
import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.repository.support.JobRepositoryFactoryBean;
import org.springframework.batch.core.repository.support.SimpleJobRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

View File

@@ -35,7 +35,7 @@ public class TaskBatchExecutionListener extends JobExecutionListenerSupport {
private TaskBatchDao taskBatchDao;
private final static Log logger = LogFactory.getLog(TaskBatchExecutionListener.class);
private static final Log logger = LogFactory.getLog(TaskBatchExecutionListener.class);
/**
* @param taskBatchDao dao used to persist the relationship. Must not be null

View File

@@ -68,6 +68,8 @@ import org.springframework.util.StringUtils;
*/
public class DeployerPartitionHandler implements PartitionHandler, EnvironmentAware, InitializingBean {
private static final long DEFAULT_POLL_INTERVAL = 10000;
public static final String SPRING_CLOUD_TASK_JOB_EXECUTION_ID =
"spring.cloud.task.job-execution-id";
@@ -100,7 +102,7 @@ public class DeployerPartitionHandler implements PartitionHandler, EnvironmentAw
private Log logger = LogFactory.getLog(DeployerPartitionHandler.class);
private long pollInterval = 10000;
private long pollInterval = DEFAULT_POLL_INTERVAL;
private long timeout = -1;
@@ -352,7 +354,8 @@ public class DeployerPartitionHandler implements PartitionHandler, EnvironmentAw
StepExecution partitionStepExecution =
jobExplorer.getStepExecution(masterStepExecution.getJobExecutionId(), curStepExecution.getId());
if (isComplete(partitionStepExecution.getStatus())) {
BatchStatus batchStatus = partitionStepExecution.getStatus();
if (batchStatus != null && isComplete(batchStatus)) {
result.add(partitionStepExecution);
currentWorkers--;

View File

@@ -49,7 +49,7 @@ import org.springframework.integration.support.locks.LockRegistry;
*/
public class SingleInstanceTaskListener implements ApplicationListener<ApplicationEvent> {
private final static Log logger = LogFactory.getLog(SingleInstanceTaskListener.class);
private static final Log logger = LogFactory.getLog(SingleInstanceTaskListener.class);
private LockRegistry lockRegistry;

View File

@@ -16,21 +16,15 @@
package org.springframework.cloud.task.configuration;
import java.util.Collection;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.task.repository.support.SimpleTaskNameResolver;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.integration.support.locks.PassThruLockRegistry;
import org.springframework.util.CollectionUtils;
/**
* Autoconfiguration of {@link SingleInstanceTaskListener}.

View File

@@ -31,6 +31,9 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "spring.cloud.task")
public class TaskProperties {
private static final int DEFAULT_CHECK_INTERVAL = 500;
private static final Log logger = LogFactory.getLog(TaskProperties.class);
public static final String DEFAULT_TABLE_PREFIX = "TASK_";
@@ -82,7 +85,7 @@ public class TaskProperties {
* Declares the time (in millis) that a task execution will wait between
* checks. Default time is: 500 millis.
*/
private int singleInstanceLockCheckInterval = 500;
private int singleInstanceLockCheckInterval = DEFAULT_CHECK_INTERVAL;
public String getExternalExecutionId() {
return externalExecutionId;

View File

@@ -29,14 +29,14 @@ public interface TaskExecutionListener {
* Invoked after the {@link TaskExecution} has been stored in the {@link TaskRepository}.
* @param taskExecution instance containing the information about the current task.
*/
public void onTaskStartup(TaskExecution taskExecution);
void onTaskStartup(TaskExecution taskExecution);
/**
* Invoked before the {@link TaskExecution} has been updated in the {@link TaskRepository}
* upon task end.
* @param taskExecution instance containing the information about the current task.
*/
public void onTaskEnd(TaskExecution taskExecution);
void onTaskEnd(TaskExecution taskExecution);
/**
* Invoked if an uncaught exception occurs during a task execution. This invocation
@@ -45,5 +45,5 @@ public interface TaskExecutionListener {
* @param taskExecution instance containing the information about the current task.
* @param throwable the uncaught exception that was thrown during task execution.
*/
public void onTaskFailed(TaskExecution taskExecution, Throwable throwable);
void onTaskFailed(TaskExecution taskExecution, Throwable throwable);
}

View File

@@ -85,7 +85,7 @@ public class TaskLifecycleListener implements ApplicationListener<ApplicationEve
private List<TaskExecutionListener> taskExecutionListeners;
private final static Log logger = LogFactory.getLog(TaskLifecycleListener.class);
private static final Log logger = LogFactory.getLog(TaskLifecycleListener.class);
private final TaskRepository taskRepository;

View File

@@ -49,7 +49,7 @@ import org.springframework.core.annotation.AnnotationUtils;
*/
public class TaskListenerExecutorObjectFactory implements ObjectFactory<TaskExecutionListener> {
private final static Log logger = LogFactory.getLog(TaskListenerExecutor.class);
private static final Log logger = LogFactory.getLog(TaskListenerExecutor.class);
private final Set<Class<?>> nonAnnotatedClasses =
Collections.newSetFromMap(new ConcurrentHashMap<>());

View File

@@ -46,7 +46,6 @@ import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
@@ -568,7 +567,7 @@ public class JdbcTaskExecutionDao implements TaskExecutionDao {
*/
private final class TaskExecutionRowMapper implements RowMapper<TaskExecution> {
public TaskExecutionRowMapper() {
private TaskExecutionRowMapper() {
}
@Override

View File

@@ -16,6 +16,7 @@
package org.springframework.cloud.task.repository.dao;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
@@ -322,7 +323,7 @@ public class MapTaskExecutionDao implements TaskExecutionDao {
}
}
private static class TaskExecutionComparator implements Comparator<TaskExecution> {
private static class TaskExecutionComparator implements Comparator<TaskExecution>, Serializable {
@Override
public int compare(TaskExecution firstTaskExecution, TaskExecution secondTaskExecution) {
if (firstTaskExecution.getStartTime().equals(secondTaskExecution.getStartTime())) {

View File

@@ -50,7 +50,7 @@ public enum DatabaseType {
private final String productName;
private DatabaseType(String productName) {
DatabaseType(String productName) {
this.productName = productName;
}

View File

@@ -39,7 +39,7 @@ public class SimpleTaskRepository implements TaskRepository {
public static final int MAX_TASK_NAME_SIZE = 100;
public static final int MAX_ERROR_MESSAGE_SIZE = 2500;
private final static Log logger = LogFactory.getLog(SimpleTaskRepository.class);
private static final Log logger = LogFactory.getLog(SimpleTaskRepository.class);
private TaskExecutionDao taskExecutionDao;

View File

@@ -41,6 +41,9 @@ import org.springframework.context.annotation.Configuration;
@Configuration
@EnableBatchProcessing
public class JobConfiguration {
private static final int DEFAULT_CHUNK_COUNT = 3;
@Autowired
private JobBuilderFactory jobBuilderFactory;
@@ -67,7 +70,7 @@ public class JobConfiguration {
@Bean
public Step step2() {
return stepBuilderFactory.get("step2").<String, String>chunk(3)
return stepBuilderFactory.get("step2").<String, String>chunk(DEFAULT_CHUNK_COUNT)
.reader(new ListItemReader<>(Arrays.asList("1", "2", "3", "4", "5", "6")))
.processor(new ItemProcessor<String, String>() {
@Override

View File

@@ -48,6 +48,8 @@ public class BatchEventsApplication {
@Configuration
public static class JobConfiguration {
private static final int DEFAULT_CHUNK_COUNT = 3;
@Autowired
private JobBuilderFactory jobBuilderFactory;
@@ -69,7 +71,7 @@ public class BatchEventsApplication {
@Bean
public Step step2() {
return this.stepBuilderFactory.get("step2")
.<String, String>chunk(3)
.<String, String>chunk(DEFAULT_CHUNK_COUNT)
.reader(new ListItemReader<>(Arrays.asList("1", "2", "3", "4", "5", "6")))
.processor(new ItemProcessor<String, String>() {
@Override

View File

@@ -20,6 +20,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import javax.sql.DataSource;
import org.springframework.batch.core.Job;
@@ -29,7 +30,6 @@ import org.springframework.batch.core.configuration.annotation.JobBuilderFactory
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.core.explore.support.JobExplorerFactoryBean;
import org.springframework.batch.core.partition.PartitionHandler;
import org.springframework.batch.core.partition.support.Partitioner;
import org.springframework.batch.core.repository.JobRepository;

View File

@@ -65,13 +65,13 @@ import org.springframework.messaging.MessageChannel;
@AutoConfigureAfter(SimpleTaskAutoConfiguration.class)
public class BatchEventAutoConfiguration {
public final static String JOB_EXECUTION_EVENTS_LISTENER = "jobExecutionEventsListener";
public final static String CHUNK_EVENTS_LISTENER = "chunkEventsListener";
public final static String STEP_EXECUTION_EVENTS_LISTENER = "stepExecutionEventsListener";
public final static String ITEM_READ_EVENTS_LISTENER = "itemReadEventsListener";
public final static String ITEM_WRITE_EVENTS_LISTENER = "itemWriteEventsListener";
public final static String ITEM_PROCESS_EVENTS_LISTENER = "itemProcessEventsListener";
public final static String SKIP_EVENTS_LISTENER = "skipEventsListener";
public static final String JOB_EXECUTION_EVENTS_LISTENER = "jobExecutionEventsListener";
public static final String CHUNK_EVENTS_LISTENER = "chunkEventsListener";
public static final String STEP_EXECUTION_EVENTS_LISTENER = "stepExecutionEventsListener";
public static final String ITEM_READ_EVENTS_LISTENER = "itemReadEventsListener";
public static final String ITEM_WRITE_EVENTS_LISTENER = "itemWriteEventsListener";
public static final String ITEM_PROCESS_EVENTS_LISTENER = "itemProcessEventsListener";
public static final String SKIP_EVENTS_LISTENER = "skipEventsListener";
@Bean
@ConditionalOnMissingBean

View File

@@ -87,7 +87,9 @@ public class JobParameterEvent {
@Override
public int hashCode() {
return 7 + 21 * (parameter == null ? parameterType.hashCode() : parameter.hashCode());
final int BASE_HASH = 7;
final int MULTIPLIER_HASH = 21;
return BASE_HASH + MULTIPLIER_HASH * (parameter == null ? parameterType.hashCode() : parameter.hashCode());
}
/**

View File

@@ -208,7 +208,9 @@ public class JobParametersEvent {
@Override
public int hashCode() {
return 17 + 23 * parameters.hashCode();
final int BASE_HASH = 17;
final int MULTIPLIER_HASH = 23;
return BASE_HASH + MULTIPLIER_HASH * parameters.hashCode();
}
@Override

View File

@@ -43,7 +43,7 @@ public class MessagePublisher<P>{
}
}
private final void publishMessage(Message<?> message) {
private void publishMessage(Message<?> message) {
this.listenerEventsChannel.send(message);
}

View File

@@ -163,10 +163,11 @@ public class TaskLaunchRequest implements Serializable{
@Override
public int hashCode() {
final int HASH_DEFAULT = 31;
int result = uri.hashCode();
result = 31 * result + commandlineArguments.hashCode();
result = 31 * result + environmentProperties.hashCode();
result = 31 * result + deploymentProperties.hashCode();
result = HASH_DEFAULT * result + commandlineArguments.hashCode();
result = HASH_DEFAULT * result + environmentProperties.hashCode();
result = HASH_DEFAULT * result + deploymentProperties.hashCode();
return result;
}
}