├── .gitignore ├── pom.xml └── src └── main ├── java └── demo │ ├── DeviceCommand.java │ ├── HelloItemProcessor.java │ ├── HelloLineAggregator.java │ ├── HelloLineMapper.java │ └── Main.java └── resources ├── applicationContext.xml └── batch-data-source.csv /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | .idea/ 3 | *.iml 4 | target/ -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | cn.wudashan 8 | spring-batch-demo 9 | 1.0-SNAPSHOT 10 | 11 | 12 | 13 | 14 | 15 | org.springframework.batch 16 | spring-batch-core 17 | 3.0.8.RELEASE 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | org.apache.maven.plugins 28 | maven-compiler-plugin 29 | 30 | 1.8 31 | 1.8 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /src/main/java/demo/DeviceCommand.java: -------------------------------------------------------------------------------- 1 | package demo; 2 | 3 | /** 4 | * 命令DTO类 5 | * Created by wuzhaofeng on 17/7/23. 6 | */ 7 | public class DeviceCommand { 8 | 9 | private String id; 10 | 11 | private String status; 12 | 13 | public String getId() { 14 | return id; 15 | } 16 | 17 | public void setId(String id) { 18 | this.id = id; 19 | } 20 | 21 | public String getStatus() { 22 | return status; 23 | } 24 | 25 | public void setStatus(String status) { 26 | this.status = status; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/demo/HelloItemProcessor.java: -------------------------------------------------------------------------------- 1 | package demo; 2 | 3 | import org.springframework.batch.item.ItemProcessor; 4 | 5 | /** 6 | * Created by wuzhaofeng on 17/7/23. 7 | */ 8 | public class HelloItemProcessor implements ItemProcessor { 9 | 10 | @Override 11 | public DeviceCommand process(DeviceCommand deviceCommand) throws Exception { 12 | 13 | // 模拟下发命令给设备 14 | System.out.println("send command to device, id=" + deviceCommand.getId()); 15 | 16 | // 更新命令状态 17 | deviceCommand.setStatus("SENT"); 18 | 19 | // 返回命令对象 20 | return deviceCommand; 21 | 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/demo/HelloLineAggregator.java: -------------------------------------------------------------------------------- 1 | package demo; 2 | 3 | import org.springframework.batch.item.file.transform.LineAggregator; 4 | 5 | /** 6 | * Created by wuzhaofeng on 17/7/23. 7 | */ 8 | public class HelloLineAggregator implements LineAggregator { 9 | 10 | @Override 11 | public String aggregate(DeviceCommand deviceCommand) { 12 | 13 | StringBuffer sb = new StringBuffer(); 14 | sb.append(deviceCommand.getId()); 15 | sb.append(","); 16 | sb.append(deviceCommand.getStatus()); 17 | return sb.toString(); 18 | 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/demo/HelloLineMapper.java: -------------------------------------------------------------------------------- 1 | package demo; 2 | 3 | import org.springframework.batch.item.file.LineMapper; 4 | 5 | /** 6 | * ORM 7 | * Created by wuzhaofeng on 17/7/23. 8 | */ 9 | public class HelloLineMapper implements LineMapper { 10 | 11 | @Override 12 | public DeviceCommand mapLine(String line, int lineNumber) throws Exception { 13 | 14 | String[] args = line.split(","); 15 | DeviceCommand deviceCommand = new DeviceCommand(); 16 | deviceCommand.setId(args[0]); 17 | deviceCommand.setStatus(args[1]); 18 | return deviceCommand; 19 | 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/demo/Main.java: -------------------------------------------------------------------------------- 1 | package demo; 2 | 3 | import org.springframework.batch.core.Job; 4 | import org.springframework.batch.core.JobParameters; 5 | import org.springframework.batch.core.Step; 6 | import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; 7 | import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; 8 | import org.springframework.batch.core.launch.JobLauncher; 9 | import org.springframework.batch.core.repository.JobRepository; 10 | import org.springframework.batch.item.file.FlatFileItemReader; 11 | import org.springframework.batch.item.file.FlatFileItemWriter; 12 | import org.springframework.context.ApplicationContext; 13 | import org.springframework.context.support.ClassPathXmlApplicationContext; 14 | import org.springframework.core.io.FileSystemResource; 15 | import org.springframework.transaction.PlatformTransactionManager; 16 | 17 | /** 18 | * demo.Demo for spring batch 19 | * Created by wuzhaofeng on 17/7/23. 20 | */ 21 | public class Main { 22 | 23 | 24 | public static void main(String[] args) throws Exception { 25 | 26 | // 加载上下文 27 | String[] configLocations = {"applicationContext.xml"}; 28 | ApplicationContext applicationContext = new ClassPathXmlApplicationContext(configLocations); 29 | 30 | // 获取任务启动器 31 | JobLauncher jobLauncher = applicationContext.getBean(JobLauncher.class); 32 | JobRepository jobRepository = applicationContext.getBean(JobRepository.class); 33 | PlatformTransactionManager transactionManager = applicationContext.getBean(PlatformTransactionManager.class); 34 | 35 | // 创建reader 36 | FlatFileItemReader flatFileItemReader = new FlatFileItemReader<>(); 37 | flatFileItemReader.setResource(new FileSystemResource("src/main/resources/batch-data-source.csv")); 38 | flatFileItemReader.setLineMapper(new HelloLineMapper()); 39 | 40 | // 创建processor 41 | HelloItemProcessor helloItemProcessor = new HelloItemProcessor(); 42 | 43 | // 创建writer 44 | FlatFileItemWriter flatFileItemWriter = new FlatFileItemWriter<>(); 45 | flatFileItemWriter.setResource(new FileSystemResource("src/main/resources/batch-data-target.csv")); 46 | flatFileItemWriter.setLineAggregator(new HelloLineAggregator()); 47 | 48 | 49 | // 创建Step 50 | StepBuilderFactory stepBuilderFactory = new StepBuilderFactory(jobRepository, transactionManager); 51 | Step step = stepBuilderFactory.get("step") 52 | .chunk(1) 53 | .reader(flatFileItemReader) // 读操作 54 | .processor(helloItemProcessor) // 处理操作 55 | .writer(flatFileItemWriter) // 写操作 56 | .build(); 57 | 58 | // 创建Job 59 | JobBuilderFactory jobBuilderFactory = new JobBuilderFactory(jobRepository); 60 | Job job = jobBuilderFactory.get("job") 61 | .start(step) 62 | .build(); 63 | 64 | // 启动任务 65 | jobLauncher.run(job, new JobParameters()); 66 | 67 | } 68 | 69 | } 70 | -------------------------------------------------------------------------------- /src/main/resources/applicationContext.xml: -------------------------------------------------------------------------------- 1 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /src/main/resources/batch-data-source.csv: -------------------------------------------------------------------------------- 1 | 1,PENDING 2 | 2,PENDING 3 | 3,PENDING 4 | 4,PENDING 5 | 5,PENDING 6 | 6,PENDING 7 | 7,PENDING 8 | 8,PENDING 9 | 9,PENDING 10 | 10,PENDING 11 | --------------------------------------------------------------------------------