Search in sources :

Example 1 with JobRepository

use of org.springframework.batch.core.repository.JobRepository in project spring-boot by spring-projects.

the class BatchAutoConfigurationTests method testUsingJpa.

@Test
public void testUsingJpa() throws Exception {
    this.context = new AnnotationConfigApplicationContext();
    // The order is very important here: DataSource -> Hibernate -> Batch
    this.context.register(TestConfiguration.class, EmbeddedDataSourceConfiguration.class, HibernateJpaAutoConfiguration.class, BatchAutoConfiguration.class, TransactionAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class);
    this.context.refresh();
    PlatformTransactionManager transactionManager = this.context.getBean(PlatformTransactionManager.class);
    // It's a lazy proxy, but it does render its target if you ask for toString():
    assertThat(transactionManager.toString().contains("JpaTransactionManager")).isTrue();
    assertThat(this.context.getBean(EntityManagerFactory.class)).isNotNull();
    // Ensure the JobRepository can be used (no problem with isolation level)
    assertThat(this.context.getBean(JobRepository.class).getLastJobExecution("job", new JobParameters())).isNull();
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) JobParameters(org.springframework.batch.core.JobParameters) JobRepository(org.springframework.batch.core.repository.JobRepository) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) Test(org.junit.Test)

Example 2 with JobRepository

use of org.springframework.batch.core.repository.JobRepository in project spring-boot by spring-projects.

the class BatchAutoConfigurationTests method testDefinesAndLaunchesNamedJob.

@Test
public void testDefinesAndLaunchesNamedJob() throws Exception {
    this.context = new AnnotationConfigApplicationContext();
    EnvironmentTestUtils.addEnvironment(this.context, "spring.batch.job.names:discreteRegisteredJob");
    this.context.register(NamedJobConfigurationWithRegisteredJob.class, EmbeddedDataSourceConfiguration.class, BatchAutoConfiguration.class, TransactionAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class);
    this.context.refresh();
    JobRepository repository = this.context.getBean(JobRepository.class);
    assertThat(this.context.getBean(JobLauncher.class)).isNotNull();
    this.context.getBean(JobLauncherCommandLineRunner.class).run();
    assertThat(repository.getLastJobExecution("discreteRegisteredJob", new JobParameters())).isNotNull();
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) JobParameters(org.springframework.batch.core.JobParameters) JobRepository(org.springframework.batch.core.repository.JobRepository) Test(org.junit.Test)

Example 3 with JobRepository

use of org.springframework.batch.core.repository.JobRepository in project spring-boot by spring-projects.

the class BatchAutoConfigurationTests method testRenamePrefix.

@Test
public void testRenamePrefix() throws Exception {
    this.context = new AnnotationConfigApplicationContext();
    EnvironmentTestUtils.addEnvironment(this.context, "spring.datasource.name:batchtest", "spring.batch.schema:classpath:batch/custom-schema-hsql.sql", "spring.batch.tablePrefix:PREFIX_");
    this.context.register(TestConfiguration.class, EmbeddedDataSourceConfiguration.class, HibernateJpaAutoConfiguration.class, BatchAutoConfiguration.class, TransactionAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class);
    this.context.refresh();
    assertThat(this.context.getBean(JobLauncher.class)).isNotNull();
    assertThat(this.context.getBean(BatchProperties.class).getInitializer().isEnabled()).isTrue();
    assertThat(new JdbcTemplate(this.context.getBean(DataSource.class)).queryForList("select * from PREFIX_JOB_EXECUTION")).isEmpty();
    JobExplorer jobExplorer = this.context.getBean(JobExplorer.class);
    assertThat(jobExplorer.findRunningJobExecutions("test")).isEmpty();
    JobRepository jobRepository = this.context.getBean(JobRepository.class);
    assertThat(jobRepository.getLastJobExecution("test", new JobParameters())).isNull();
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) JobExplorer(org.springframework.batch.core.explore.JobExplorer) JobParameters(org.springframework.batch.core.JobParameters) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) JobRepository(org.springframework.batch.core.repository.JobRepository) Test(org.junit.Test)

Example 4 with JobRepository

use of org.springframework.batch.core.repository.JobRepository in project spring-boot by spring-projects.

the class JobLauncherCommandLineRunnerTests method init.

@Before
public void init() throws Exception {
    this.context.register(BatchConfiguration.class);
    this.context.refresh();
    JobRepository jobRepository = this.context.getBean(JobRepository.class);
    this.jobLauncher = this.context.getBean(JobLauncher.class);
    this.jobs = new JobBuilderFactory(jobRepository);
    PlatformTransactionManager transactionManager = this.context.getBean(PlatformTransactionManager.class);
    this.steps = new StepBuilderFactory(jobRepository, transactionManager);
    this.step = this.steps.get("step").tasklet(new Tasklet() {

        @Override
        public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
            return null;
        }
    }).build();
    this.job = this.jobs.get("job").start(this.step).build();
    this.jobExplorer = this.context.getBean(JobExplorer.class);
    this.runner = new JobLauncherCommandLineRunner(this.jobLauncher, this.jobExplorer);
    this.context.getBean(BatchConfiguration.class).clear();
}
Also used : SimpleJobLauncher(org.springframework.batch.core.launch.support.SimpleJobLauncher) JobLauncher(org.springframework.batch.core.launch.JobLauncher) StepContribution(org.springframework.batch.core.StepContribution) StepBuilderFactory(org.springframework.batch.core.configuration.annotation.StepBuilderFactory) JobRepository(org.springframework.batch.core.repository.JobRepository) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) Tasklet(org.springframework.batch.core.step.tasklet.Tasklet) ChunkContext(org.springframework.batch.core.scope.context.ChunkContext) JobBuilderFactory(org.springframework.batch.core.configuration.annotation.JobBuilderFactory) RepeatStatus(org.springframework.batch.repeat.RepeatStatus) JobExplorer(org.springframework.batch.core.explore.JobExplorer) Before(org.junit.Before)

Aggregations

JobRepository (org.springframework.batch.core.repository.JobRepository)4 Test (org.junit.Test)3 JobParameters (org.springframework.batch.core.JobParameters)3 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)3 JobExplorer (org.springframework.batch.core.explore.JobExplorer)2 PlatformTransactionManager (org.springframework.transaction.PlatformTransactionManager)2 Before (org.junit.Before)1 StepContribution (org.springframework.batch.core.StepContribution)1 JobBuilderFactory (org.springframework.batch.core.configuration.annotation.JobBuilderFactory)1 StepBuilderFactory (org.springframework.batch.core.configuration.annotation.StepBuilderFactory)1 JobLauncher (org.springframework.batch.core.launch.JobLauncher)1 SimpleJobLauncher (org.springframework.batch.core.launch.support.SimpleJobLauncher)1 ChunkContext (org.springframework.batch.core.scope.context.ChunkContext)1 Tasklet (org.springframework.batch.core.step.tasklet.Tasklet)1 RepeatStatus (org.springframework.batch.repeat.RepeatStatus)1 JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)1