Summer.Batch.Core.Launch.Support.SimpleJobLauncher.Run C# (CSharp) Method

Run() public method

Runs the provided job with the given JobParameters. The JobParameters will be used to determine if this is an execution of an existing job instance, or if a new one should be created.
  if the execution would be a re-start, but a re-start is either not allowed or not needed.  if the JobInstance already exists and has an execution already running   if this instance has already completed successfully  if given parameters do not pass validation process
public Run ( IJob job, JobParameters jobParameters ) : JobExecution
job IJob
jobParameters JobParameters
return JobExecution
        public JobExecution Run(IJob job, JobParameters jobParameters)
        {
            Assert.NotNull(job, "The job must be not null");
            Assert.NotNull(jobParameters, "The job parameters must be not null");
            JobExecution lastExecution = _jobRepository.GetLastJobExecution(job.Name, jobParameters);

            //manage last execution if needed
            HandleLastExecution(job, lastExecution);

            //validate Parameters            
            job.JobParametersValidator.Validate(jobParameters);

            //create new job execution
            var jobExecution = _jobRepository.CreateJobExecution(job.Name, jobParameters);

            //make an creation to be able to create a task, that will be executed by given TaskExecutor as expected
            var jobAction = CreateJobAction(job, jobParameters, jobExecution);

            using (var jobTask = new Task(jobAction))
            {
                try
                {
                    _taskExecutor.Execute(jobTask);
                    //in case of asynchronous executor ...
                    jobTask.Wait();
                }
                catch (InvalidOperationException exception)
                {
                    jobExecution.UpgradeStatus(BatchStatus.Failed);
                    if (jobExecution.ExitStatus.Equals(ExitStatus.Unknown))
                    {
                        jobExecution.ExitStatus = ExitStatus.Failed.AddExitDescription(exception);
                    }
                    _jobRepository.Update(jobExecution);
                }
            }
            return jobExecution;
        }

Usage Example

        public void RunTestSynchronousExecutor()
        {            
            SimpleJobLauncher launcher = new SimpleJobLauncher();
            IJobInstanceDao jobInstanceDao = new MapJobInstanceDao();
            IJobExecutionDao jobExecutionDao = new MapJobExecutionDao();
            IStepExecutionDao stepExecutionDao = new MapStepExecutionDao();
            IExecutionContextDao executionContextDao = new MapExecutionContextDao();

            IJobRepository jobRepository =
                new SimpleJobRepository(jobInstanceDao, jobExecutionDao, stepExecutionDao, executionContextDao);
            launcher.JobRepository = jobRepository;

            DefaultJobParametersConverter converter = new DefaultJobParametersConverter();
            NameValueCollection props2 = new NameValueCollection
            {
                {"+dateDebut(date)", "1970/07/31"},
                {"+everything(long)", "42"},
                {"-balance(double)", "1000.0"},
                {"+name(string)", "thierry"},
                {"-default", "default"}
            };
            JobParameters jobParameters = converter.GetJobParameters(props2);

            IJob job = new SimpleJob("myTestJob");
            job.JobParametersValidator = new DummyValidator();
            job.Restartable = false;
            TaskletStep step = new TaskletStep("simpleTS")
            {
                JobRepository = jobRepository,
                Tasklet = new MyDummyTasklet()
            };
            ((SimpleJob)job).AddStep(step);
            ((SimpleJob) job).JobRepository = jobRepository;
            JobExecution jobExecution = launcher.Run(job, jobParameters);
            Assert.IsFalse(jobExecution.Status.IsUnsuccessful());
            Assert.IsFalse(jobExecution.Status.IsRunning()); 
        }
All Usage Examples Of Summer.Batch.Core.Launch.Support.SimpleJobLauncher::Run