Spring Batch Sample
程序员文章站
2022-05-29 17:04:40
...
1. SampleDecider.java
package sample; import org.apache.log4j.Logger; import org.springframework.batch.core.JobExecution; import org.springframework.batch.core.JobParameters; import org.springframework.batch.core.StepExecution; import org.springframework.batch.core.job.flow.FlowExecutionStatus; import org.springframework.batch.core.job.flow.JobExecutionDecider; public class SampleDecider implements JobExecutionDecider { private Logger logger = Logger.getLogger(SampleDecider.class); private String DECIDER_FLAG = "DECIDER_FLAG"; @Override public FlowExecutionStatus decide(JobExecution jobExecution, StepExecution stepExecution) { logger.debug("execute the SampleDecider... "); JobParameters jobParameters =jobExecution.getJobParameters(); String flag = jobParameters.getString(DECIDER_FLAG); if(flag == null){ flag = ""; } logger.debug("the decider flag is "+flag); return new FlowExecutionStatus(flag); } }
2. SampleItemProcessor.java
package sample; import org.apache.log4j.Logger; import org.springframework.batch.item.ItemProcessor; public class SampleItemProcessor implements ItemProcessor<String, String> { private Logger logger = Logger.getLogger(SampleItemProcessor.class); @Override public String process(String str) throws Exception { logger.debug("execute the SampleItemProcessor..."); return str; } }
3. SampleItemReader.java
package sample; import java.util.ArrayList; import java.util.List; import org.apache.log4j.Logger; import org.springframework.batch.item.ItemReader; import org.springframework.batch.item.NonTransientResourceException; import org.springframework.batch.item.ParseException; import org.springframework.batch.item.UnexpectedInputException; public class SampleItemReader implements ItemReader<String> { private Logger logger = Logger.getLogger(SampleItemReader.class); private List<String> list = new ArrayList<String>(); public SampleItemReader(){ list.add("1"); list.add("2"); list.add("3"); } @Override public String read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException { logger.debug("execute the SampleItemWriter... "); if(list.size()>0){ return list.remove(0); } return null; } }
4. SampleItemWriter.java
package sample; import java.util.List; import org.apache.log4j.Logger; import org.springframework.batch.item.ItemWriter; public class SampleItemWriter implements ItemWriter<String> { private Logger logger = Logger.getLogger(SampleItemWriter.class); @Override public void write(List<? extends String> arg0) throws Exception { logger.debug("execute the SampleItemWriter... "); } }
5. DefaultConnection.java
package sample; import java.sql.Array; import java.sql.Blob; import java.sql.CallableStatement; import java.sql.Clob; import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.NClob; import java.sql.PreparedStatement; import java.sql.SQLClientInfoException; import java.sql.SQLException; import java.sql.SQLWarning; import java.sql.SQLXML; import java.sql.Savepoint; import java.sql.Statement; import java.sql.Struct; import java.util.Map; import java.util.Properties; public class DefaultConnection implements Connection { ..... }
6. SampleDataSource.java
package sample; import java.io.PrintWriter; import java.sql.Connection; import java.sql.SQLException; import javax.sql.DataSource; public class SampleDataSource implements DataSource{ .... }
7. sample-job.xml
<?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns="http://www.springframework.org/schema/batch" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.2.xsd"> <!-- configure the job repository --> <beans:bean id="jobRepository" class="org.springframework.batch.core.repository.support.SimpleJobRepository"> <beans:constructor-arg> <beans:bean class="org.springframework.batch.core.repository.dao.MapJobInstanceDao"/> </beans:constructor-arg> <beans:constructor-arg> <beans:bean class="org.springframework.batch.core.repository.dao.MapJobExecutionDao" /> </beans:constructor-arg> <beans:constructor-arg> <beans:bean class="org.springframework.batch.core.repository.dao.MapStepExecutionDao"/> </beans:constructor-arg> <beans:constructor-arg> <beans:bean class="org.springframework.batch.core.repository.dao.MapExecutionContextDao"/> </beans:constructor-arg> </beans:bean> <!-- configure the job launcher --> <beans:bean id="jobLauncher" class="launcher.DefaultSimpleJobLauncher"> <beans:property name="jobRepository" ref="jobRepository"/> </beans:bean> <job id="sampleJob" restartable="true"> <decision decider="sampleDecider" id="sampleDecision"> <next on="*" to="sampleStep"/> <end on="end"/> </decision> <step id="sampleStep"> <tasklet> <chunk reader="sampleItemReader" writer="sampleItemWriter" processor="sampleItemProcessor" commit-interval="100"> </chunk> </tasklet> </step> </job> <beans:bean id="sampleItemReader" class="sample.SampleItemReader"/> <beans:bean id="sampleItemWriter" class="sample.SampleItemWriter"/> <beans:bean id="sampleItemProcessor" class="sample.SampleItemProcessor"/> <beans:bean id="sampleDecider" class="sample.SampleDecider"/> <beans:bean id="dataSource" class="sample.SampleDataSource"/> <beans:bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <beans:property name="dataSource" ref="dataSource" /> </beans:bean> </beans:beans>
8. Main.java 测试
package sample; import org.springframework.batch.core.launch.support.CommandLineJobRunner; public class Main { public static void main(String[] args) throws Exception { String[] params = {"sample/sample-job.xml","sampleJob","DECIDER_FLAG=end"}; CommandLineJobRunner.main(params); } }