首页 文章

Spring Batch将数据从步骤1保存到步骤2

提问于
浏览
0

我已经彻底搜索了Spring文档和支持站点,但是没有找到并回答这个问题;如果我想在ExecutionContext中访问和存储一些值,我是否必须编写实现ItemStream的自定义databaseItemReader和ItemWriter,或者我可以使用“开箱即用”的读者和编写器并在 Spring 天编辑bean -batch-context.xml文件这样做?任何代码示例将不胜感激 . 谢谢!

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

<import resource="classpath:context-datasource.xml" />

<!-- 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 which reads from database and returns the row mapped by 
    rowMapper -->
<bean id="databaseItemReader"
    class="org.springframework.batch.item.database.JdbcCursorItemReader">

    <property name="dataSource" ref="dataSource" />

    <property name="sql"
        value="SELECT PartnerID, ftpUserName, ftpPassword, ftpPath, jobRunTime, jobFrequency FROM tblRosterJobParams" />

    <property name="rowMapper">
        <bean class="com.explorelearning.batch.ParamResultRowMapper" />
    </property>

</bean>


<!-- This was supposed to change to a SavingItemWriter that persists these values to the Step ExecutionContext -->
<bean id="flatFileItemWriter" class="org.springframework.batch.item.file.FlatFileItemWriter"
    scope="step">

    <property name="resource" value="file:csv/ParamResult.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="PartnerID" />
                </bean>
            </property>
        </bean>
    </property>
</bean>

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

<!-- Optional StepExecutionListener to perform business logic before and after the job -->
<bean id="stepExecutionListener" class="com.explorelearning.batch.ParamResultStepExecutionListener" />

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

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

<!-- Actual Job -->
<batch:job-repository id="jobRepository"  data-source="dataSource" table-prefix="BATCH_"
    transaction-manager="transactionManager" isolation-level-for-create="SERIALIZABLE" />
<batch:job id="RosterBatchJob" job-repository="jobRepository">
 <batch:step id="readParams" >
    <batch:tasklet transaction-manager="transactionManager" allow-start-if-complete="true">
        <batch:chunk reader="databaseItemReader"  writer="flatFileItemWriter"
            processor="itemProcessor" commit-interval="10" />
    </batch:tasklet>
 </batch:step>
 <!--<batch:step id="grabCSVs" next="validateCSVs">

 </batch:step>
 <batch:step id="validateCSVs" next="filterRecords">

 </step>
 <batch:step id="filterRecords" next="determineActions">

 </batch:step>
  <batch:step id="determineActions" next="executeActions">

 </batch:step>
  <batch:step id="executeActions" next="">

 </batch:step>  -->

</batch:job>

1 回答

  • 0

    Updated now that the question has more detail...

    好的,我正在阅读你的上下文文件,你需要:

    • 从数据库中获取一些FTP登录信息

    • 下载一些CSV文件

    • 验证文件(文件级?还是仅记录级验证?)

    • 过滤掉一些垃圾记录

    • 确定一些"actions"

    • 执行一些"actions"

    (在我看来)实现这一目标的最佳方法是创建以下步骤:

    • 步骤1:一个简单的 Tasklet ,它在数据库中查询FTP登录信息,然后将CSV文件下载到本地文件夹

    • 步骤2:分区步骤,在文件夹中为每个CSV创建一个分区

    • 每个分区都有一个读取记录的阅读器( FlatFileItemReader ) .

    • 记录将转到 ItemProcessor ,如果记录是垃圾,则返回 null .

    • 有效项目将写入某个数据库临时表以进一步操作

    • 您可以选择使用 ClassifierItemWriter 来执行垃圾记录

    • 步骤3:下一步读取登台表中的有效数据并执行"Actions"

    • 步骤4:对垃圾记录执行某些操作可能是另一个步骤

相关问题