Spring Batch 之 Sample(Hello World)(三(san))
通過前(qian)面兩篇(pian)關(guan)于Spring Batch文章(zhang)的(de)介紹,大家應該(gai)已經對(dui)Spring Batch有個初步的(de)概念了。這篇(pian)文章(zhang),將通過一個”Hello World!”實例(li),和(he)大家一起探討關(guan)于Spring Batch的(de)一些基本配置(zhi)和(he)實現。使(shi)大家從開發(fa)的(de)角(jiao)度(du)對(dui)Spring Batch有一個真切的(de)體會。
說明:1,本實例使用(yong)的是spring-batch 2.1.8
2,本(ben)實(shi)例沒有像(xiang)前(qian)面講(jiang)的(de)那樣配置ItemReader、ItemProcessor和(he)ItemWriter,而是(shi)之間在Step中調用Tasklet,由Tasklet完成”Hello World!”的(de)輸出。
工程結構如下圖:

JobLaunch.java類用(yong)來啟動Bath,writeTasklet.java用(yong)來完成輸出工作。application.xml用(yong)來配(pei)置一些(xie)Spring信息,batch.xml配(pei)置Job信息。
application.xml文件(jian)配置如下(xia):
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="//www.springframework.org/schema/beans"
xmlns:xsi="//www.w3.org/2001/XMLSchema-instance" xmlns:p="//www.springframework.org/schema/p"
xmlns:tx="//www.springframework.org/schema/tx" xmlns:aop="//www.springframework.org/schema/aop"
xmlns:context="//www.springframework.org/schema/context"
xsi:schemaLocation="//www.springframework.org/schema/beans
//www.springframework.org/schema/beans/spring-beans-3.0.xsd
//www.springframework.org/schema/tx
//www.springframework.org/schema/tx/spring-tx-3.0.xsd
//www.springframework.org/schema/aop
//www.springframework.org/schema/aop/spring-aop-3.0.xsd
//www.springframework.org/schema/context
//www.springframework.org/schema/context/spring-context-2.5.xsd"
default-autowire="byName">
<bean id="jobLauncher"class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository"/>
</bean>
<bean id="jobRepository"class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
</bean>
<bean id="transactionManager"
class="org.springframework.batch.support.transaction.ResourcelessTransactionManager"/>
</beans>
jobLauncher負責(ze)(ze)batch的(de)啟(qi)動工作,jobRepository負責(ze)(ze)job的(de)整個(ge)運(yun)行過程中的(de)CRUD操作,transactionManager負責(ze)(ze)事務的(de)管理(li)操作。
batch.xml文件配置如(ru)下:
<?xml version="1.0" encoding="UTF-8"?>
<bean:beans xmlns="//www.springframework.org/schema/batch"
xmlns:bean="//www.springframework.org/schema/beans" xmlns:xsi="//www.w3.org/2001/XMLSchema-instance"
xmlns:p="//www.springframework.org/schema/p" xmlns:tx="//www.springframework.org/schema/tx"
xmlns:aop="//www.springframework.org/schema/aop" xmlns:context="//www.springframework.org/schema/context"
xsi:schemaLocation="//www.springframework.org/schema/beans
//www.springframework.org/schema/beans/spring-beans-3.0.xsd
//www.springframework.org/schema/tx
//www.springframework.org/schema/tx/spring-tx-3.0.xsd
//www.springframework.org/schema/aop
//www.springframework.org/schema/aop/spring-aop-3.0.xsd
//www.springframework.org/schema/context
//www.springframework.org/schema/context/spring-context-2.5.xsd
//www.springframework.org/schema/batch
//www.springframework.org/schema/batch/spring-batch-2.1.xsd">
<bean:import resource="applicationContext.xml"/>
<job id="helloWorldJob">
<step id="step_hello" next="step_world">
<tasklet ref="hello" transaction-manager="transactionManager"></tasklet>
</step>
<step id="step_world">
<tasklet ref="world" transaction-manager="transactionManager"></tasklet>
</step>
</job>
<bean:bean id="hello" class="com.wanggc.springbatch.sample.helloworld.writeTasklet">
<bean:property name="message" value="Hello "></bean:property>
</bean:bean>
<bean:bean id="world" class="com.wanggc.springbatch.sample.helloworld.writeTasklet">
<bean:property name="message" value=" World!"></bean:property>
</bean:bean>
</bean:beans>
配置了一個ID為helloWorldJob的job,此job有(you)兩個Step : step_hello和step_world,前者(zhe)負責輸出(chu)“Hello ”,后(hou)者(zhe)負責輸出(chu)“World!”,當第(di)一個Step完成以(yi)后(hou),執行第(di)二個Step。
writeTasklet類的代碼如(ru)下(xia):
public class writeTasklet implements Tasklet {
/** Message */
private String message;
/**
* @param message
* the message to set
*/
public void setMessage(String message) {
this.message = message;
}
@Override
public RepeatStatus execute(StepContribution arg0, ChunkContext arg1)
throws Exception {
System.out.println(message);
return RepeatStatus.FINISHED;
}
}
此類中(zhong)定義了一個message屬性,通過batch.xml的(de)“hello”和“world” Bean為其注入值。 execute方法,是(shi)由Tasklet接口繼承(cheng)而來的(de),是(shi)Tasklet實現業務邏輯的(de)地(di)方。此實例中(zhong)只(zhi)是(shi)簡單的(de)輸出Message信(xin)息后,直接返回。
啟動類JobLaunch類的代碼如下:
public class JobLaunch {
/**
* @param args
*/
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"batch.xml");
JobLauncher launcher = (JobLauncher) context.getBean("jobLauncher");
Job job = (Job) context.getBean("helloWorldJob");
try {
/* 運(yun)行Job */
JobExecution result = launcher.run(job, new JobParameters());
/* 處理結束,控制臺打(da)印處理結果 */
System.out.println(result.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
本例通過Spring配置的方式(shi)取得JobLauncher和Job對(dui)象,然后由JobLauncher的run方法啟動job,參數(shu)JobParameters是(shi)標志job的一些參數(shu),處理結束后,控制臺(tai)輸出處理結果。
上面就(jiu)是通過SpringBatch運行一個"Hello World”程序所需(xu)要的(de)基本(ben)配(pei)置(zhi)。由于其(qi)優(you)(you)勢是處(chu)理大(da)批(pi)量的(de)數據,所以僅僅為(wei)了輸出"Hello World"而編(bian)寫(xie)這么多代碼和配(pei)置(zhi)文件,確實顯得有些笨拙,也體現不出其(qi)優(you)(you)越(yue)性。
下次,將通過(guo)讀取一(yi)個CSV文件(jian),經(jing)過(guo)簡單(dan)的處理,再寫入(ru)另外一(yi)個CSV文件(jian)的實例(li),與大家共同探討SpringBatch的應用。
歡迎轉載,請注明出處!
感謝您的閱讀,請關注后續博客!
共享視頻教程請訪問:
