Add method StepExecutionDao.countStepExecutions to eliminate for-loop in SimpleJobRepository.getStepExecutionCount
Resolves #3657
This commit is contained in:
committed by
Mahmoud Ben Hassine
parent
56fbc270e2
commit
b50c665f04
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2006-2019 the original author or authors.
|
||||
* Copyright 2006-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.
|
||||
@@ -61,6 +61,7 @@ import org.springframework.util.Assert;
|
||||
* @author Robert Kasanicky
|
||||
* @author David Turanski
|
||||
* @author Mahmoud Ben Hassine
|
||||
* @author Baris Cubukcuoglu
|
||||
*
|
||||
* @see StepExecutionDao
|
||||
*/
|
||||
@@ -105,6 +106,14 @@ public class JdbcStepExecutionDao extends AbstractJdbcBatchMetadataDao implement
|
||||
private static final String CURRENT_VERSION_STEP_EXECUTION = "SELECT VERSION FROM %PREFIX%STEP_EXECUTION WHERE " +
|
||||
"STEP_EXECUTION_ID=?";
|
||||
|
||||
private static final String COUNT_STEP_EXECUTIONS = "SELECT COUNT(*) " +
|
||||
" from %PREFIX%JOB_EXECUTION JE, %PREFIX%STEP_EXECUTION SE" +
|
||||
" where " +
|
||||
" SE.JOB_EXECUTION_ID in (SELECT JOB_EXECUTION_ID from %PREFIX%JOB_EXECUTION " +
|
||||
"where JE.JOB_INSTANCE_ID = ?)" +
|
||||
" and SE.JOB_EXECUTION_ID = JE.JOB_EXECUTION_ID " +
|
||||
" and SE.STEP_NAME = ?";
|
||||
|
||||
private int exitMessageLength = DEFAULT_EXIT_MESSAGE_LENGTH;
|
||||
|
||||
private DataFieldMaxValueIncrementer stepExecutionIncrementer;
|
||||
@@ -347,6 +356,11 @@ public class JdbcStepExecutionDao extends AbstractJdbcBatchMetadataDao implement
|
||||
jobExecution.getId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int countStepExecutions(JobInstance jobInstance, String stepName) {
|
||||
return getJdbcTemplate().queryForObject(getQuery(COUNT_STEP_EXECUTIONS), new Object[] { jobInstance.getInstanceId(), stepName }, Integer.class);
|
||||
}
|
||||
|
||||
private static class StepExecutionRowMapper implements RowMapper<StepExecution> {
|
||||
|
||||
private final JobExecution jobExecution;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2006-2019 the original author or authors.
|
||||
* Copyright 2006-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.
|
||||
@@ -172,4 +172,17 @@ public class MapStepExecutionDao implements StepExecutionDao {
|
||||
saveStepExecution(stepExecution);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int countStepExecutions(JobInstance jobInstance, String stepName) {
|
||||
int count = 0;
|
||||
|
||||
for (StepExecution stepExecution : executionsByStepExecutionId.values()) {
|
||||
if (stepExecution.getStepName().equals(stepName) && stepExecution.getJobExecution().getJobInstance()
|
||||
.getInstanceId() == jobInstance.getInstanceId()) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2006-2019 the original author or authors.
|
||||
* Copyright 2006-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.
|
||||
@@ -86,4 +86,15 @@ public interface StepExecutionDao {
|
||||
*/
|
||||
void addStepExecutions(JobExecution jobExecution);
|
||||
|
||||
/**
|
||||
* Counts all the {@link StepExecution} for a given step name.
|
||||
*
|
||||
* @param jobInstance the parent {@link JobInstance}
|
||||
* @param stepName the name of the step
|
||||
* @since 4.3
|
||||
* @return the count of {@link StepExecution}s for a given step
|
||||
*/
|
||||
default int countStepExecutions(JobInstance jobInstance, String stepName) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2006-2019 the original author or authors.
|
||||
* Copyright 2006-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.
|
||||
@@ -51,6 +51,7 @@ import java.util.List;
|
||||
* @author Robert Kasanicky
|
||||
* @author David Turanski
|
||||
* @author Mahmoud Ben Hassine
|
||||
* @author Baris Cubukcuoglu
|
||||
*
|
||||
* @see JobRepository
|
||||
* @see JobInstanceDao
|
||||
@@ -235,17 +236,7 @@ public class SimpleJobRepository implements JobRepository {
|
||||
*/
|
||||
@Override
|
||||
public int getStepExecutionCount(JobInstance jobInstance, String stepName) {
|
||||
int count = 0;
|
||||
List<JobExecution> jobExecutions = jobExecutionDao.findJobExecutions(jobInstance);
|
||||
for (JobExecution jobExecution : jobExecutions) {
|
||||
stepExecutionDao.addStepExecutions(jobExecution);
|
||||
for (StepExecution stepExecution : jobExecution.getStepExecutions()) {
|
||||
if (stepName.equals(stepExecution.getStepName())) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
return stepExecutionDao.countStepExecutions(jobInstance, stepName);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2008-2013 the original author or authors.
|
||||
* Copyright 2008-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.
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package org.springframework.batch.core.repository.dao;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
@@ -75,4 +76,16 @@ public class JdbcStepExecutionDaoTests extends AbstractStepExecutionDaoTests {
|
||||
.length() < stepExecution.getExitStatus().getExitDescription().length());
|
||||
}
|
||||
|
||||
@Transactional
|
||||
@Test
|
||||
public void testCountStepExecutions() {
|
||||
// Given
|
||||
dao.saveStepExecution(stepExecution);
|
||||
|
||||
// When
|
||||
int result = dao.countStepExecutions(jobInstance, stepExecution.getStepName());
|
||||
|
||||
// Then
|
||||
assertEquals(1, result);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2008-2012 the original author or authors.
|
||||
* Copyright 2008-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.
|
||||
@@ -117,4 +117,26 @@ public class MapStepExecutionDaoTests extends AbstractStepExecutionDaoTests {
|
||||
assertEquals(BatchStatus.COMPLETED, jobStepExecution.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCountStepExecutions() {
|
||||
// Given
|
||||
StepExecutionDao tested = new MapStepExecutionDao();
|
||||
JobExecution jobExecution = new JobExecution(jobInstance, 88L, null, null);
|
||||
|
||||
StepExecution firstStepExecution = new StepExecution("Step one", jobExecution);
|
||||
firstStepExecution.setStatus(BatchStatus.STARTED);
|
||||
|
||||
tested.saveStepExecution(firstStepExecution);
|
||||
|
||||
StepExecution secondStepExecution = new StepExecution("Step two", jobExecution);
|
||||
secondStepExecution.setStatus(BatchStatus.STARTED);
|
||||
|
||||
tested.saveStepExecution(secondStepExecution);
|
||||
|
||||
// When
|
||||
int result = tested.countStepExecutions(jobInstance, firstStepExecution.getStepName());
|
||||
|
||||
// Then
|
||||
assertEquals(1, result);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2006-2018 the original author or authors.
|
||||
* Copyright 2006-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.
|
||||
@@ -16,10 +16,11 @@
|
||||
|
||||
package org.springframework.batch.core.repository.support;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
@@ -56,6 +57,7 @@ import org.springframework.batch.core.step.StepSupport;
|
||||
* @author Lucas Ward
|
||||
* @author Will Schipp
|
||||
* @author Dimitrios Liapis
|
||||
* @author Baris Cubukcuoglu
|
||||
*
|
||||
*/
|
||||
public class SimpleJobRepositoryTests {
|
||||
@@ -272,4 +274,16 @@ public class SimpleJobRepositoryTests {
|
||||
jobRepository.createJobExecution("foo", new JobParameters());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetStepExecutionCount() {
|
||||
// Given
|
||||
int expectedResult = 1;
|
||||
when(stepExecutionDao.countStepExecutions(jobInstance, "stepName")).thenReturn(expectedResult);
|
||||
|
||||
// When
|
||||
int actualResult = jobRepository.getStepExecutionCount(jobInstance, "stepName");
|
||||
|
||||
// Then
|
||||
assertEquals(expectedResult, actualResult);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user