protected JobRepository createJobRepository() throws Exception { JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean(); factory.setDataSource(this.dataSource); if (this.entityManagerFactory != null) { logger.warn( "JPA does not support custom isolation levels, so locks may not be taken when launching Jobs"); factory.setIsolationLevelForCreate("ISOLATION_DEFAULT"); } String tablePrefix = this.properties.getTablePrefix(); if (StringUtils.hasText(tablePrefix)) { factory.setTablePrefix(tablePrefix); } factory.setTransactionManager(getTransactionManager()); factory.afterPropertiesSet(); return factory.getObject(); }
@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, 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(); }
@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, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); assertThat(this.context.getBean(JobLauncher.class)).isNotNull(); 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(); }
@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(); }
@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, 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(): assertTrue(transactionManager.toString().contains("JpaTransactionManager")); assertNotNull(this.context.getBean(EntityManagerFactory.class)); // Ensure the JobRepository can be used (no problem with isolation level) assertNull(this.context.getBean(JobRepository.class).getLastJobExecution("job", new JobParameters())); }
@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, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); assertNotNull(this.context.getBean(JobLauncher.class)); assertEquals(0, new JdbcTemplate(this.context.getBean(DataSource.class)) .queryForList("select * from PREFIX_JOB_EXECUTION").size()); JobExplorer jobExplorer = this.context.getBean(JobExplorer.class); assertEquals(0, jobExplorer.findRunningJobExecutions("test").size()); JobRepository jobRepository = this.context.getBean(JobRepository.class); assertNull(jobRepository.getLastJobExecution("test", new JobParameters())); }
/** * @param args */ public static void main(String[] args) { ClassPathXmlApplicationContext c = new ClassPathXmlApplicationContext( "billing_job.xml"); SimpleJobLauncher launcher = new SimpleJobLauncher(); launcher.setJobRepository((JobRepository) c.getBean("jobRepository")); launcher.setTaskExecutor(new SyncTaskExecutor()); try { Map<String, JobParameter> parameters = new HashMap<String, JobParameter>(); parameters.put(RUN_MONTH_KEY, new JobParameter("2011-2")); JobExecution je = launcher.run((Job) c.getBean("billingJob"), new JobParameters(parameters)); System.out.println(je); System.out.println(je.getJobInstance()); System.out.println(je.getStepExecutions()); } catch (Exception e) { e.printStackTrace(); } }
public static void main(String[] args) { ClassPathXmlApplicationContext c = new ClassPathXmlApplicationContext( "message_job.xml"); SimpleJobLauncher launcher = new SimpleJobLauncher(); launcher.setJobRepository((JobRepository) c.getBean("jobRepository")); launcher.setTaskExecutor(new SyncTaskExecutor()); try { Map<String, JobParameter> parameters = new HashMap<String, JobParameter>(); parameters.put(RUN_MONTH_KEY, new JobParameter("2011-10")); JobExecution je = launcher.run((Job) c.getBean("messageJob"), new JobParameters(parameters)); System.out.println(je); System.out.println(je.getJobInstance()); System.out.println(je.getStepExecutions()); } catch (Exception e) { e.printStackTrace(); } }
public JobServiceImpl(JobExplorer jobExplorer, JobOperator jobOperator, JobLauncher jobLauncher, JobRegistry jobRegistry, JobRepository jobRepository) { this.jobExplorer = jobExplorer; this.jobOperator = jobOperator; this.jobLauncher = jobLauncher; this.jobRegistry = jobRegistry; this.jobRepository = jobRepository; }
@Bean public JobRepository batchSimpleJobRepository(DataSource dataSource) throws Exception { JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean(); factory.setDataSource(dataSource); factory.setTransactionManager(transactionManager); factory.afterPropertiesSet(); return factory.getObject(); }
@Bean public JobService jobService(JobOperator batchJobOperator, JobRegistry batchJobRegistry, JobExplorer jobExplorer, JobLauncher jobLauncher, JobRepository jobRepository) throws Exception { return new JobServiceImpl(jobExplorer, batchJobOperator, jobLauncher, batchJobRegistry, jobRepository); }
@Bean @ConditionalOnMissingBean(JobRepository.class) public JobRepository getJobRepository() throws Exception { BatchConfigurer batchConfigurer = getBatchConfigurer(); if (batchConfigurer != null) { return batchConfigurer.getJobRepository(); } JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean(); factory.setDataSource(dataSource); factory.setTransactionManager(transactionManager); factory.afterPropertiesSet(); // factory.setIsolationLevelForCreate("ISOLATION_DEFAULT"); return factory.getObject(); }
@Bean @ConditionalOnMissingBean(JobService.class) public JobService jobService(JobOperator batchJobOperator, JobRegistry batchJobRegistry, JobExplorer jobExplorer, JobLauncher jobLauncher, JobRepository jobRepository) throws Exception { return new JobServiceImpl(jobExplorer, batchJobOperator, jobLauncher, batchJobRegistry, jobRepository); }
@Bean public JobRepository jobRepository() throws Exception { JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean(); factory.setDataSource(dataSource); factory.setTransactionManager(transactionManager); factory.afterPropertiesSet(); return (JobRepository) factory.getObject(); }
private static JobRepository createJobRepository(DataSource dataSource, PlatformTransactionManager transactionManager) throws Exception { JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean(); factory.setDataSource(dataSource); factory.setTransactionManager(transactionManager); factory.afterPropertiesSet(); return factory.getObject(); }
private static JobLauncher createJobLauncher(JobRepository jobRepository, TaskExecutor taskExecutor) throws Exception { SimpleJobLauncher jobLauncher = new SimpleJobLauncher(); jobLauncher.setJobRepository(jobRepository); jobLauncher.setTaskExecutor(taskExecutor); jobLauncher.afterPropertiesSet(); return jobLauncher; }
@Bean @ConditionalOnMissingBean(JobOperator.class) public SimpleJobOperator jobOperator(JobExplorer jobExplorer, JobLauncher jobLauncher, ListableJobLocator jobRegistry, JobRepository jobRepository) throws Exception { SimpleJobOperator factory = new SimpleJobOperator(); factory.setJobExplorer(jobExplorer); factory.setJobLauncher(jobLauncher); factory.setJobRegistry(jobRegistry); factory.setJobRepository(jobRepository); if (this.jobParametersConverter != null) { factory.setJobParametersConverter(this.jobParametersConverter); } return factory; }
@Test public void testNoBatchConfiguration() throws Exception { this.context = new AnnotationConfigApplicationContext(); this.context.register(EmptyConfiguration.class, BatchAutoConfiguration.class, EmbeddedDataSourceConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); assertThat(this.context.getBeanNamesForType(JobLauncher.class).length) .isEqualTo(0); assertThat(this.context.getBeanNamesForType(JobRepository.class).length) .isEqualTo(0); }
@Test public void testDefinesAndLaunchesJob() throws Exception { this.context = new AnnotationConfigApplicationContext(); this.context.register(JobConfiguration.class, EmbeddedDataSourceConfiguration.class, BatchAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); assertThat(this.context.getBean(JobLauncher.class)).isNotNull(); this.context.getBean(JobLauncherCommandLineRunner.class).run(); assertThat(this.context.getBean(JobRepository.class).getLastJobExecution("job", new JobParameters())).isNotNull(); }
@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, 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(); }
@Test public void testDefinesAndLaunchesLocalJob() throws Exception { this.context = new AnnotationConfigApplicationContext(); EnvironmentTestUtils.addEnvironment(this.context, "spring.batch.job.names:discreteLocalJob"); this.context.register(NamedJobConfigurationWithLocalJob.class, EmbeddedDataSourceConfiguration.class, BatchAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); assertThat(this.context.getBean(JobLauncher.class)).isNotNull(); this.context.getBean(JobLauncherCommandLineRunner.class).run(); assertThat(this.context.getBean(JobRepository.class) .getLastJobExecution("discreteLocalJob", new JobParameters())) .isNotNull(); }
@Override public JobRepository getJobRepository() throws Exception { if (this.jobRepository == null) { this.factory.afterPropertiesSet(); this.jobRepository = this.factory.getObject(); } return this.jobRepository; }
@Test public void testNoBatchConfiguration() throws Exception { this.context = new AnnotationConfigApplicationContext(); this.context.register(EmptyConfiguration.class, BatchAutoConfiguration.class, EmbeddedDataSourceConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); assertEquals(0, this.context.getBeanNamesForType(JobLauncher.class).length); assertEquals(0, this.context.getBeanNamesForType(JobRepository.class).length); }
@Test public void testDefinesAndLaunchesJob() throws Exception { this.context = new AnnotationConfigApplicationContext(); this.context.register(JobConfiguration.class, EmbeddedDataSourceConfiguration.class, BatchAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); assertNotNull(this.context.getBean(JobLauncher.class)); this.context.getBean(JobLauncherCommandLineRunner.class).run(); assertNotNull(this.context.getBean(JobRepository.class).getLastJobExecution("job", new JobParameters())); }
@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, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); JobRepository repository = this.context.getBean(JobRepository.class); assertNotNull(this.context.getBean(JobLauncher.class)); this.context.getBean(JobLauncherCommandLineRunner.class).run(); assertNotNull(repository.getLastJobExecution("discreteRegisteredJob", new JobParameters())); }
@Test public void testDefinesAndLaunchesLocalJob() throws Exception { this.context = new AnnotationConfigApplicationContext(); EnvironmentTestUtils.addEnvironment(this.context, "spring.batch.job.names:discreteLocalJob"); this.context.register(NamedJobConfigurationWithLocalJob.class, EmbeddedDataSourceConfiguration.class, BatchAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); assertNotNull(this.context.getBean(JobLauncher.class)); this.context.getBean(JobLauncherCommandLineRunner.class).run(); assertNotNull(this.context.getBean(JobRepository.class) .getLastJobExecution("discreteLocalJob", new JobParameters())); }