org.springframework.batch.core.Job Java Examples

The following examples show how to use org.springframework.batch.core.Job. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AdHocSchedulerTest.java    From spring-batch-rest with Apache License 2.0 6 votes vote down vote up
@Test
public void scheduleWithJobConfigAndDateWorks() throws InterruptedException {
	Job job1 = job("j1", latch1);
	Job job2 = job("j2", latch2);

	jobBuilder.registerJob(job1);
	jobBuilder.registerJob(job2);

	JobConfig job1Config = JobConfig.builder().name("j1").build();
	JobConfig job2Config = JobConfig.builder().name("j2").build();

	Date oneSecondFromNow = Date.from(Instant.now().plusMillis(1000));

	scheduler.schedule(job1Config, oneSecondFromNow);
	scheduler.schedule(job2Config, oneSecondFromNow);
	scheduler.start();

	latch1.await(4, SECONDS);
	latch2.await(4, SECONDS);
	scheduler.pause();
}
 
Example #2
Source File: SettleQuartzController.java    From seed with Apache License 2.0 6 votes vote down vote up
/**
 * @param jobNameStr   任务名字符串
 * @param jobNameDesc  任务名描述
 * @param time         随机数:批量的唯一标志(非断点续跑可直接传null)
 * @param parameterMap 其它参数:不会作为批量的唯一标志(无参可传null)
 * Comment by 玄玉<https://jadyer.cn/> on 2019/8/12 18:25.
 */
private JobExecution runJob(String jobNameStr, String jobNameDesc, String time, Map<String, String> parameterMap) throws Exception {
    //判断是否断点续跑
    boolean isResume = false;
    long timeLong;
    if(StringUtils.isBlank(time)){
        timeLong = SystemClockUtil.INSTANCE.now();
    }else{
        isResume = true;
        timeLong = Long.parseLong(time);
    }
    LogUtil.getLogger().info("{}==>{}:Starting...time={}", jobNameDesc, isResume?":断点续跑":"", timeLong);
    //构造JobParameters
    JobParametersBuilder jobParametersBuilder = new JobParametersBuilder();
    jobParametersBuilder.addLong("time", timeLong);
    if(null!=parameterMap && !parameterMap.isEmpty()){
        for(Map.Entry<String,String> entry : parameterMap.entrySet()){
            jobParametersBuilder.addString(entry.getKey(), entry.getValue(), false);
        }
    }
    //执行job
    Job xmlSettleJob = (Job)SpringContextHolder.getBean(jobNameStr);
    JobExecution execution = jobLauncher.run(xmlSettleJob, jobParametersBuilder.toJobParameters());
    LogUtil.getLogger().info("{}==>{}:Ending......jobInstance={}", jobNameDesc, isResume?":断点续跑":"", execution.getJobInstance());
    return execution;
}
 
Example #3
Source File: LocalConfiguration.java    From CogStack-Pipeline with Apache License 2.0 6 votes vote down vote up
@Bean
public Job job(JobBuilderFactory jobs,
               StepBuilderFactory steps,
               Partitioner partitioner,
               JobCompleteNotificationListener jobCompleteNotificationListener,
               @Qualifier("partitionHandler") PartitionHandler partitionHandler,
               @Qualifier("tLJobParametersIncrementer") TLJobParametersIncrementer runIdIncrementer

) {
    return jobs.get(env.getProperty("job.jobName"))
            .incrementer(runIdIncrementer)
            .listener(jobCompleteNotificationListener)
            .flow(
                    steps
                            .get(jobName + "MasterStep")
                            .partitioner((jobName+"SlaveStep"), partitioner)
                            .partitionHandler(partitionHandler)
                            .build()
            )
            .end()
            .build();

}
 
Example #4
Source File: JobOperationsController.java    From spring-boot-starter-batch-web with Apache License 2.0 6 votes vote down vote up
/**
 * Borrowed from CommandLineJobRunner.
 *
 * @param job
 *            the job that we need to find the next parameters for
 * @return the next job parameters if they can be located
 * @throws JobParametersNotFoundException
 *             if there is a problem
 */
