From b11c4b019b508293cf32b90c65fdccf79d0633aa Mon Sep 17 00:00:00 2001 From: Mahmoud Ben Hassine Date: Wed, 7 May 2025 23:16:15 +0200 Subject: [PATCH] Reorganize methods in JobOperator --- .../batch/core/launch/JobOperator.java | 184 ++++++++-------- .../launch/support/SimpleJobOperator.java | 206 +++++++++--------- 2 files changed, 195 insertions(+), 195 deletions(-) diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/launch/JobOperator.java b/spring-batch-core/src/main/java/org/springframework/batch/core/launch/JobOperator.java index e31e5b3c7..2d9c91c2c 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/launch/JobOperator.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/launch/JobOperator.java @@ -43,79 +43,11 @@ import org.springframework.lang.Nullable; public interface JobOperator extends JobLauncher { /** - * List the {@link JobExecution JobExecutions} associated with a particular - * {@link JobInstance}, in reverse order of creation (and therefore usually of - * execution). - * @param instanceId the id of a {@link JobInstance} - * @return the id values of all the {@link JobExecution JobExecutions} associated with - * this instance - * @throws NoSuchJobInstanceException if the {@link JobInstance} associated with the - * {@code instanceId} cannot be found. - * @deprecated Since 6.0 in favor of - * {@link org.springframework.batch.core.repository.JobRepository#getJobExecutions(JobInstance)}. - * Scheduled for removal in 6.2 or later. + * List the available job names that can be launched with + * {@link #start(String, Properties)}. + * @return a set of job names */ - @Deprecated(since = "6.0", forRemoval = true) - List getExecutions(long instanceId) throws NoSuchJobInstanceException; - - /** - * List the {@link JobInstance JobInstances} for a given job name, in reverse order of - * creation (and therefore usually of first execution). - * @param jobName the job name that all the instances have - * @param start the start index of the instances - * @param count the maximum number of values to return - * @return the id values of the {@link JobInstance JobInstances} - * @throws NoSuchJobException is thrown if no {@link JobInstance}s for the jobName - * exist. - * @deprecated Since 6.0 in favor of - * {@link org.springframework.batch.core.repository.JobRepository#getJobInstances(String, int, int)}. - * Scheduled for removal in 6.2 or later. - */ - @Deprecated(since = "6.0", forRemoval = true) - List getJobInstances(String jobName, int start, int count) throws NoSuchJobException; - - /** - * @param jobName {@link String} name of the job. - * @param jobParameters {@link JobParameters} parameters for the job instance. - * @return the {@link JobInstance} with the given name and parameters, or - * {@code null}. - * @deprecated Since 6.0 in favor of - * {@link org.springframework.batch.core.repository.JobRepository#getJobInstance(String, JobParameters)}. - * Scheduled for removal in 6.2 or later. - */ - @Deprecated(since = "6.0", forRemoval = true) - @Nullable - default JobInstance getJobInstance(String jobName, JobParameters jobParameters) { - throw new UnsupportedOperationException(); - } - - /** - * Get the id values of all the running {@link JobExecution JobExecutions} with the - * given job name. - * @param jobName the name of the job to search under - * @return the id values of the running {@link JobExecution} instances - * @throws NoSuchJobException if there are no {@link JobExecution JobExecutions} with - * that job name - * @deprecated Since 6.0 in favor of - * {@link org.springframework.batch.core.repository.JobRepository#findRunningJobExecutions(String)}. - * Scheduled for removal in 6.2 or later. - */ - @Deprecated(since = "6.0", forRemoval = true) - Set getRunningExecutions(String jobName) throws NoSuchJobException; - - /** - * Get the {@link JobParameters} as a human readable String (new line separated - * key=value pairs). - * @param executionId the id of an existing {@link JobExecution} - * @return the job parameters that were used to launch the associated instance - * @throws NoSuchJobExecutionException if the id was not associated with any - * {@link JobExecution} - * @deprecated Since 6.0 in favor of - * {@link org.springframework.batch.core.repository.JobRepository#getJobExecution(Long).getJobParameters()}. - * Scheduled for removal in 6.2 or later. - */ - @Deprecated(since = "6.0", forRemoval = true) - String getParameters(long executionId) throws NoSuchJobExecutionException; + Set getJobNames(); /** * Start a new instance of a job with the parameters specified. @@ -195,6 +127,94 @@ public interface JobOperator extends JobLauncher { */ boolean stop(long executionId) throws NoSuchJobExecutionException, JobExecutionNotRunningException; + /** + * Mark the {@link JobExecution} as ABANDONED. If a stop signal is ignored because the + * process died this is the best way to mark a job as finished with (as opposed to + * STOPPED). An abandoned job execution cannot be restarted by the framework. + * @param jobExecutionId the job execution id to abort + * @return the {@link JobExecution} that was aborted + * @throws NoSuchJobExecutionException thrown if there is no job execution for the + * jobExecutionId. + * @throws JobExecutionAlreadyRunningException if the job is running (it should be + * stopped first) + */ + JobExecution abandon(long jobExecutionId) throws NoSuchJobExecutionException, JobExecutionAlreadyRunningException; + + /** + * List the {@link JobExecution JobExecutions} associated with a particular + * {@link JobInstance}, in reverse order of creation (and therefore usually of + * execution). + * @param instanceId the id of a {@link JobInstance} + * @return the id values of all the {@link JobExecution JobExecutions} associated with + * this instance + * @throws NoSuchJobInstanceException if the {@link JobInstance} associated with the + * {@code instanceId} cannot be found. + * @deprecated Since 6.0 in favor of + * {@link org.springframework.batch.core.repository.JobRepository#getJobExecutions(JobInstance)}. + * Scheduled for removal in 6.2 or later. + */ + @Deprecated(since = "6.0", forRemoval = true) + List getExecutions(long instanceId) throws NoSuchJobInstanceException; + + /** + * List the {@link JobInstance JobInstances} for a given job name, in reverse order of + * creation (and therefore usually of first execution). + * @param jobName the job name that all the instances have + * @param start the start index of the instances + * @param count the maximum number of values to return + * @return the id values of the {@link JobInstance JobInstances} + * @throws NoSuchJobException is thrown if no {@link JobInstance}s for the jobName + * exist. + * @deprecated Since 6.0 in favor of + * {@link org.springframework.batch.core.repository.JobRepository#getJobInstances(String, int, int)}. + * Scheduled for removal in 6.2 or later. + */ + @Deprecated(since = "6.0", forRemoval = true) + List getJobInstances(String jobName, int start, int count) throws NoSuchJobException; + + /** + * @param jobName {@link String} name of the job. + * @param jobParameters {@link JobParameters} parameters for the job instance. + * @return the {@link JobInstance} with the given name and parameters, or + * {@code null}. + * @deprecated Since 6.0 in favor of + * {@link org.springframework.batch.core.repository.JobRepository#getJobInstance(String, JobParameters)}. + * Scheduled for removal in 6.2 or later. + */ + @Deprecated(since = "6.0", forRemoval = true) + @Nullable + default JobInstance getJobInstance(String jobName, JobParameters jobParameters) { + throw new UnsupportedOperationException(); + } + + /** + * Get the id values of all the running {@link JobExecution JobExecutions} with the + * given job name. + * @param jobName the name of the job to search under + * @return the id values of the running {@link JobExecution} instances + * @throws NoSuchJobException if there are no {@link JobExecution JobExecutions} with + * that job name + * @deprecated Since 6.0 in favor of + * {@link org.springframework.batch.core.repository.JobRepository#findRunningJobExecutions(String)}. + * Scheduled for removal in 6.2 or later. + */ + @Deprecated(since = "6.0", forRemoval = true) + Set getRunningExecutions(String jobName) throws NoSuchJobException; + + /** + * Get the {@link JobParameters} as a human readable String (new line separated + * key=value pairs). + * @param executionId the id of an existing {@link JobExecution} + * @return the job parameters that were used to launch the associated instance + * @throws NoSuchJobExecutionException if the id was not associated with any + * {@link JobExecution} + * @deprecated Since 6.0 in favor of + * {@link org.springframework.batch.core.repository.JobRepository#getJobExecution(Long).getJobParameters()}. + * Scheduled for removal in 6.2 or later. + */ + @Deprecated(since = "6.0", forRemoval = true) + String getParameters(long executionId) throws NoSuchJobExecutionException; + /** * Summarise the {@link JobExecution} with the supplied id, giving details of status, * start and end times etc. @@ -223,24 +243,4 @@ public interface JobOperator extends JobLauncher { @Deprecated(since = "6.0", forRemoval = true) Map getStepExecutionSummaries(long executionId) throws NoSuchJobExecutionException; - /** - * List the available job names that can be launched with - * {@link #start(String, Properties)}. - * @return a set of job names - */ - Set getJobNames(); - - /** - * Mark the {@link JobExecution} as ABANDONED. If a stop signal is ignored because the - * process died this is the best way to mark a job as finished with (as opposed to - * STOPPED). An abandoned job execution cannot be restarted by the framework. - * @param jobExecutionId the job execution id to abort - * @return the {@link JobExecution} that was aborted - * @throws NoSuchJobExecutionException thrown if there is no job execution for the - * jobExecutionId. - * @throws JobExecutionAlreadyRunningException if the job is running (it should be - * stopped first) - */ - JobExecution abandon(long jobExecutionId) throws NoSuchJobExecutionException, JobExecutionAlreadyRunningException; - } diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/SimpleJobOperator.java b/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/SimpleJobOperator.java index 80b285db2..c81eebc2b 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/SimpleJobOperator.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/launch/support/SimpleJobOperator.java @@ -121,114 +121,11 @@ public class SimpleJobOperator extends TaskExecutorJobLauncher implements JobOpe this.jobRegistry = jobRegistry; } - @Override - @Deprecated(since = "6.0", forRemoval = true) - public List getExecutions(long instanceId) throws NoSuchJobInstanceException { - JobInstance jobInstance = jobRepository.getJobInstance(instanceId); - if (jobInstance == null) { - throw new NoSuchJobInstanceException(String.format("No job instance with id=%d", instanceId)); - } - List list = new ArrayList<>(); - for (JobExecution jobExecution : jobRepository.getJobExecutions(jobInstance)) { - list.add(jobExecution.getId()); - } - return list; - } - @Override public Set getJobNames() { return new TreeSet<>(jobRegistry.getJobNames()); } - @Override - @Deprecated(since = "6.0", forRemoval = true) - public List getJobInstances(String jobName, int start, int count) throws NoSuchJobException { - List list = new ArrayList<>(); - List jobInstances = jobRepository.getJobInstances(jobName, start, count); - for (JobInstance jobInstance : jobInstances) { - list.add(jobInstance.getId()); - } - if (list.isEmpty() && !jobRegistry.getJobNames().contains(jobName)) { - throw new NoSuchJobException("No such job (either in registry or in historical data): " + jobName); - } - return list; - } - - @Override - @Nullable - @Deprecated(since = "6.0", forRemoval = true) - public JobInstance getJobInstance(String jobName, JobParameters jobParameters) { - return this.jobRepository.getJobInstance(jobName, jobParameters); - } - - @Override - @Deprecated(since = "6.0", forRemoval = true) - public String getParameters(long executionId) throws NoSuchJobExecutionException { - JobExecution jobExecution = findExecutionById(executionId); - - Properties properties = this.jobParametersConverter.getProperties(jobExecution.getJobParameters()); - - return PropertiesConverter.propertiesToString(properties); - } - - @Override - @Deprecated(since = "6.0", forRemoval = true) - public Set getRunningExecutions(String jobName) throws NoSuchJobException { - Set set = new LinkedHashSet<>(); - for (JobExecution jobExecution : jobRepository.findRunningJobExecutions(jobName)) { - set.add(jobExecution.getId()); - } - if (set.isEmpty() && !jobRegistry.getJobNames().contains(jobName)) { - throw new NoSuchJobException("No such job (either in registry or in historical data): " + jobName); - } - return set; - } - - @Override - @Deprecated(since = "6.0", forRemoval = true) - public Map getStepExecutionSummaries(long executionId) throws NoSuchJobExecutionException { - JobExecution jobExecution = findExecutionById(executionId); - - Map map = new LinkedHashMap<>(); - for (StepExecution stepExecution : jobExecution.getStepExecutions()) { - map.put(stepExecution.getId(), stepExecution.toString()); - } - return map; - } - - @Override - @Deprecated(since = "6.0", forRemoval = true) - public String getSummary(long executionId) throws NoSuchJobExecutionException { - JobExecution jobExecution = findExecutionById(executionId); - return jobExecution.toString(); - } - - @Override - public Long restart(long executionId) throws JobInstanceAlreadyCompleteException, NoSuchJobExecutionException, - NoSuchJobException, JobRestartException, JobParametersInvalidException { - - if (logger.isInfoEnabled()) { - logger.info("Checking status of job execution with id=" + executionId); - } - JobExecution jobExecution = findExecutionById(executionId); - - String jobName = jobExecution.getJobInstance().getJobName(); - Job job = jobRegistry.getJob(jobName); - JobParameters parameters = jobExecution.getJobParameters(); - - if (logger.isInfoEnabled()) { - logger.info(String.format("Attempting to resume job with name=%s and parameters=%s", jobName, parameters)); - } - try { - return run(job, parameters).getId(); - } - catch (JobExecutionAlreadyRunningException e) { - throw new UnexpectedJobExecutionException( - String.format(ILLEGAL_STATE_MSG, "job execution already running", jobName, parameters), e); - } - - } - @Override public Long start(String jobName, Properties parameters) throws NoSuchJobException, JobInstanceAlreadyExistsException, JobParametersInvalidException { @@ -267,6 +164,32 @@ public class SimpleJobOperator extends TaskExecutorJobLauncher implements JobOpe } + @Override + public Long restart(long executionId) throws JobInstanceAlreadyCompleteException, NoSuchJobExecutionException, + NoSuchJobException, JobRestartException, JobParametersInvalidException { + + if (logger.isInfoEnabled()) { + logger.info("Checking status of job execution with id=" + executionId); + } + JobExecution jobExecution = findExecutionById(executionId); + + String jobName = jobExecution.getJobInstance().getJobName(); + Job job = jobRegistry.getJob(jobName); + JobParameters parameters = jobExecution.getJobParameters(); + + if (logger.isInfoEnabled()) { + logger.info(String.format("Attempting to resume job with name=%s and parameters=%s", jobName, parameters)); + } + try { + return run(job, parameters).getId(); + } + catch (JobExecutionAlreadyRunningException e) { + throw new UnexpectedJobExecutionException( + String.format(ILLEGAL_STATE_MSG, "job execution already running", jobName, parameters), e); + } + + } + @Override public Long startNextInstance(String jobName) throws NoSuchJobException, UnexpectedJobExecutionException, JobParametersInvalidException { @@ -364,6 +287,83 @@ public class SimpleJobOperator extends TaskExecutorJobLauncher implements JobOpe return jobExecution; } + @Override + @Deprecated(since = "6.0", forRemoval = true) + public List getExecutions(long instanceId) throws NoSuchJobInstanceException { + JobInstance jobInstance = jobRepository.getJobInstance(instanceId); + if (jobInstance == null) { + throw new NoSuchJobInstanceException(String.format("No job instance with id=%d", instanceId)); + } + List list = new ArrayList<>(); + for (JobExecution jobExecution : jobRepository.getJobExecutions(jobInstance)) { + list.add(jobExecution.getId()); + } + return list; + } + + @Override + @Deprecated(since = "6.0", forRemoval = true) + public List getJobInstances(String jobName, int start, int count) throws NoSuchJobException { + List list = new ArrayList<>(); + List jobInstances = jobRepository.getJobInstances(jobName, start, count); + for (JobInstance jobInstance : jobInstances) { + list.add(jobInstance.getId()); + } + if (list.isEmpty() && !jobRegistry.getJobNames().contains(jobName)) { + throw new NoSuchJobException("No such job (either in registry or in historical data): " + jobName); + } + return list; + } + + @Override + @Nullable + @Deprecated(since = "6.0", forRemoval = true) + public JobInstance getJobInstance(String jobName, JobParameters jobParameters) { + return this.jobRepository.getJobInstance(jobName, jobParameters); + } + + @Override + @Deprecated(since = "6.0", forRemoval = true) + public String getParameters(long executionId) throws NoSuchJobExecutionException { + JobExecution jobExecution = findExecutionById(executionId); + + Properties properties = this.jobParametersConverter.getProperties(jobExecution.getJobParameters()); + + return PropertiesConverter.propertiesToString(properties); + } + + @Override + @Deprecated(since = "6.0", forRemoval = true) + public Set getRunningExecutions(String jobName) throws NoSuchJobException { + Set set = new LinkedHashSet<>(); + for (JobExecution jobExecution : jobRepository.findRunningJobExecutions(jobName)) { + set.add(jobExecution.getId()); + } + if (set.isEmpty() && !jobRegistry.getJobNames().contains(jobName)) { + throw new NoSuchJobException("No such job (either in registry or in historical data): " + jobName); + } + return set; + } + + @Override + @Deprecated(since = "6.0", forRemoval = true) + public Map getStepExecutionSummaries(long executionId) throws NoSuchJobExecutionException { + JobExecution jobExecution = findExecutionById(executionId); + + Map map = new LinkedHashMap<>(); + for (StepExecution stepExecution : jobExecution.getStepExecutions()) { + map.put(stepExecution.getId(), stepExecution.toString()); + } + return map; + } + + @Override + @Deprecated(since = "6.0", forRemoval = true) + public String getSummary(long executionId) throws NoSuchJobExecutionException { + JobExecution jobExecution = findExecutionById(executionId); + return jobExecution.toString(); + } + private JobExecution findExecutionById(long executionId) throws NoSuchJobExecutionException { JobExecution jobExecution = jobRepository.getJobExecution(executionId);