@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(); }
public static void main(String[] args) { String[] springConfig = { "spring/batch/jobs/job-config.xml" }; @SuppressWarnings("resource") ApplicationContext context = new ClassPathXmlApplicationContext(springConfig); JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher"); Job job = (Job) context.getBean("multiResourceItemReaderJob"); try { JobParameters jobParameters = new JobParametersBuilder().addLong("time",System.currentTimeMillis()).toJobParameters(); JobExecution execution = jobLauncher.run(job, jobParameters); System.out.println("Exit Status : " + execution.getStatus()); System.out.println("Exit Status : " + execution.getAllFailureExceptions()); } catch (Exception e) { e.printStackTrace(); } System.out.println("Done"); }
public static void main(String[] args) { String[] springConfig = { "spring/batch/jobs/job-config.xml" }; @SuppressWarnings("resource") ApplicationContext context = new ClassPathXmlApplicationContext(springConfig); JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher"); Job job = (Job) context.getBean("customReaderWriterProcesorJob"); try { JobParameters jobParameters = new JobParametersBuilder().addLong("time",System.currentTimeMillis()).toJobParameters(); JobExecution execution = jobLauncher.run(job, jobParameters); System.out.println("Exit Status : " + execution.getStatus()); System.out.println("Exit Status : " + execution.getAllFailureExceptions()); } catch (Exception e) { e.printStackTrace(); } System.out.println("Done"); }
public static void main(String[] args) { String[] springConfig = { "spring/batch/jobs/job-config.xml" }; @SuppressWarnings("resource") ApplicationContext context = new ClassPathXmlApplicationContext(springConfig); JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher"); Job job = (Job) context.getBean("taskletJob"); try { JobParameters jobParameters = new JobParametersBuilder().addLong("time",System.currentTimeMillis()).toJobParameters(); JobExecution execution = jobLauncher.run(job, jobParameters); System.out.println("Exit Status : " + execution.getStatus()); System.out.println("Exit Status : " + execution.getAllFailureExceptions()); } catch (Exception e) { e.printStackTrace(); } System.out.println("Done"); }
public static void main(String[] args) { String[] springConfig = { "spring/batch/jobs/job-config.xml" }; @SuppressWarnings("resource") ApplicationContext context = new ClassPathXmlApplicationContext(springConfig); JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher"); Job job = (Job) context.getBean("customListeners"); try { JobParameters jobParameters = new JobParametersBuilder().addLong("time",System.currentTimeMillis()).toJobParameters(); JobExecution execution = jobLauncher.run(job, jobParameters); System.out.println("Exit Status : " + execution.getStatus()); System.out.println("Exit Status : " + execution.getAllFailureExceptions()); } catch (Exception e) { e.printStackTrace(); } System.out.println("Done"); }
public static void main(String[] args) { String[] springConfig = { "spring/batch/jobs/job-config.xml" }; @SuppressWarnings("resource") ApplicationContext context = new ClassPathXmlApplicationContext(springConfig); JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher"); Job job = (Job) context.getBean("flatFileItemWriterJob"); try { JobParameters jobParameters = new JobParametersBuilder().addLong("time",System.currentTimeMillis()).toJobParameters(); JobExecution execution = jobLauncher.run(job, jobParameters); System.out.println("Exit Status : " + execution.getStatus()); System.out.println("Exit Status : " + execution.getAllFailureExceptions()); } catch (Exception e) { e.printStackTrace(); } System.out.println("Done"); }
public static void main(String[] args) { String[] springConfig = { "spring/batch/jobs/job-config.xml" }; @SuppressWarnings("resource") ApplicationContext context = new ClassPathXmlApplicationContext(springConfig); JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher"); Job job = (Job) context.getBean("parallelStepsJob"); try { JobParameters jobParameters = new JobParametersBuilder().addLong("time",System.currentTimeMillis()).toJobParameters(); JobExecution execution = jobLauncher.run(job, jobParameters); System.out.println("Exit Status : " + execution.getStatus()); System.out.println("Exit Status : " + execution.getAllFailureExceptions()); } catch (Exception e) { e.printStackTrace(); } System.out.println("Done"); }
public static void main(String[] args) { String[] springConfig = { "spring/batch/jobs/job-config.xml" }; @SuppressWarnings("resource") ApplicationContext context = new ClassPathXmlApplicationContext(springConfig); JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher"); Job job = (Job) context.getBean("chunkJob"); try { JobParameters jobParameters = new JobParametersBuilder().addLong("time",System.currentTimeMillis()).toJobParameters(); JobExecution execution = jobLauncher.run(job, jobParameters); System.out.println("Exit Status : " + execution.getStatus()); System.out.println("Exit Status : " + execution.getAllFailureExceptions()); } catch (Exception e) { e.printStackTrace(); } System.out.println("Done"); }
public static void main(String[] args) { String[] springConfig = { "spring/batch/jobs/job-config.xml" }; @SuppressWarnings("resource") ApplicationContext context = new ClassPathXmlApplicationContext(springConfig); JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher"); Job job = (Job) context.getBean("secuentialControlFlow"); try { JobParameters jobParameters = new JobParametersBuilder().addLong("time",System.currentTimeMillis()).toJobParameters(); JobExecution execution = jobLauncher.run(job, jobParameters); System.out.println("Exit Status : " + execution.getStatus()); System.out.println("Exit Status : " + execution.getAllFailureExceptions()); } catch (Exception e) { e.printStackTrace(); } System.out.println("Done"); }
public static void main(String[] args) { String[] springConfig = { "spring/batch/jobs/job-config.xml" }; @SuppressWarnings("resource") ApplicationContext context = new ClassPathXmlApplicationContext(springConfig); JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher"); Job job = (Job) context.getBean("xmlReadersWritersJob"); try { JobParameters jobParameters = new JobParametersBuilder().addLong("time",System.currentTimeMillis()).toJobParameters(); JobExecution execution = jobLauncher.run(job, jobParameters); System.out.println("Exit Status : " + execution.getStatus()); System.out.println("Exit Status : " + execution.getAllFailureExceptions()); } catch (Exception e) { e.printStackTrace(); } System.out.println("Done"); }
private JobLauncher resolveJobLauncher() { if (jobLauncherRef != null) { JobLauncher jobLauncher = getCamelContext().getRegistry().lookupByNameAndType(jobLauncherRef, JobLauncher.class); if (jobLauncher == null) { throw new IllegalStateException(String.format("No JobLauncher named %s found in the registry.", jobLauncherRef)); } return jobLauncher; } if (defaultResolvedJobLauncher != null) { return defaultResolvedJobLauncher; } if (allResolvedJobLaunchers.size() == 1) { return allResolvedJobLaunchers.values().iterator().next(); } else if (allResolvedJobLaunchers.size() > 1) { throw new IllegalStateException("Expected single jobLauncher instance. Found: " + allResolvedJobLaunchers.size()); } throw new IllegalStateException("Cannot find Spring Batch JobLauncher."); }
@Test public void shouldInjectJobLauncherByReferenceName() throws Exception { // Given context().addRoutes(new RouteBuilder() { @Override public void configure() throws Exception { from("direct:launcherRefTest").to("spring-batch:mockJob?jobLauncher=#alternativeJobLauncher"); } }); // When template.sendBody("direct:launcherRefTest", "Start the job, please."); // Then SpringBatchEndpoint batchEndpoint = context().getEndpoint("spring-batch:mockJob?jobLauncher=#alternativeJobLauncher", SpringBatchEndpoint.class); JobLauncher batchEndpointJobLauncher = (JobLauncher) FieldUtils.readField(batchEndpoint, "jobLauncher", true); assertSame(alternativeJobLauncher, batchEndpointJobLauncher); }
@Test public void shouldResolveAnyJobLauncher() throws Exception { // Given SimpleRegistry registry = new SimpleRegistry(); registry.put("mockJob", job); registry.put("someRandomName", jobLauncher); CamelContext camelContext = new DefaultCamelContext(registry); camelContext.addRoutes(new RouteBuilder() { @Override public void configure() throws Exception { from("direct:start").to("spring-batch:mockJob"); } }); // When camelContext.start(); // Then SpringBatchEndpoint batchEndpoint = camelContext.getEndpoint("spring-batch:mockJob", SpringBatchEndpoint.class); JobLauncher batchEndpointJobLauncher = (JobLauncher) FieldUtils.readField(batchEndpoint, "jobLauncher", true); assertSame(jobLauncher, batchEndpointJobLauncher); }
@Test public void shouldUseJobLauncherFromComponent() throws Exception { // Given SpringBatchComponent batchComponent = new SpringBatchComponent(); batchComponent.setJobLauncher(alternativeJobLauncher); context.addComponent("customBatchComponent", batchComponent); // When context().addRoutes(new RouteBuilder() { @Override public void configure() throws Exception { from("direct:startCustom").to("customBatchComponent:mockJob"); } }); // Then SpringBatchEndpoint batchEndpoint = context().getEndpoint("customBatchComponent:mockJob", SpringBatchEndpoint.class); JobLauncher batchEndpointJobLauncher = (JobLauncher) FieldUtils.readField(batchEndpoint, "jobLauncher", true); assertSame(alternativeJobLauncher, batchEndpointJobLauncher); }
CommandLineRunner runner(JobLauncher launcher, Job job, @Value("${file}") File in, JdbcTemplate jdbcTemplate) { return args -> { JobExecution execution = launcher.run(job, new JobParametersBuilder() .addString("file", in.getAbsolutePath()) .toJobParameters()); System.out.println("execution status: " + execution.getExitStatus().toString()); List<Person> personList = jdbcTemplate.query("select * from PEOPLE", (resultSet, i) -> new Person(resultSet.getString("first"), resultSet.getString("last"), resultSet.getString("email"))); personList.forEach(System.out::println); }; }
@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())); }
public static void main(String[] args) throws Exception { ServerConfigHelper.initLog4j("log4j-shell.xml"); ClassPathXmlApplicationContext ctx = Util.initContext("batch/new-context.xml","batch-file-ac01.xml"); JobLauncher launch = ctx.getBean(JobLauncher.class); JobExplorer epl=ctx.getBean(JobExplorer.class); JobRegistry reg =ctx.getBean(JobRegistry.class); JobOperator jop=ctx.getBean(JobOperator.class); System.out.println(epl.getJobNames()+" "+reg.getJobNames()+" "+jop.toString()); for(String bn: ctx.getBeanFactory().getBeanDefinitionNames()) { System.out.println(bn); } ctx.close(); }
private static void run(final String[] args, final String payloadFilePath) throws Exception { try(final ConfigurableApplicationContext context = SpringApplication.run(PayloadJobMain.class, args)) { final List<JobLaunchingData> jobLaunchingDataList = parseJobParameters(payloadFilePath); final JobLauncher jobLauncher = context.getBean(JobLauncher.class); for(final JobLaunchingData jobLaunchingData : jobLaunchingDataList) { final String jobName = jobLaunchingData.getJobName(); final JobParameters jobParameters = jobLaunchingData.getJobParameters(); final Job job = context.getBean(jobName, Job.class); final JobExecution jobExecution = jobLauncher.run(job, jobParameters); awaitTermination(jobExecution, Duration.ofMinutes(30)); if (!jobExecution.getExitStatus().equals(ExitStatus.COMPLETED)) { throw new JobExecutionUnsuccessfullException(String.format("Job %s was unsuccessful with status %s.", jobName, jobExecution.getExitStatus())); } } } }
public static void main(String[] args) throws Exception { AbstractApplicationContext context = new ClassPathXmlApplicationContext("classpath:/META-INF/spring/application-context.xml"); log.info("Batch TweetCount Application Running"); context.registerShutdownHook(); JobLauncher jobLauncher = context.getBean(JobLauncher.class); Job job = context.getBean(Job.class); jobLauncher.run( job, new JobParametersBuilder() .addString("mr.input", "/tweets/input") .addString("mr.output", "/tweets/output") .addString("localData", "data/nbatweets-small.txt") .addDate("date", new Date()).toJobParameters()); context.close(); }
public static void main( String[] args ) { System.out.println( "Hello World!" ); try { ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("SIGSettleJob.xml"); final JobLauncher jobLauncher = (JobLauncher) applicationContext.getBean("jobLauncher"); final Job job = (Job) applicationContext.getBean("importProducts"); System.out.println("batch context running job " + job); final JobExecution jobExecution = jobLauncher.run(job, new JobParametersBuilder() .addLong("timestamp", System.currentTimeMillis()) .toJobParameters() ); batchStatus = jobExecution.getStatus(); } catch (Exception e) { System.out.println("Exception thrown in batch context" + e.getMessage()); throw new RuntimeException(e); } System.out.println("batch context finished running job: " + batchStatus); }
public static void main(String... args) { AbstractApplicationContext context = null; try { context = new ClassPathXmlApplicationContext(new String[] { "com/javaetmoi/elasticsearch/musicbrainz/batch/applicationContext-datasource.xml", "com/javaetmoi/elasticsearch/musicbrainz/batch/applicationContext-elasticsearch.xml", "com/javaetmoi/elasticsearch/musicbrainz/batch/applicationContext-batch.xml" }); JobLauncher jobLauncher = context.getBean(JobLauncher.class); Job musicAlbumJob = context.getBean("musicAlbumJob", Job.class); jobLauncher.run(musicAlbumJob, new JobParameters()); } catch (Throwable e) { String message = "Job Terminated in error: " + e.getMessage(); LOG.error(message, e); systemExiter.exit(exitCodeMapper.intValue(ExitStatus.FAILED.getExitCode())); } finally { if (context != null) { context.close(); } } systemExiter.exit(exitCodeMapper.intValue(ExitStatus.COMPLETED.getExitCode())); }
@Override protected JobLauncher createJobLauncher() throws Exception { SimpleJobLauncher jobLauncher = new SimpleJobLauncher(); jobLauncher.setJobRepository(super.getJobRepository()); jobLauncher.setTaskExecutor(batchTaskScheduler()); jobLauncher.afterPropertiesSet(); return jobLauncher; }
@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(JobLauncher.class) public JobLauncher getJobLauncher() throws Exception { BatchConfigurer batchConfigurer = getBatchConfigurer(); if (batchConfigurer != null) { return batchConfigurer.getJobLauncher(); } return null; }
@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); }
@Autowired public BlackDuckFortifyPhoneHomeJobConfig(JobLauncher jobLauncher, JobBuilderFactory jobBuilderFactory, StepBuilderFactory stepBuilderFactory, HubServices hubServices, PropertyConstants propertyConstants) { this.jobLauncher = jobLauncher; this.jobBuilderFactory = jobBuilderFactory; this.stepBuilderFactory = stepBuilderFactory; this.hubServices = hubServices; this.propertyConstants = propertyConstants; }
@Bean public JobLauncher getJobLauncher() throws Exception { SimpleJobLauncher jobLauncher = new SimpleJobLauncher(); jobLauncher.setJobRepository(jobRepository().getObject()); jobLauncher.afterPropertiesSet(); return jobLauncher; }
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 @ConditionalOnProperty(prefix = "spring.batch.job", name = "enabled", havingValue = "true", matchIfMissing = true) public JobLauncherCommandLineRunner jobLauncherCommandLineRunner( JobLauncher jobLauncher, JobExplorer jobExplorer) { JobLauncherCommandLineRunner runner = new JobLauncherCommandLineRunner( jobLauncher, jobExplorer); String jobNames = this.properties.getJob().getNames(); if (StringUtils.hasText(jobNames)) { runner.setJobNames(jobNames); } return runner; }
@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 testDefaultContext() throws Exception { this.context = new AnnotationConfigApplicationContext(); this.context.register(TestConfiguration.class, EmbeddedDataSourceConfiguration.class, BatchAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); assertThat(this.context.getBean(JobLauncher.class)).isNotNull(); assertThat(this.context.getBean(JobExplorer.class)).isNotNull(); assertThat(new JdbcTemplate(this.context.getBean(DataSource.class)) .queryForList("select * from BATCH_JOB_EXECUTION")).isEmpty(); }
@Test public void testNoDatabase() throws Exception { this.context = new AnnotationConfigApplicationContext(); this.context.register(TestCustomConfiguration.class, BatchAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); assertThat(this.context.getBean(JobLauncher.class)).isNotNull(); JobExplorer explorer = this.context.getBean(JobExplorer.class); assertThat(explorer).isNotNull(); assertThat(explorer.getJobInstances("job", 0, 100)).isEmpty(); }
@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(); }