private JobParameters getNextJobParameters(Job job) throws JobParametersNotFoundException {
	String jobIdentifier = job.getName();
	JobParameters jobParameters;
	List<JobInstance> lastInstances = jobExplorer.getJobInstances(jobIdentifier, 0, 1);

	JobParametersIncrementer incrementer = job.getJobParametersIncrementer();

	if (lastInstances.isEmpty()) {
		jobParameters = incrementer.getNext(new JobParameters());
		if (jobParameters == null) {
			throw new JobParametersNotFoundException(
					"No bootstrap parameters found from incrementer for job=" + jobIdentifier);
		}
	} else {
		List<JobExecution> lastExecutions = jobExplorer.getJobExecutions(lastInstances.get(0));
		jobParameters = incrementer.getNext(lastExecutions.get(0).getJobParameters());
	}
	return jobParameters;
}
 
Example #5
Source File: ComposedRunnerJobFactory.java    From composed-task-runner with Apache License 2.0 6 votes vote down vote up
@Override
public Job getObject() throws Exception {
	ComposedRunnerVisitor composedRunnerVisitor = new ComposedRunnerVisitor();

	TaskParser taskParser = new TaskParser("composed-task-runner",
			this.dsl,false,true);
	taskParser.parse().accept(composedRunnerVisitor);

	this.visitorDeque = composedRunnerVisitor.getFlow();

	FlowJobBuilder builder = this.jobBuilderFactory
			.get(this.taskNameResolver.getTaskName())
			.start(this.flowBuilder
					.start(createFlow())
					.end())
			.end();
	if(this.incrementInstanceEnabled) {
		builder.incrementer(new RunIdIncrementer());
	}
	return builder.build();
}
 
Example #6
Source File: JobConfiguration.java    From tutorials with MIT License 6 votes vote down vote up
@Bean
public Job job2() {
    return jobBuilderFactory.get("job2")
        .start(stepBuilderFactory.get("job2step1")
            .tasklet(new Tasklet() {
                @Override
                public RepeatStatus execute(
                    StepContribution contribution,
                    ChunkContext chunkContext)
                    throws Exception {
                    LOGGER
                        .info("This job is from Baeldung");
                    return RepeatStatus.FINISHED;
                }
            })
            .build())
        .build();
}
 
Example #7
Source File: AdHocScheduler.java    From spring-batch-rest with Apache License 2.0 6 votes vote down vote up
/**
 * Schedules a Spring Batch job via a Quartz cron expression. Also registers the
 * job with the specified jobName, rather than the job param's name
 */
public synchronized Job schedule(String jobName, Job job, String cronExpression) {
	log.debug("Scheduling job {} with CRON expression {}", jobName, cronExpression);
	try {
		jobBuilder.registerJob(job);
		JobDetail jobDetail = this.jobDetailFor(jobName);

		Trigger trigger = TriggerUtil.triggerFor(cronExpression, jobName);

		scheduler.unscheduleJob(trigger.getKey());
		scheduler.scheduleJob(jobDetail, trigger);
		log.info("Scheduled job {} with CRON expression {}", jobName, cronExpression);
	} catch (Exception e) {
		throw new RuntimeException(format("Can't schedule job %s with cronExpression %s", jobName, cronExpression),
				e);
	}
	return job;
}
 
Example #8
Source File: JobBuilder.java    From spring-batch-rest with Apache License 2.0 6 votes vote down vote up
public static Job registerJob(JobRegistry jobRegistry, Job job) {
    jobRegistry.unregister(job.getName());
    try {
        jobRegistry.register(new JobFactory() {
            @Override
            public Job createJob() {
                return job;
            }

            @Override
            public String getJobName() {
                return job.getName();
            }
        });
    } catch (Exception e) {
        throw new RuntimeException("Could not create " + job.getName(), e);
    }
    return job;
}
 
Example #9
Source File: ComposedRunnerJobFactory.java    From spring-cloud-dataflow with Apache License 2.0 6 votes vote down vote up
@Override
public Job getObject() throws Exception {
	ComposedRunnerVisitor composedRunnerVisitor = new ComposedRunnerVisitor();

	TaskParser taskParser = new TaskParser("composed-task-runner",
			this.dsl,false,true);
	taskParser.parse().accept(composedRunnerVisitor);

	this.visitorDeque = composedRunnerVisitor.getFlow();

	FlowJobBuilder builder = this.jobBuilderFactory
			.get(this.taskNameResolver.getTaskName())
			.start(this.flowBuilder
					.start(createFlow())
					.end())
			.end();
	if(this.incrementInstanceEnabled) {
		builder.incrementer(new RunIdIncrementer());
	}
	return builder.build();
}
 
