【问题标题】:Test spring batch do nothing测试spring批处理什么都不做
【发布时间】:2012-04-03 15:06:27
【问题描述】:

我正在使用 Spring 3 开发应用程序。我正在使用 Spring Batch 进行一些测试。这是我的工作定义:

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>

这就是我配置批处理的方式:

批处理上下文.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"

什么都没有发生。没有例外。作业执行以正确的方式保存在数据库中。但是我的小任务没有运行。 我在这里做错了什么?

我在 Ubuntu 10.10 上使用 maven 2.2.1。 Spring Batch 版本为 2.1.8

【问题讨论】:

标签: java spring-mvc jobs spring-batch


【解决方案1】:

问题解决了。正如 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;
}

而且工作正常。

【讨论】:

    猜你喜欢
    • 2023-03-16
    • 1970-01-01
    • 2021-03-07
    • 2018-05-22
    • 1970-01-01
    • 1970-01-01
    • 2019-07-20
    • 2017-11-11
    • 2012-06-30
    相关资源
    最近更新 更多