Search in sources :

Example 6 with JobLauncher

use of org.springframework.batch.core.launch.JobLauncher in project camel by apache.

the class SpringBatchEndpointTest method shouldUseJobLauncherFromComponent.

@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);
}
Also used : JobLauncher(org.springframework.batch.core.launch.JobLauncher) RouteBuilder(org.apache.camel.builder.RouteBuilder) FailedToCreateRouteException(org.apache.camel.FailedToCreateRouteException) Test(org.junit.Test)

Example 7 with JobLauncher

use of org.springframework.batch.core.launch.JobLauncher in project tutorials by eugenp.

the class SpringbatchPartitionerApp method main.

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");
    System.out.println("Starting the batch job");
    try {
        final JobExecution execution = jobLauncher.run(job, new JobParameters());
        System.out.println("Job Status : " + execution.getStatus());
        System.out.println("Job succeeded");
    } catch (final Exception e) {
        e.printStackTrace();
        System.out.println("Job failed");
    }
}
Also used : JobExecution(org.springframework.batch.core.JobExecution) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) JobLauncher(org.springframework.batch.core.launch.JobLauncher) JobParameters(org.springframework.batch.core.JobParameters) Job(org.springframework.batch.core.Job)

Example 8 with JobLauncher

use of org.springframework.batch.core.launch.JobLauncher in project Spring-Family by Sierou-Java.

the class Main method main.

public static void main(String[] args) {
    try {
        String[] configLocations = { "classpath*:applicationContext.xml" };
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(configLocations);
        JobLauncher jobLauncher = applicationContext.getBean(JobLauncher.class);
        JobRepository jobRepository = applicationContext.getBean(JobRepository.class);
        PlatformTransactionManager transactionManager = applicationContext.getBean(PlatformTransactionManager.class);
        // 创建reader
        FlatFileItemReader<DeviceCommand> flatFileItemReader = new FlatFileItemReader<>();
        flatFileItemReader.setResource(new FileSystemResource("/project/Spring-Family-Parent/Spring-Family-Batch/src/main/resources/batch-data.csv"));
        // // 转换数据
        flatFileItemReader.setLineMapper(new DemoLineMapper());
        // 创建processor 对数据进行操作
        DemoProcessor helloItemProcessor = new DemoProcessor();
        // 创建writer
        FlatFileItemWriter<DeviceCommand> flatFileItemWriter = new FlatFileItemWriter<>();
        flatFileItemWriter.setResource(new FileSystemResource("/project/Spring-Family-Parent/Spring-Family-Batch/src/main/resources/batch-data.csv"));
        flatFileItemWriter.setLineAggregator(new DemoLineAggregator());
        // 创建Step
        StepBuilderFactory stepBuilderFactory = new StepBuilderFactory(jobRepository, transactionManager);
        Step step = stepBuilderFactory.get("step").<DeviceCommand, DeviceCommand>chunk(1).reader(// 读操作
        flatFileItemReader).processor(// 处理操作
        helloItemProcessor).writer(// 写操作
        flatFileItemWriter).build();
        // 创建Job
        JobBuilderFactory jobBuilderFactory = new JobBuilderFactory(jobRepository);
        Job job = jobBuilderFactory.get("job").start(step).build();
        // 启动任务
        jobLauncher.run(job, new JobParameters());
    } catch (JobExecutionAlreadyRunningException e) {
        e.printStackTrace();
    } catch (JobRestartException e) {
        e.printStackTrace();
    } catch (JobInstanceAlreadyCompleteException e) {
        e.printStackTrace();
    } catch (JobParametersInvalidException e) {
        e.printStackTrace();
    }
}
Also used : JobLauncher(org.springframework.batch.core.launch.JobLauncher) FlatFileItemReader(org.springframework.batch.item.file.FlatFileItemReader) DemoProcessor(org.family.processor.DemoProcessor) JobExecutionAlreadyRunningException(org.springframework.batch.core.repository.JobExecutionAlreadyRunningException) FileSystemResource(org.springframework.core.io.FileSystemResource) Step(org.springframework.batch.core.Step) JobRepository(org.springframework.batch.core.repository.JobRepository) ApplicationContext(org.springframework.context.ApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) JobParametersInvalidException(org.springframework.batch.core.JobParametersInvalidException) Job(org.springframework.batch.core.Job) DemoLineAggregator(org.family.aggregator.DemoLineAggregator) StepBuilderFactory(org.springframework.batch.core.configuration.annotation.StepBuilderFactory) DeviceCommand(org.family.dto.DeviceCommand) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) JobInstanceAlreadyCompleteException(org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException) JobBuilderFactory(org.springframework.batch.core.configuration.annotation.JobBuilderFactory) DemoLineMapper(org.family.mapper.DemoLineMapper) FlatFileItemWriter(org.springframework.batch.item.file.FlatFileItemWriter) JobParameters(org.springframework.batch.core.JobParameters) JobRestartException(org.springframework.batch.core.repository.JobRestartException)

Example 9 with JobLauncher

use of org.springframework.batch.core.launch.JobLauncher in project tutorials by eugenp.

the class App method main.

public static void main(final String[] args) {
    // Spring Java config
    final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.register(SpringConfig.class);
    context.register(SpringBatchConfig.class);
    context.refresh();
    // Spring xml config
    // ApplicationContext context = new ClassPathXmlApplicationContext("spring-batch.xml");
    final JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");
    final Job job = (Job) context.getBean("firstBatchJob");
    System.out.println("Starting the batch job");
    try {
        final JobExecution execution = jobLauncher.run(job, new JobParameters());
        System.out.println("Job Status : " + execution.getStatus());
        System.out.println("Job succeeded");
    } catch (final Exception e) {
        e.printStackTrace();
        System.out.println("Job failed");
    }
}
Also used : JobExecution(org.springframework.batch.core.JobExecution) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) JobLauncher(org.springframework.batch.core.launch.JobLauncher) JobParameters(org.springframework.batch.core.JobParameters) Job(org.springframework.batch.core.Job)

Aggregations

JobLauncher (org.springframework.batch.core.launch.JobLauncher)9 Test (org.junit.Test)6 JobExecution (org.springframework.batch.core.JobExecution)5 Date (java.util.Date)3 FailedToCreateRouteException (org.apache.camel.FailedToCreateRouteException)3 RouteBuilder (org.apache.camel.builder.RouteBuilder)3 Job (org.springframework.batch.core.Job)3 JobInstance (org.springframework.batch.core.JobInstance)3 JobParameters (org.springframework.batch.core.JobParameters)3 JobLocator (org.springframework.batch.core.configuration.JobLocator)3 JobExplorer (org.springframework.batch.core.explore.JobExplorer)3 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)2 Calendar (java.util.Calendar)1 CamelContext (org.apache.camel.CamelContext)1 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)1 SimpleRegistry (org.apache.camel.impl.SimpleRegistry)1 DemoLineAggregator (org.family.aggregator.DemoLineAggregator)1 DeviceCommand (org.family.dto.DeviceCommand)1 DemoLineMapper (org.family.mapper.DemoLineMapper)1 DemoProcessor (org.family.processor.DemoProcessor)1