Example #10
Source File: SpringbatchPartitionerApp.java    From tutorials with MIT License 6 votes vote down vote up
public static void main(final String[] args) {
    // Spring Java config
    final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.register(SpringbatchPartitionConfig.class);
    context.refresh();

    final JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");
    final Job job = (Job) context.getBean("partitionerJob");
    LOGGER.info("Starting the batch job");
    try {
        final JobExecution execution = jobLauncher.run(job, new JobParameters());
        LOGGER.info("Job Status : {}", execution.getStatus());
    } catch (final Exception e) {
        e.printStackTrace();
        LOGGER.error("Job failed {}", e.getMessage());
    }
}
 
Example #11
Source File: TaskJobLauncherCommandLineRunnerFactoryBean.java    From spring-cloud-task with Apache License 2.0 6 votes vote down vote up
public TaskJobLauncherCommandLineRunnerFactoryBean(JobLauncher jobLauncher,
		JobExplorer jobExplorer, List<Job> jobs,
		TaskBatchProperties taskBatchProperties, JobRegistry jobRegistry,
		JobRepository jobRepository, BatchProperties batchProperties) {
	Assert.notNull(taskBatchProperties, "taskBatchProperties must not be null");
	Assert.notNull(batchProperties, "batchProperties must not be null");
	this.jobLauncher = jobLauncher;
	this.jobExplorer = jobExplorer;
	Assert.notEmpty(jobs, "jobs must not be null nor empty");
	this.jobs = jobs;
	this.jobNames = taskBatchProperties.getJobNames();
	this.jobRegistry = jobRegistry;
	this.taskBatchProperties = taskBatchProperties;
	if (StringUtils.hasText(batchProperties.getJob().getNames())) {
		this.jobNames = batchProperties.getJob().getNames();
	}
	else {
		this.jobNames = taskBatchProperties.getJobNames();
	}
	this.order = taskBatchProperties.getCommandLineRunnerOrder();
	this.jobRepository = jobRepository;
}
 
Example #12
Source File: NestedJobDemo.java    From SpringAll with MIT License 5 votes vote down vote up
@Bean
public Job parentJob() {
    return jobBuilderFactory.get("parentJob")
            .start(childJobOneStep())
            .next(childJobTwoStep())
            .build();
}
 
Example #13
Source File: BatchConfiguration.java    From spring-boot-samples with Apache License 2.0 5 votes vote down vote up
@Bean
public Job importUserJob(JobCompletionNotificationListener listener) {
    return jobBuilderFactory.get("importUserJob")
            .incrementer(new RunIdIncrementer())
            .listener(listener)
            .flow(step1())
            .end()
            .build();
}
 
Example #14
Source File: EmployeeJobConfigSingleJvm.java    From batchers with Apache License 2.0 5 votes vote down vote up
@Bean
public Job employeeJob() {
    return jobBuilders.get(EMPLOYEE_JOB)
            .start(taxCalculationStep())
            .next(wsCallAndGenerateAndSendPaycheckStep())
            .next(jobResultsPdf())
            .listener(jobStatusListener)
            .listener(changeStatusOnFailedStepsJobExecListener)
            .build();
}
 
Example #15
Source File: TaxCalculatorJobServiceTest.java    From batchers with Apache License 2.0 5 votes vote down vote up
@Test
public void whenStarJobs_withGivenYearAndMonth_runJobWithParameters() throws JobParametersInvalidException, JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException {
    taxCalculatorJobService.runTaxCalculatorJob(new JobStartParams(YEAR, MONTH));

    verify(jobLauncherMock).run(any(Job.class), jobParametersArgumentCaptor.capture());

    JobParameters jobParameters = jobParametersArgumentCaptor.getValue();
    assertThat(jobParameters.getLong("year")).isEqualTo(YEAR);
    assertThat(jobParameters.getLong("month")).isEqualTo(MONTH);
}
 
Example #16
Source File: TaxCalculatorJobServiceTest.java    From batchers with Apache License 2.0 5 votes vote down vote up
@Before
public void setUpJobLauncher() throws Exception {
    setInternalState(taxCalculatorJobService, "jobStartListeners",
            asList(jobStartListenerMock1, jobStartListenerMock2).stream().collect(toSet())
    );

    when(jobLauncherMock.run(any(Job.class), any(JobParameters.class))).thenReturn(jobExecution);
}
 
