chaman gupta
chaman gupta

Reputation: 1

How to run spring batch job through CommandLineRunner, if i am using xml based configuration?

I am reading txt file and writing csv file with itemprocessor Below my xml

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:batch="http://www.springframework.org/schema/batch" `xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">

<!-- JobRepository and JobLauncher are configuration/setup classes -->
<bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean" />

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


<!-- ItemReader reads a complete line one by one from input file -->
<bean id="flatFileItemReader" class="org.springframework.batch.item.file.FlatFileItemReader"  scope="step">
    <property name="resource" value="classpath:Test.txt" />

    <property name="lineMapper">

        <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">

            <property name="fieldSetMapper">
                <!-- Mapper which maps each individual items in a record to properties in POJO -->
                <bean class="com.chaman.springbatch.ResultFieldSetMapper" />
            </property>

            <property name="lineTokenizer">
                <!-- A tokenizer class to be used when items in input record are separated by specific characters -->
                <bean class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
                    <!-- <property name="delimiter" value="|" /> -->
                </bean>
            </property>

        </bean>

    </property>

</bean>
<bean id="flatFileItemWriter" class="org.springframework.batch.item.file.FlatFileItemWriter" scope="step">`

`

    <property name="resource" value="file:csv/Result.csv" />

    <property name="lineAggregator">

        <!-- An Aggregator which converts an object into delimited list of strings -->
        <bean class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">

            <!-- <property name="delimiter" value="|" /> -->

            <property name="fieldExtractor">

                <!-- Extractor which returns the value of beans property through reflection -->
                <bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
                    <property name="names" value="number" />
                </bean>
            </property>
        </bean>
    </property>
</bean>

<!-- XML ItemWriter which writes the data in XML format -->
<!-- <bean id="xmlItemWriter" class="org.springframework.batch.item.xml.StaxEventItemWriter">

    <property name="resource" value="file:xml/examResult.xml" />

    <property name="rootTagName" value="UniversityExamResultList" />

    <property name="marshaller">

        <bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
            <property name="classesToBeBound">
                <list>
                    <value>com.websystique.springbatch.model.ExamResult</value>
                </list>
            </property>
        </bean>

    </property>

</bean> -->

<!-- Optional ItemProcessor to perform business logic/filtering on the input records -->
<bean id="itemProcessor" class="com.chaman.springbatch.ResultItemProcessor" />

<!-- Optional JobExecutionListener to perform business logic before and after the job -->
<bean id="jobListener" class="com.chaman.springbatch.ResultJobListener" />

<!-- Step will need a transaction manager -->
<bean id="transactionManager" class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />

<!-- Actual Job -->
<batch:job id="ResultJob">
    <batch:step id="step1">
        <batch:tasklet transaction-manager="transactionManager">
            <batch:chunk reader="flatFileItemReader" writer="flatFileItemWriter"    processor="itemProcessor" commit-interval="10" />
        </batch:tasklet>
    </batch:step>
    <batch:listeners>
        <batch:listener ref="jobListener" />
    </batch:listeners>
</batch:job>

Upvotes: 0

Views: 4346

Answers (2)

Mahmoud Ben Hassine
Mahmoud Ben Hassine

Reputation: 31590

How to run spring batch job through CommandLineRunner, if i am using xml based configuration?

This is explained in the documentation, see Running Jobs from the Command Line. Here is an example:

java CommandLineJobRunner myJob-configuration.xml myJob param=value

Upvotes: 1

Paul Wong
Paul Wong

Reputation: 46

package com.paul.testspringbatch;

import java.util.Date;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.boot.CommandLineRunner;

public class MyCommandLineRunner implements CommandLineRunner {
    
    private JobLauncher jobLauncher;

    private Job resultJob;

    @Override
    public void run(String... args) throws Exception {
        JobParameters jobParameters = new JobParametersBuilder().addDate("start-date", new Date()).toJobParameters();
        this.jobLauncher.run(resultJob, jobParameters);
    }
    
    public void setJobLauncher(JobLauncher jobLauncher) {
        this.jobLauncher = jobLauncher;
    }
    
    public void setResultJob(Job resultJob) {
        this.resultJob = resultJob;
    }


}

add the bean to your xml:

<bean id="jobLauncher"  class="com.paul.testspringbatch.MyCommandLineRunner">
    <property name="jobLauncher" ref="jobLauncher" />
    <property name="resultJob" ref="ResultJob" />
</bean>

when the application starts up, the job will be excuted.

Upvotes: 1

Related Questions