Spring Batch 之 Sample(Hello World)(三)

轉自:http://www.cnblogs.com/gulvzhe/archive/2011/10/31/2230655.html

通過前面兩篇關於Spring Batch文章的介紹,大家應該已經對Spring Batch有個初步的概念了。這篇文章,將通過一個”Hello World!”實例,和大家一起探討關於Spring Batch的一些基本配置和實現。使大家從開發的角度對Spring Batch有一個真切的體會。

      說明:1,本實例使用的是spring-batch 2.1.8

             2,本實例沒有像前面講的那樣配置ItemReader、ItemProcessor和ItemWriter,而是之間在Step中調用Tasklet,由Tasklet完成”Hello World!”的輸出。

      工程結構如下圖:

                    

      JobLaunch.java類用來啓動Bath,writeTasklet.java用來完成輸出工作。application.xml用來配置一些Spring信息,batch.xml配置Job信息。

application.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" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
http://www.springframework.org/schema/tx  
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
http://www.springframework.org/schema/aop  
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
http://www.springframework.org/schema/context  
http://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負責batch的啓動工作,jobRepository負責job的整個運行過程中的CRUD操作,transactionManager負責事務的管理操作。

      batch.xml文件配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<bean:beans xmlns="http://www.springframework.org/schema/batch"
    xmlns:bean="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd  
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/batch 
http://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有兩個Step : step_hello和step_world,前者負責輸出“Hello ”,後者負責輸出“World!”,當第一個Step完成以後,執行第二個Step。 

      writeTasklet類的代碼如下:

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;
    }

}


      此類中定義了一個message屬性,通過batch.xml的“hello”和“world” Bean爲其注入值。 execute方法,是由Tasklet接口繼承而來的,是Tasklet實現業務邏輯的地方。此實例中只是簡單的輸出Message信息後,直接返回。

      啓動類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 {
            /* 運行Job */
            JobExecution result = launcher.run(job, new JobParameters());
            /* 處理結束,控制檯打印處理結果 */
            System.out.println(result.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


      本例通過Spring配置的方式取得JobLauncher和Job對象,然後由JobLauncher的run方法啓動job,參數JobParameters是標誌job的一些參數,處理結束後,控制檯輸出處理結果。

      上面就是通過SpringBatch運行一個"Hello World”程序所需要的基本配置。由於其優勢是處理大批量的數據,所以僅僅爲了輸出"Hello World"而編寫這麼多代碼和配置文件,確實顯得有些笨拙,也體現不出其優越性。

      下次,將通過讀取一個CSV文件,經過簡單的處理,再寫入另外一個CSV文件的實例,與大家共同探討SpringBatch的應用。

發佈了15 篇原創文章 · 獲贊 1 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章