Spring Batch with Schedular

时间:2017-09-18 06:58:46

标签: spring-batch

enter image description here我是Spring Batch with Scheduler的新手。我的任务是从一个表中读取数据并将其写入另一个表。

我随机浏览博客和不同的教程。

我不知道是否有从数据库读取的直接方法并写入数据库。我采取这种方式 作业1:使用JdbcCursorItemReader从数据库读取数据,使用FlatFileItemWriter将数据写入txt文件。 作业2:使用FlatFileItemReader,multiResourceItemReader从txt文件中读取数据,并使用HibernateItemWriter将数据写入另一个表。

我正在使用调度程序,它将每20秒运行一次批处理。

在第一次运行的这种方法中它工作正常。对于第二次运行(20秒后),我正在更新数据库(基表)中的数据,但它没有将更新的数据写入文件和数据库。

这是我的配置& code`package com.cg.schedulers;

public class UserScheduler {
    @Autowired
    private JobLauncher launcher;
    @Autowired
    private Job userJob;
    @Autowired
    private Job userJob2;
    private JobExecution execution1,execution2;
    public void run() {
        try {
            execution1 = launcher.run(userJob, new JobParameters());
            execution2 = launcher.run(userJob2, new JobParameters());
            System.out.println("Execution status: " + execution1.getStatus());
            System.out.println("Execution status: " + execution2.getStatus());
        } catch (JobExecutionAlreadyRunningException e) {
            e.printStackTrace();
        } catch (JobRestartException e) {
            e.printStackTrace();
        } catch (JobInstanceAlreadyCompleteException e) {
            e.printStackTrace();
        } catch (JobParametersInvalidException e) {
            e.printStackTrace();
        }
    }
}

Xml配置

<import resource="spring-batch1.xml" />
<import resource="springbatch-database.xml" />
<context:annotation-config/>
<context:component-scan base-package="com.cg"/>

<!-- Reading data from -->
<bean id="itemReader"
    class="org.springframework.batch.item.database.JdbcCursorItemReader"
    scope="step">
    <property name="dataSource" ref="dataSource" />
    <property name="sql" value="select UserId, UserName, Password from USER" />
    <property name="rowMapper">
        <bean class="com.cg.mapper.UserRowMapper" />
    </property>
</bean>


<!-- ItemWriter writes a line into output flat file -->
<bean id="flatFileItemWriter" class="org.springframework.batch.item.file.FlatFileItemWriter"
    scope="step">

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

    <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="userId, username, password" />
                </bean>
            </property>
        </bean>
    </property>
</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="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.cg.mapper.UserFieldSetMapper" />
            </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="multiResourceItemReader"
    class="org.springframework.batch.item.file.MultiResourceItemReader">
    <property name="resources" value="classpath:csv/User.txt" />
    <property name="delegate" ref="flatFileItemReader" />
</bean>


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

<!-- Optional ItemProcessor to perform business logic/filtering on the input 
    records -->
<bean id="itemProcessor1" class="com.cg.support.UserItemProcessor" />

<bean id="itemProcessor2" class="com.cg.support.UserItemProcessor2" />

<!-- ItemWriter which writes data to database -->
<bean id="databaseItemWriter"
    class="org.springframework.batch.item.database.HibernateItemWriter">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>


<!-- Actual Job -->
<batch:job id="userJob">
    <batch:step id="step1">
        <batch:tasklet transaction-manager="transactionManager">
            <batch:chunk reader="itemReader" writer="flatFileItemWriter"
                processor="itemProcessor1" commit-interval="10" />
        </batch:tasklet>
    </batch:step>
    <batch:listeners>
        <batch:listener ref="jobListener" />
    </batch:listeners>
</batch:job>
<batch:job id="userJob2">
    <batch:step id="step2">
        <batch:tasklet transaction-manager="transactionManager">
            <batch:chunk reader="multiResourceItemReader" writer="databaseItemWriter"
                processor="itemProcessor2" commit-interval="10" />
        </batch:tasklet>
    </batch:step>
</batch:job>
<bean id="myScheduler" class="com.cg.schedulers.UserScheduler"/>
<task:scheduled-tasks>
    <task:scheduled ref="myScheduler" method="run" cron="*/20 * * * * *" />
</task:scheduled-tasks>

如果可能,请使用hibernate提供直接方法。

[在此输入图片说明] [2] 执行状态:已完成

谢谢, Vamshi。

0 个答案:

没有答案