Spring Batch将数据从step1保存到step2

时间:2016-04-25 17:23:47

标签: spring-batch

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

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 :(得分:0)

现在更新了问题的详细信息......

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

  1. 从数据库中获取一些FTP登录信息
  2. 下载一些CSV文件
  3. 验证文件(文件级?还是仅记录级验证?)
  4. 过滤掉一些垃圾记录
  5. 确定一些“行动”
  6. 执行一些“行动”
  7. 实现这一目标的最佳方法(在我看来)是创建以下步骤:

    1. 步骤1:一个简单的Tasklet,用于查询数据库中的FTP登录信息,然后将CSV文件下载到本地文件夹
    2. 步骤2:分区步骤,在文件夹中为每个CSV创建一个分区
      1. 每个分区都有一个读取记录的阅读器(FlatFileItemReader)。
      2. 如果记录是垃圾,记录将转到ItemProcessor,返回null
      3. 有效项目将写入某个数据库临时表以便采取进一步行动
      4. 您可以选择使用ClassifierItemWriter来执行垃圾记录
    3. 步骤3:下一步读取登台表中的有效数据并执行“操作”
    4. 第4步:对垃圾记录采取行动可能是另一个步骤