Search in sources :

Example 1 with Step

use of org.springframework.batch.core.Step 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 2 with Step

use of org.springframework.batch.core.Step in project hub-alert by blackducksoftware.

the class CommonConfigTest method testCreateStep.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testCreateStep() {
    final StepBuilderFactory stepBuilderFactory = Mockito.mock(StepBuilderFactory.class);
    final TaskExecutor taskExecutor = Mockito.mock(TaskExecutor.class);
    final PlatformTransactionManager transactionManager = Mockito.mock(PlatformTransactionManager.class);
    final C config = getConfigWithParams(stepBuilderFactory, taskExecutor, transactionManager);
    final R reader = getMockReader();
    final P processor = getMockProcessor();
    final W writer = getMockWriter();
    final StepBuilder stepBuilder = Mockito.mock(StepBuilder.class);
    final SimpleStepBuilder simpleStepBuilder = Mockito.mock(SimpleStepBuilder.class);
    final AbstractTaskletStepBuilder abstractTaskletStepBuilder = Mockito.mock(AbstractTaskletStepBuilder.class);
    Mockito.when(stepBuilderFactory.get(Mockito.anyString())).thenReturn(stepBuilder);
    Mockito.when(stepBuilder.chunk(Mockito.anyInt())).thenReturn(simpleStepBuilder);
    Mockito.when(simpleStepBuilder.reader(reader)).thenReturn(simpleStepBuilder);
    Mockito.when(simpleStepBuilder.processor(processor)).thenReturn(simpleStepBuilder);
    Mockito.when(simpleStepBuilder.writer(writer)).thenReturn(simpleStepBuilder);
    Mockito.when(simpleStepBuilder.taskExecutor(taskExecutor)).thenReturn(abstractTaskletStepBuilder);
    Mockito.when(abstractTaskletStepBuilder.transactionManager(transactionManager)).thenReturn(abstractTaskletStepBuilder);
    Mockito.when(abstractTaskletStepBuilder.build()).thenReturn(new TaskletStep());
    final Step step = config.createStep(reader, processor, writer);
    assertNotNull(step);
}
Also used : AbstractTaskletStepBuilder(org.springframework.batch.core.step.builder.AbstractTaskletStepBuilder) TaskExecutor(org.springframework.core.task.TaskExecutor) AbstractTaskletStepBuilder(org.springframework.batch.core.step.builder.AbstractTaskletStepBuilder) SimpleStepBuilder(org.springframework.batch.core.step.builder.SimpleStepBuilder) StepBuilder(org.springframework.batch.core.step.builder.StepBuilder) SimpleStepBuilder(org.springframework.batch.core.step.builder.SimpleStepBuilder) StepBuilderFactory(org.springframework.batch.core.configuration.annotation.StepBuilderFactory) TaskletStep(org.springframework.batch.core.step.tasklet.TaskletStep) Step(org.springframework.batch.core.Step) TaskletStep(org.springframework.batch.core.step.tasklet.TaskletStep) PlatformTransactionManager(org.springframework.transaction.PlatformTransactionManager) Test(org.junit.Test)

Aggregations

Step (org.springframework.batch.core.Step)2 StepBuilderFactory (org.springframework.batch.core.configuration.annotation.StepBuilderFactory)2 PlatformTransactionManager (org.springframework.transaction.PlatformTransactionManager)2 DemoLineAggregator (org.family.aggregator.DemoLineAggregator)1 DeviceCommand (org.family.dto.DeviceCommand)1 DemoLineMapper (org.family.mapper.DemoLineMapper)1 DemoProcessor (org.family.processor.DemoProcessor)1 Test (org.junit.Test)1 Job (org.springframework.batch.core.Job)1 JobParameters (org.springframework.batch.core.JobParameters)1 JobParametersInvalidException (org.springframework.batch.core.JobParametersInvalidException)1 JobBuilderFactory (org.springframework.batch.core.configuration.annotation.JobBuilderFactory)1 JobLauncher (org.springframework.batch.core.launch.JobLauncher)1 JobExecutionAlreadyRunningException (org.springframework.batch.core.repository.JobExecutionAlreadyRunningException)1 JobInstanceAlreadyCompleteException (org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException)1 JobRepository (org.springframework.batch.core.repository.JobRepository)1 JobRestartException (org.springframework.batch.core.repository.JobRestartException)1 AbstractTaskletStepBuilder (org.springframework.batch.core.step.builder.AbstractTaskletStepBuilder)1 SimpleStepBuilder (org.springframework.batch.core.step.builder.SimpleStepBuilder)1 StepBuilder (org.springframework.batch.core.step.builder.StepBuilder)1