首页 文章

测试 spring 批量什么都不做

提问于
浏览
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 Batch版本是2.1.8

1 回答

  • 0

    问题解决了 . 正如Michael Lange建议的那样,我只是这样做:

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

    并且工作正常 .

相关问题