本範例實作說明:
本篇主要使用 Spring Batch,做一個基本測試專案,
此Batch 為 讀取csv檔 轉換為 csv檔 的功能。
使用到 Spring Batch 的 ItemReader、ItemProcessor和ItemWriter 。
一、範例開發準備工具
作業系統:Windows 7
開發工具:Eclipse Java EE IDE for Web Developers Juno Service Release2
JAVA JDK : JDK1.7.0_60
MS_SQL 2012
其它相關:Maven 4.0
Springframework 4.1.3.RELEASE
Springframework batch 3.0.2.RELEASE
Junit-test 4.11
二、新增Maven範例專案(請參考 Spring Batch Hello World Example 專案)
三、相關resoures
建立resoures 路徑
src/main/reresoures
建立設定檔存放路徑
src/main/reresoures/config
建立Spring batch 設定檔存放路徑
src/main/reresoures/job
建立匯入csv測試資料檔存放路徑
src/test/reresoures/csv
圖1 建立相關 resoures
四、建立資料庫資訊檔 job-database.xml(請參考 Spring Batch Hello World Example 專案)
五、建立Spring batch 設定檔存放路徑
src/main/reresoures/config/job-context.xml
src/main/reresoures/config/job/spring-batch-context.xml
spring-batch-context.xml內容:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"> <job id="inMemoryJob" xmlns="http://www.springframework.org/schema/batch"> <step id="inMemoryStep"> <tasklet> <chunk reader="itemReader" processor="itemProcessor" writer="itemWriter" commit-interval="5"> </chunk> </tasklet> </step> </job> <bean id="itemReader" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step"> <property name="resource" value="#{jobParameters['input.file']}" /> <property name="lineMapper"> <!-- 直接拋回一筆資料 --> <bean class="org.springframework.batch.item.file.mapping.PassThroughLineMapper" /> </property> <property name="strict" value="true" /> </bean> <bean id="itemProcessor" class="com.sample.SampleItemProcessor" /> <bean id="itemWriter" class="org.springframework.batch.item.file.FlatFileItemWriter" scope="step"> <property name="resource" value="#{jobParameters['output.file']}" /> <!-- 取得一筆資料,回傳 String --> <property name="lineAggregator"> <!-- 取得資料後直接toString()拋出 --> <bean class="org.springframework.batch.item.file.transform.PassThroughLineAggregator"/> </property> </bean> </beans>
job-context.xml內容:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="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-4.0.xsd"> <bean id="jobRepository" class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="transactionManager" ref="transactionManager" /> <property name="databaseType" value="sqlserver" /> </bean> <bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher"> <property name="jobRepository" ref="jobRepository" /> </bean> </beans>六、建立匯入csv測試資料檔
資料存放位置: src/test/reresoures/csv/input.csv
1001,"213,12",980,levin , 2013/1/02
1002,"220,20",1080,tom 1 , 2013/2/4
1003,"252,19",2200,tom 2 , 2013/11/10
1003,"248,19",1500,tom 3 , 2013/10/20
1003,"552,18",2800,tom 4 , 2013/6/15
1003,"323,78",1900,tom 5 , 2013/5/12
七、建立讀取資料項目程式 SampleItemProcessor.java
檔案路徑: src/main/java/com/sample/SampleItemProcessor.java
package com.sample; import org.springframework.batch.item.ItemProcessor; public class SampleItemProcessor implements ItemProcessor{ public String process(String item) throws Exception { System.out.println(item); return item; } }
檔案路徑: src/test/java/com/sample/ApplicationTest.java八、執行job ApplicationTest.java
package com.sample; import static org.junit.Assert.assertEquals; import java.util.HashMap; import java.util.Map; import org.junit.Test; import org.springframework.batch.core.BatchStatus; import org.springframework.batch.core.Job; import org.springframework.batch.core.JobExecution; import org.springframework.batch.core.JobParameter; import org.springframework.batch.core.JobParameters; import org.springframework.batch.core.launch.JobLauncher; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class ApplicationTest { @Test @SuppressWarnings("resource") public void launchJob() throws Exception { ApplicationContext context = new ClassPathXmlApplicationContext( "job/spring-batch-context.xml", "config/job-context.xml", "config/job-database.xml"); Job job = (Job) context.getBean("inMemoryJob"); JobLauncher launcher = (JobLauncher) context.getBean("jobLauncher"); MapjobParametersMap = new HashMap (); jobParametersMap.put("input.file", new JobParameter("file:src/test/resources/csv/input.csv")); jobParametersMap.put("output.file", new JobParameter("file:target/test-outputs/output.csv")); JobExecution jobExecution = launcher.run(job, new JobParameters(jobParametersMap)); assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus()); System.out.println("執行結果 = " + jobExecution.getStatus()); System.out.println("執行程式結束."); } }
九、執行結果
圖一.
圖二. 產生檔案及內容
其它文章
沒有留言:
張貼留言