Spring Batch官網(wǎng)介紹:
十多年的托克遜網(wǎng)站建設(shè)經(jīng)驗,針對設(shè)計、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。成都全網(wǎng)營銷推廣的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整托克遜建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計,從而大程度地提升瀏覽體驗。創(chuàng)新互聯(lián)從事“托克遜網(wǎng)站設(shè)計”,“托克遜網(wǎng)站推廣”以來,每個客戶項目都認真落實執(zhí)行。
A lightweight, comprehensive batch framework designed to enable the development of robust batch applications vital for the daily operations of enterprise systems.(一款輕量的、全面的批處理框架,用于開發(fā)強大的日常運營的企業(yè)級批處理應(yīng)用程序。)
springbatch
主要實現(xiàn)批量數(shù)據(jù)的處理,我對batch進行的封裝,提出了jobBase類型,具體job需要實現(xiàn)它即可。Spring Batch 不僅提供了統(tǒng)一的讀寫接口、豐富的任務(wù)處理方式、靈活的事務(wù)管理及并發(fā)處理,同時還支持日志、監(jiān)控、任務(wù)重啟與跳過等特性,大大簡化了批處理應(yīng)用開發(fā),將開發(fā)人員從復(fù)雜的任務(wù)配置管理過程中解放出來,使他們可以更多地去關(guān)注核心的業(yè)務(wù)處理過程。
幾個組件
•job
•step
•read
•write
•listener
•process
•validator
JobBase定義了幾個公用的方法
/** * springBatch的job基礎(chǔ)類. */ public abstract class JobBase{ /** * 批次. */ protected int chunkCount = 5000; /** * 監(jiān)聽器. */ private JobExecutionListener jobExecutionListener; /** * 處理器. */ private ValidatingItemProcessor validatingItemProcessor; /** * job名稱. */ private String jobName; /** * 檢驗器. */ private Validator validator; @Autowired private JobBuilderFactory job; @Autowired private StepBuilderFactory step; /** * 初始化. * * @param jobName job名稱 * @param jobExecutionListener 監(jiān)聽器 * @param validatingItemProcessor 處理器 * @param validator 檢驗 */ public JobBase(String jobName, JobExecutionListener jobExecutionListener, ValidatingItemProcessor validatingItemProcessor, Validator validator) { this.jobName = jobName; this.jobExecutionListener = jobExecutionListener; this.validatingItemProcessor = validatingItemProcessor; this.validator = validator; } /** * job初始化與啟動. */ public Job getJob() throws Exception { return job.get(jobName).incrementer(new RunIdIncrementer()) .start(syncStep()) .listener(jobExecutionListener) .build(); } /** * 執(zhí)行步驟. * * @return */ public Step syncStep() throws Exception { return step.get("step1") . chunk(chunkCount) .reader(reader()) .processor(processor()) .writer(writer()) .build(); } /** * 單條處理數(shù)據(jù). * * @return */ public ItemProcessor processor() { validatingItemProcessor.setValidator(processorValidator()); return validatingItemProcessor; } /** * 校驗數(shù)據(jù). * * @return */ @Bean public Validator processorValidator() { return validator; } /** * 批量讀數(shù)據(jù). * * @return * @throws Exception */ public abstract ItemReader reader() throws Exception; /** * 批量寫數(shù)據(jù). * * @return */ @Bean public abstract ItemWriter writer(); }
主要規(guī)定了公用方法的執(zhí)行策略,而具體的job名稱,讀,寫還是需要具體JOB去實現(xiàn)的。
具體Job實現(xiàn)
@Configuration @EnableBatchProcessing public class SyncPersonJob extends JobBase{ @Autowired private DataSource dataSource; @Autowired @Qualifier("primaryJdbcTemplate") private JdbcTemplate jdbcTemplate; /** * 初始化,規(guī)則了job名稱和監(jiān)視器. */ public SyncPersonJob() { super("personJob", new PersonJobListener(), new PersonItemProcessor(), new BeanValidator<>()); } @Override public ItemReader reader() throws Exception { StringBuffer sb = new StringBuffer(); sb.append("select * from person"); String sql = sb.toString(); JdbcCursorItemReader jdbcCursorItemReader = new JdbcCursorItemReader<>(); jdbcCursorItemReader.setSql(sql); jdbcCursorItemReader.setRowMapper(new BeanPropertyRowMapper<>(Person.class)); jdbcCursorItemReader.setDataSource(dataSource); return jdbcCursorItemReader; } @Override @Bean("personJobWriter") public ItemWriter writer() { JdbcBatchItemWriter writer = new JdbcBatchItemWriter (); writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider ()); String sql = "insert into person_export " + "(id,name,age,nation,address) " + "values(:id, :name, :age, :nation,:address)"; writer.setSql(sql); writer.setDataSource(dataSource); return writer; } }
寫操作需要定義自己的bean的聲明
注意,需要為每個job的write啟個名稱,否則在多job時,write將會被打亂
/** * 批量寫數(shù)據(jù). * * @return */ @Override @Bean("personVerson2JobWriter") public ItemWriterwriter() { }
添加一個api,手動觸發(fā)
@Autowired SyncPersonJob syncPersonJob; @Autowired JobLauncher jobLauncher; void exec(Job job) throws Exception { JobParameters jobParameters = new JobParametersBuilder() .addLong("time", System.currentTimeMillis()) .toJobParameters(); jobLauncher.run(job, jobParameters); } @RequestMapping("/run1") public String run1() throws Exception { exec(syncPersonJob.getJob()); return "personJob success"; }
總結(jié)
以上所述是小編給大家介紹的springbatch的封裝與使用實例詳解,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復(fù)大家的!