下面列出了org.springframework.boot.autoconfigure.batch.BatchAutoConfiguration#org.springframework.batch.core.explore.JobExplorer 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Bean
public SimpleJobServiceFactoryBean simpleJobServiceFactoryBean(DataSource dataSource,
JobRepositoryFactoryBean repositoryFactoryBean, JobExplorer jobExplorer,
PlatformTransactionManager dataSourceTransactionManager) {
SimpleJobServiceFactoryBean factoryBean = new SimpleJobServiceFactoryBean();
factoryBean.setDataSource(dataSource);
try {
factoryBean.setJobRepository(repositoryFactoryBean.getObject());
factoryBean.setJobLauncher(new SimpleJobLauncher());
factoryBean.setJobExplorer(jobExplorer);
factoryBean.setTransactionManager(dataSourceTransactionManager);
}
catch (Exception e) {
throw new IllegalStateException(e);
}
return factoryBean;
}
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;
}
@Bean
@ConditionalOnMissingBean(JobOperator.class)
public JobOperator jobOperator(ObjectProvider<JobParametersConverter> jobParametersConverter,
JobExplorer jobExplorer, JobLauncher jobLauncher, ListableJobLocator jobRegistry,
JobRepository jobRepository) throws Exception {
System.out.println("FOOBAR");
SimpleJobOperator factory = new SimpleJobOperator();
factory.setJobExplorer(jobExplorer);
factory.setJobLauncher(jobLauncher);
factory.setJobRegistry(jobRegistry);
factory.setJobRepository(jobRepository);
jobParametersConverter.ifAvailable(factory::setJobParametersConverter);
return factory;
}
public static void configureForJobExecutionsService(JobExplorer jobExplorer) {
when(jobExplorer.getJobInstances(eq(JOB_NAME_1), anyInt(), anyInt())).thenReturn(newArrayList(ji11, ji12));
when(jobExplorer.getJobInstances(eq(JOB_NAME_2), anyInt(), anyInt())).thenReturn(newArrayList(ji21, ji22, ji23));
when(jobExplorer.getJobExecutions(ji11)).thenReturn(newArrayList(je11));
when(jobExplorer.getJobExecutions(ji12)).thenReturn(newArrayList(je12));
when(jobExplorer.getJobExecutions(ji21)).thenReturn(newArrayList(je21));
when(jobExplorer.getJobExecutions(ji22)).thenReturn(newArrayList(je22));
when(jobExplorer.getJobExecutions(ji23)).thenReturn(newArrayList(je23, je24));
}
private Collection<StepExecution> getStepExecutions() {
JobExplorer jobExplorer = this.applicationContext.getBean(JobExplorer.class);
List<JobInstance> jobInstances = jobExplorer.findJobInstancesByJobName("job", 0, 1);
assertEquals(1, jobInstances.size());
JobInstance jobInstance = jobInstances.get(0);
List<JobExecution> jobExecutions = jobExplorer.getJobExecutions(jobInstance);
assertEquals(1, jobExecutions.size());
JobExecution jobExecution = jobExecutions.get(0);
return jobExecution.getStepExecutions();
}
@Bean
BatchConfigurer myBatchConfigurer(DataSource dataSource,
Jackson2ExecutionContextStringSerializer myJackson2ExecutionContextStringSerializer,
PlatformTransactionManager transactionManager) {
return new DefaultBatchConfigurer(dataSource) {
@Override
protected JobExplorer createJobExplorer() throws Exception {
JobExplorerFactoryBean jobExplorerFactoryBean = new JobExplorerFactoryBean();
jobExplorerFactoryBean.setDataSource(dataSource);
jobExplorerFactoryBean
.setSerializer(myJackson2ExecutionContextStringSerializer);
jobExplorerFactoryBean.afterPropertiesSet();
return jobExplorerFactoryBean.getObject();
}
@Override
protected JobRepository createJobRepository() throws Exception {
JobRepositoryFactoryBean jobRepositoryFactoryBean = new JobRepositoryFactoryBean();
jobRepositoryFactoryBean.setDataSource(dataSource);
jobRepositoryFactoryBean
.setSerializer(myJackson2ExecutionContextStringSerializer);
jobRepositoryFactoryBean.setTransactionManager(transactionManager);
jobRepositoryFactoryBean.afterPropertiesSet();
return jobRepositoryFactoryBean.getObject();
}
};
}
@Bean
public StepExecutionRequestHandler stepExecutionRequestHandler(
JobExplorer jobExplorer, BeanFactoryStepLocator stepLocator){
StepExecutionRequestHandler handler = new StepExecutionRequestHandler();
handler.setJobExplorer(jobExplorer);
handler.setStepLocator(stepLocator);
return handler;
}
@Bean
@Autowired
public JobExplorer jobExplorer(@Qualifier("jobRepositoryDataSource")
DataSource dataSource) throws Exception{
JobExplorerFactoryBean factory = new JobExplorerFactoryBean();
factory.setDataSource(dataSource);
factory.afterPropertiesSet();
return factory.getObject();
}
@Bean
@Profile("worker")
public DeployerStepExecutionHandler stepExecutionHandler(JobExplorer jobExplorer) {
DeployerStepExecutionHandler handler = new DeployerStepExecutionHandler(this.context, jobExplorer, this.jobRepository);
return handler;
}
private Collection<StepExecution> getStepExecutions() {
JobExplorer jobExplorer = this.applicationContext.getBean(JobExplorer.class);
List<JobInstance> jobInstances = jobExplorer.findJobInstancesByJobName("job", 0, 1);
assertEquals(1, jobInstances.size());
JobInstance jobInstance = jobInstances.get(0);
List<JobExecution> jobExecutions = jobExplorer.getJobExecutions(jobInstance);
assertEquals(1, jobExecutions.size());
JobExecution jobExecution = jobExecutions.get(0);
return jobExecution.getStepExecutions();
}
@Bean
public SimpleJobServiceFactoryBean simpleJobServiceFactoryBean(DataSource dataSource,
JobRepositoryFactoryBean repositoryFactoryBean, JobExplorer jobExplorer,
PlatformTransactionManager dataSourceTransactionManager) throws Exception {
SimpleJobServiceFactoryBean factoryBean = new SimpleJobServiceFactoryBean();
factoryBean.setDataSource(dataSource);
factoryBean.setJobRepository(repositoryFactoryBean.getObject());
factoryBean.setJobLauncher(new SimpleJobLauncher());
factoryBean.setDataSource(dataSource);
factoryBean.setJobExplorer(jobExplorer);
factoryBean.setTransactionManager(dataSourceTransactionManager);
return factoryBean;
}
public DefaultJobService(final JobOperator jobOperator,
final JobRegistry jobRegistry,
final JobExplorer jobExplorer,
final LightminJobExecutionDao lightminJobExecutionDao) {
this.jobOperator = jobOperator;
this.jobRegistry = jobRegistry;
this.jobExplorer = jobExplorer;
this.lightminJobExecutionDao = lightminJobExecutionDao;
}
@Test
public void testSimpleConfiguration() {
ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
.withUserConfiguration(SimpleConfiguration.class)
.withConfiguration(
AutoConfigurations.of(PropertyPlaceholderAutoConfiguration.class,
BatchAutoConfiguration.class,
SingleStepJobAutoConfiguration.class))
.withPropertyValues("spring.batch.job.jobName=job",
"spring.batch.job.stepName=step1",
"spring.batch.job.chunkSize=5");
applicationContextRunner.run((context) -> {
JobLauncher jobLauncher = context.getBean(JobLauncher.class);
Job job = context.getBean(Job.class);
ListItemWriter itemWriter = context.getBean(ListItemWriter.class);
JobExecution jobExecution = jobLauncher.run(job, new JobParameters());
JobExplorer jobExplorer = context.getBean(JobExplorer.class);
while (jobExplorer.getJobExecution(jobExecution.getJobId()).isRunning()) {
Thread.sleep(1000);
}
List<Map<Object, Object>> writtenItems = itemWriter.getWrittenItems();
assertThat(writtenItems.size()).isEqualTo(3);
assertThat(writtenItems.get(0).get("item")).isEqualTo("foo");
assertThat(writtenItems.get(1).get("item")).isEqualTo("bar");
assertThat(writtenItems.get(2).get("item")).isEqualTo("baz");
});
}
@Test
public void testCustomLineMapper() {
ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
.withUserConfiguration(CustomLineMapperConfiguration.class)
.withConfiguration(
AutoConfigurations.of(PropertyPlaceholderAutoConfiguration.class,
BatchAutoConfiguration.class,
SingleStepJobAutoConfiguration.class,
FlatFileItemReaderAutoConfiguration.class))
.withPropertyValues("spring.batch.job.jobName=job",
"spring.batch.job.stepName=step1", "spring.batch.job.chunkSize=5",
"spring.batch.job.flatfilereader.name=fixedWidthConfiguration",
"spring.batch.job.flatfilereader.resource=/test.txt",
"spring.batch.job.flatfilereader.strict=true");
applicationContextRunner.run((context) -> {
JobLauncher jobLauncher = context.getBean(JobLauncher.class);
Job job = context.getBean(Job.class);
ListItemWriter itemWriter = context.getBean(ListItemWriter.class);
JobExecution jobExecution = jobLauncher.run(job, new JobParameters());
JobExplorer jobExplorer = context.getBean(JobExplorer.class);
while (jobExplorer.getJobExecution(jobExecution.getJobId()).isRunning()) {
Thread.sleep(1000);
}
List writtenItems = itemWriter.getWrittenItems();
assertThat(writtenItems.size()).isEqualTo(8);
});
}
@Test
public void testCustomMapping() {
ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
.withUserConfiguration(CustomMappingConfiguration.class)
.withConfiguration(AutoConfigurations.of(
PropertyPlaceholderAutoConfiguration.class,
BatchAutoConfiguration.class,
SingleStepJobAutoConfiguration.class,
FlatFileItemReaderAutoConfiguration.class, RangeConverter.class))
.withPropertyValues("spring.batch.job.jobName=job",
"spring.batch.job.stepName=step1", "spring.batch.job.chunkSize=5",
"spring.batch.job.flatfilereader.name=fixedWidthConfiguration",
"spring.batch.job.flatfilereader.resource=/test.txt",
"spring.batch.job.flatfilereader.maxItemCount=1",
"spring.batch.job.flatfilereader.strict=true");
applicationContextRunner.run((context) -> {
JobLauncher jobLauncher = context.getBean(JobLauncher.class);
Job job = context.getBean(Job.class);
ListItemWriter itemWriter = context.getBean(ListItemWriter.class);
JobExecution jobExecution = jobLauncher.run(job, new JobParameters());
JobExplorer jobExplorer = context.getBean(JobExplorer.class);
while (jobExplorer.getJobExecution(jobExecution.getJobId()).isRunning()) {
Thread.sleep(1000);
}
List<Map<Object, Object>> writtenItems = itemWriter.getWrittenItems();
assertThat(writtenItems.size()).isEqualTo(1);
assertThat(writtenItems.get(0).get("one")).isEqualTo("1 2 3");
assertThat(writtenItems.get(0).get("two")).isEqualTo("4 5 six");
});
}
@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");
});
}
@Test
public void testFieldExtractorFileGeneration() {
ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
.withUserConfiguration(FieldExtractorConfiguration.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");
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("f\nb\nb\n");
});
}
@Test
public void testCustomLineAggregatorFileGeneration() {
ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
.withUserConfiguration(LineAggregatorConfiguration.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");
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=foo}\n{item=bar}\n{item=baz}\n");
});
}
@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");
});
}
/**
* Create a new {@link TaskJobLauncherCommandLineRunner}.
* @param jobLauncher to launch jobs
* @param jobExplorer to check the job repository for previous executions
* @param jobRepository to check if a job instance exists with the given parameters
* when running a job
* @param taskBatchProperties the properties used to configure the
* taskBatchProperties.
*/
public TaskJobLauncherCommandLineRunner(JobLauncher jobLauncher,
JobExplorer jobExplorer, JobRepository jobRepository,
TaskBatchProperties taskBatchProperties) {
super(jobLauncher, jobExplorer, jobRepository);
this.taskJobLauncher = jobLauncher;
this.taskJobExplorer = jobExplorer;
this.taskJobRepository = jobRepository;
this.taskBatchProperties = taskBatchProperties;
}
/**
* Constructor initializing the DeployerPartitionHandler instance.
* @param taskLauncher the launcher used to execute partitioned tasks.
* @param jobExplorer used to acquire the status of the job.
* @param resource the url to the app to be launched.
* @param stepName the name of the step.
* @deprecated Use the constructor that accepts {@link TaskRepository} as well as the
* this constructor's set of parameters.
*/
@Deprecated
public DeployerPartitionHandler(TaskLauncher taskLauncher, JobExplorer jobExplorer,
Resource resource, String stepName) {
Assert.notNull(taskLauncher, "A taskLauncher is required");
Assert.notNull(jobExplorer, "A jobExplorer is required");
Assert.notNull(resource, "A resource is required");
Assert.hasText(stepName, "A step name is required");
this.taskLauncher = taskLauncher;
this.jobExplorer = jobExplorer;
this.resource = resource;
this.stepName = stepName;
}
public DeployerPartitionHandler(TaskLauncher taskLauncher, JobExplorer jobExplorer,
Resource resource, String stepName, TaskRepository taskRepository) {
Assert.notNull(taskLauncher, "A taskLauncher is required");
Assert.notNull(jobExplorer, "A jobExplorer is required");
Assert.notNull(resource, "A resource is required");
Assert.hasText(stepName, "A step name is required");
Assert.notNull(taskRepository, "A TaskRepository is required");
this.taskLauncher = taskLauncher;
this.jobExplorer = jobExplorer;
this.resource = resource;
this.stepName = stepName;
this.taskRepository = taskRepository;
}
public DeployerStepExecutionHandler(BeanFactory beanFactory, JobExplorer jobExplorer,
JobRepository jobRepository) {
Assert.notNull(beanFactory, "A beanFactory is required");
Assert.notNull(jobExplorer, "A jobExplorer is required");
Assert.notNull(jobRepository, "A jobRepository is required");
this.stepLocator = new BeanFactoryStepLocator();
((BeanFactoryStepLocator) this.stepLocator).setBeanFactory(beanFactory);
this.jobExplorer = jobExplorer;
this.jobRepository = jobRepository;
}
@Bean
public TaskJobLauncherCommandLineRunnerFactoryBean jobLauncherCommandLineRunner(
JobLauncher jobLauncher, JobExplorer jobExplorer, List<Job> jobs,
JobRegistry jobRegistry, JobRepository jobRepository,
BatchProperties batchProperties) {
TaskJobLauncherCommandLineRunnerFactoryBean taskJobLauncherCommandLineRunnerFactoryBean;
taskJobLauncherCommandLineRunnerFactoryBean = new TaskJobLauncherCommandLineRunnerFactoryBean(
jobLauncher, jobExplorer, jobs, this.properties, jobRegistry,
jobRepository, batchProperties);
return taskJobLauncherCommandLineRunnerFactoryBean;
}
@Test
public void testNoTaskJobLauncher() {
String[] enabledArgs = new String[] {
"--spring.cloud.task.batch.failOnJobFailure=true",
"--spring.cloud.task.batch.failOnJobFailurePollInterval=500",
"--spring.batch.job.enabled=false" };
this.applicationContext = SpringApplication.run(new Class[] {
TaskJobLauncherCommandLineRunnerTests.JobWithFailureConfiguration.class },
enabledArgs);
JobExplorer jobExplorer = this.applicationContext.getBean(JobExplorer.class);
assertThat(jobExplorer.getJobNames().size()).isEqualTo(0);
}
private void validateDeprecatedConstructorValidation(TaskLauncher taskLauncher,
JobExplorer jobExplorer, Resource resource, String stepName,
String expectedMessage) {
try {
new DeployerPartitionHandler(taskLauncher, jobExplorer, resource, stepName,
this.taskRepository);
}
catch (IllegalArgumentException iae) {
assertThat(iae.getMessage()).isEqualTo(expectedMessage);
}
}
private void validateConstructorValidation(TaskLauncher taskLauncher,
JobExplorer jobExplorer, Resource resource, String stepName,
TaskRepository taskRepository, String expectedMessage) {
try {
new DeployerPartitionHandler(taskLauncher, jobExplorer, resource, stepName,
taskRepository);
}
catch (IllegalArgumentException iae) {
assertThat(iae.getMessage()).isEqualTo(expectedMessage);
}
}
private void validateConstructorValidation(BeanFactory beanFactory,
JobExplorer jobExplorer, JobRepository jobRepository, String message) {
try {
new DeployerStepExecutionHandler(beanFactory, jobExplorer, jobRepository);
}
catch (IllegalArgumentException iae) {
assertThat(iae.getMessage()).isEqualTo(message);
}
}
@Bean
public JobExplorer jobExplorer() throws Exception {
JobExplorerFactoryBean factory = new JobExplorerFactoryBean();
factory.setDataSource(dataSource);
factory.afterPropertiesSet();
return factory.getObject();
}
public JobMonitoringController(JobOperator jobOperator, JobExplorer jobExplorer,
RunningExecutionTracker runningExecutionTracker) {
super();
this.jobOperator = jobOperator;
this.jobExplorer = jobExplorer;
this.runningExecutionTracker = runningExecutionTracker;
}