Example #17
Source File: CompositeJobExecutionListenerJobDemo.java    From SpringAll with MIT License 5 votes vote down vote up
@Bean
public Job compositeJobExecutionListenerJob() {
    return jobBuilderFactory.get("compositeJobExecutionListenerJob")
            .start(step())
            .listener(compositeJobExecutionListener())
            .build();
}
 
Example #18
Source File: SplitJobDemo.java    From SpringAll with MIT License 5 votes vote down vote up
@Bean
public Job splitJob() {
    return jobBuilderFactory.get("splitJob")
            .start(flow1())
            .split(new SimpleAsyncTaskExecutor()).add(flow2())
            .end()
            .build();

}
 
Example #19
Source File: JobConfiguration.java    From spring-cloud-task with Apache License 2.0 5 votes vote down vote up
@Bean
public Job job2() {
	return this.jobBuilderFactory.get("job2")
		.start(this.stepBuilderFactory.get("job2step1")
			.tasklet(new Tasklet() {
				@Override
				public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
					logger.info("Job2 was run");
					return RepeatStatus.FINISHED;
				}
			})
			.build())
		.build();
}
 
Example #20
Source File: JobRunner.java    From spring-batch-performance-tuning with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
	ApplicationContext applicationContext = new ClassPathXmlApplicationContext(CONFIG);

	Job job = applicationContext.getBean(Job.class);
	JobLauncher jobLauncher = applicationContext.getBean(JobLauncher.class);

	jobLauncher.run(job, new JobParameters());
}
 
Example #21
Source File: CapitalizeNamesJobConfig.java    From spring-batch with MIT License 5 votes vote down vote up
@Bean
public Job capitalizeNamesJob(JobBuilderFactory jobBuilders,
    StepBuilderFactory stepBuilders) {
  return jobBuilders.get("capitalizeNamesJob")
      .start(capitalizeNamesStep(stepBuilders))
      .next(deleteFilesStep(stepBuilders)).build();
}
 
Example #22
Source File: FlatFileItemWriterAutoConfigurationTests.java    From spring-cloud-task with Apache License 2.0 5 votes vote down vote up
@Test
public void testHeaderFooterFileGeneration() {
	ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
			.withUserConfiguration(HeaderFooterConfiguration.class)
			.withConfiguration(
					AutoConfigurations.of(PropertyPlaceholderAutoConfiguration.class,
							BatchAutoConfiguration.class,
							SingleStepJobAutoConfiguration.class,
							FlatFileItemWriterAutoConfiguration.class))
			.withPropertyValues("spring.batch.job.jobName=job",
					"spring.batch.job.stepName=step1", "spring.batch.job.chunkSize=5",
					"spring.batch.job.flatfilewriter.name=fooWriter",
					String.format(
							"spring.batch.job.flatfilewriter.resource=file://%s",
							this.outputFile.getAbsolutePath()),
					"spring.batch.job.flatfilewriter.encoding=UTF-8",
					"spring.batch.job.flatfilewriter.delimited=true",
					"spring.batch.job.flatfilewriter.names=item");

	applicationContextRunner.run((context) -> {
		JobLauncher jobLauncher = context.getBean(JobLauncher.class);

		Job job = context.getBean(Job.class);

		JobExecution jobExecution = jobLauncher.run(job, new JobParameters());

		JobExplorer jobExplorer = context.getBean(JobExplorer.class);

		while (jobExplorer.getJobExecution(jobExecution.getJobId()).isRunning()) {
			Thread.sleep(1000);
		}

		AssertFile.assertLineCount(5, this.outputFile);

		String results = FileCopyUtils.copyToString(new InputStreamReader(
				new FileSystemResource(this.outputFile).getInputStream()));
		assertThat(results).isEqualTo("header\nfoo\nbar\nbaz\nfooter");
	});
}
 
Example #23
Source File: BatchJobConfiguration.java    From patient-batch-loader with GNU General Public License v3.0 5 votes vote down vote up
@Bean
public Job job(Step step) throws Exception {
    return this.jobBuilderFactory
        .get(Constants.JOB_NAME)
        .validator(validator())
        .start(step)
        .build();
}
 
