2012-04-03 48 views
0

我正在開發一個Spring 3的應用程序。我正在用Spring批量進行一些測試。這是我的工作定義:測試春天的批次什麼都不做

job.xml:

<bean id="fabio" class="com.firststepteam.handshake.jobs.PrintTasklet"> 
    <property name="message" value="Fabio"/> 
</bean> 

<bean id="taskletStep" abstract="true" 
    class="org.springframework.batch.core.step.tasklet.TaskletStep"> 
    <property name="jobRepository" ref="jobRepository"/> 
    <property name="transactionManager" ref="txManager"/> 
</bean> 

<bean id="simpleJob" class="org.springframework.batch.core.job.SimpleJob"> 
    <property name="name" value="simpleJob" /> 
    <property name="steps"> 
     <list> 
      <bean parent="taskletStep"> 
       <property name="tasklet" ref="fabio"/> 
      </bean> 
     </list> 
    </property> 
    <property name="jobRepository" ref="jobRepository"/> 
</bean> 

這是我如何配置批處理:

批次的context.xml:

<bean id="txManager" 
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
    <property name="dataSource" ref="dataSource" /> 
</bean> 

<batch:job-repository id="jobRepository" 
    data-source="dataSource" transaction-manager="txManager" 
    isolation-level-for-create="SERIALIZABLE" table-prefix="BATCH_" 
    max-varchar-length="1000" /> 

<bean id="jobLauncher" 
    class="org.springframework.batch.core.launch.support.SimpleJobLauncher"> 
    <property name="jobRepository" ref="jobRepository" /> 
</bean> 

我想要運行的tasklet:

public class PrintTasklet implements Tasklet{ 

private String message; 

public void setMessage(String message) { 
    this.message = message; 
} 

public ExitStatus execute() throws Exception { 
    System.out.println("Hello "+message); 
    return ExitStatus.COMPLETED; 
} 

這是如何我試圖運行作業:

mvn clean compile exec:java -Dexec.mainClass=org.springframework.batch.core.launch.support.CommandLineJobRunner -Dexec.args="job.xml simpleJob" 

什麼也沒有發生。沒有例外。作業執行以正確的方式保存在數據庫中。但是我的tasklet沒有運行。 我在這裏做錯了什麼?

我在Ubuntu 10.10上使用maven 2.2.1。 Spring批量版本是2.1.8

+0

請爲您的環境添加一些更多詳情,例如操作系統? Maven版本,特別是3.0版本的Spring批處理版本+ Tasklet支持Repeatstatus而不是Exitstatus,參見https://github.com/langmi/spring-batch-tutorials/blob/master/hello-world-java/src /main/java/de/langmi/spring/batch/tutorials/helloworld/HelloWorldTasklet.java – 2012-04-03 21:20:24

+0

已更新的詳細信息 – Fabio 2012-04-04 16:34:38

回答

0

問題解決了。正如邁克爾·蘭格建議,我只是做了這樣的:

@Override 
public RepeatStatus execute(StepContribution contribution, 
          ChunkContext chunkContext) throws Exception { 

    // why not using println? because it makes testing harder, *nix and 
    // windows think different about new line as in \n vs \r\n 
    System.out.print("Hello World! "+message); 

    return RepeatStatus.FINISHED; 
} 

和工程罰款。