Search code examples
javaspring-mvcjobsspring-batch

Test spring batch do nothing


I'm developing an application with Spring 3. I'm doing some tests with spring batch. This is my job definition:

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>

This is how i configure batch:

batch-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>

The tasklet that i want to run:

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

This is how i'm trying to run the job:

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

Nothing happens. No exceptions. The job execution is saved in the database in the correct way. But my tasklet is not running. What am I doing wrong here?

I'm using maven 2.2.1 on Ubuntu 10.10. Spring Batch version is 2.1.8


Solution

  • Problem solved. As Michael Lange suggest, i just did like this:

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

    And works fine.