Example #24
Source File: AdHocStarterTest.java    From spring-batch-rest with Apache License 2.0 5 votes vote down vote up
private Job createJobFromStepExecutionConsumer(Set<String> readPropertyValues, CountDownLatch latch) {
    return jobBuilder.createJobFromStepExecutionConsumer(JOB_NAME, (stepExecution) -> {
        String propertyValue = Optional.ofNullable(stepExecution.getJobParameters().getString(PROPERTY_NAME))
                .orElseGet(() -> env.getProperty(PROPERTY_NAME));
        readPropertyValues.add(propertyValue);
        latch.countDown();
    });
}
 
Example #25
Source File: TaskletsConfig.java    From tutorials with MIT License 5 votes vote down vote up
@Bean
public Job job() {
    return jobs
      .get("taskletsJob")
      .start(readLines())
      .next(processLines())
      .next(writeLines())
      .build();
}
 
Example #26
Source File: FlatFileItemWriterAutoConfigurationTests.java    From spring-cloud-task with Apache License 2.0 5 votes vote down vote up
@Test
public void testFormattedFieldExtractorFileGeneration() {
	ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
			.withUserConfiguration(FormattedFieldExtractorJobConfiguration.class)
			.withConfiguration(
					AutoConfigurations.of(PropertyPlaceholderAutoConfiguration.class,
							BatchAutoConfiguration.class,
							SingleStepJobAutoConfiguration.class,
							FlatFileItemWriterAutoConfiguration.class))
			.withPropertyValues("spring.batch.job.jobName=job",
					"spring.batch.job.stepName=step1", "spring.batch.job.chunkSize=5",
					"spring.batch.job.flatfilewriter.name=fooWriter",
					String.format(
							"spring.batch.job.flatfilewriter.resource=file://%s",
							this.outputFile.getAbsolutePath()),
					"spring.batch.job.flatfilewriter.encoding=UTF-8",
					"spring.batch.job.flatfilewriter.formatted=true",
					"spring.batch.job.flatfilewriter.names=item",
					"spring.batch.job.flatfilewriter.format=item = %s");

	applicationContextRunner.run((context) -> {
		JobLauncher jobLauncher = context.getBean(JobLauncher.class);

		Job job = context.getBean(Job.class);

		JobExecution jobExecution = jobLauncher.run(job, new JobParameters());

		JobExplorer jobExplorer = context.getBean(JobExplorer.class);

		while (jobExplorer.getJobExecution(jobExecution.getJobId()).isRunning()) {
			Thread.sleep(1000);
		}

		AssertFile.assertLineCount(3, this.outputFile);

		String results = FileCopyUtils.copyToString(new InputStreamReader(
				new FileSystemResource(this.outputFile).getInputStream()));
		assertThat(results).isEqualTo("item = f\nitem = b\nitem = b\n");
	});
}
 
Example #27
Source File: SpringBatchConfig.java    From tutorials with MIT License 5 votes vote down vote up
@Bean(name = "skippingBatchJob")
public Job skippingJob(@Qualifier("skippingStep") Step skippingStep) {
    return jobBuilderFactory
            .get("skippingBatchJob")
            .start(skippingStep)
            .build();
}
 
Example #28
Source File: DefaultJobService.java    From spring-batch-lightmin with Apache License 2.0 5 votes vote down vote up
@Override
public Job getJobByName(final String jobName) {
    Job job;
    try {
        job = this.jobRegistry.getJob(jobName);
    } catch (final NoSuchJobException e) {
        log.info("Could not find job with jobName: " + jobName);
        job = null;
    }
    return job;
}
 
Example #29
Source File: CsvBatchConfig.java    From Demo with Apache License 2.0 5 votes vote down vote up
@Bean
public Job importJob(JobBuilderFactory jobs, Step s1) {
    return jobs.get("importJob")
            .incrementer(new RunIdIncrementer())
            .flow(s1) //为 job 指定 step
            .end()
            .listener(csvJobListener()) //绑定监听器 csvJobListener
            .build();
}
 
Example #30
Source File: JobRunner.java    From spring-batch-performance-tuning with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
	ApplicationContext applicationContext = new ClassPathXmlApplicationContext(CONFIG);

	Job job = applicationContext.getBean(Job.class);
	JobLauncher jobLauncher = applicationContext.getBean(JobLauncher.class);

	jobLauncher.run(job, new JobParameters());
}