如何访问Bean类中的StepExecutionContext

时间:2017-09-14 06:42:38

标签: java spring spring-batch

所以我需要能够在步骤中执行一个要在页脚中写出的值。

  

注意:专注于bean   “cashMantasFileUpdateJobFileReaderFieldMapperFooter”和类   用它。

我有以下xml:

{
    "SUCCESS": true,
    "MESSAGE": "Found 398 Results!",
    "NOFRESULTS": 3,
    "RESULTS": {
        "stationList": [
            {
                "stationCode": "ABN",
                "stationID": 3,
                "stationName": "ABLA"
            },
            {
                "stationCode": "ADLA",
                "stationID": 410,
                "stationName": "ADLA"
            },
            {
                "stationCode": "ANM",
                "stationID": 11,
                "stationName": "AHAMA"
            }]
    },
    "STATUSCODE": "2000"
}

以下课程:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:batch="http://www.springframework.org/schema/batch"
    xmlns:task="http://www.springframework.org/schema/task" xmlns:util="http://www.springframework.org/schema/util" xmlns:int="http://www.springframework.org/schema/integration" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xsi:schemaLocation="http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-3.0.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd 
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.1.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd
        http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-4.1.xsd">


    <!-- ###################################################################################### -->
    <!--  Cash Mantas File Update Batch Job                                                     -->
    <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
    <batch:job id="cashMantasFileUpdateJob" job-repository="jobRepository" restartable="false" parent="abstractJob">

        <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
        <!--  STEP 1 : Validate the inbound file                                                -->
        <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<!--         <batch:step id="cashMantasFileUpdateJob_step1_fileValidation" next="cashMantasFileUpdateJob_step2_processFile" parent="abstractStep"> -->
<!--             <batch:tasklet ref="cashMantasFileUpdateJobFileValidationTasklet" /> -->
<!--         </batch:step> -->

        <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
        <!--  STEP 2 : Read, process and write the outbound file                                -->
        <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
        <batch:step id="cashMantasFileUpdateJob_step2_processFile" parent="abstractStep">
            <batch:tasklet transaction-manager="transactionManager">
                <batch:chunk
                    commit-interval="#{jobExecutionContext['MantasDatabaseCommit']}"
                    reader="cashMantasFileUpdateJobFileReader" 
                    processor="cashMantasFileUpdateJobItemProcessor"
                    writer="cashMantasFileUpdateJobFileWriter">
                    <batch:streams>
                        <batch:stream ref="cashMantasFileUpdateJobFileReader" />
                        <batch:stream ref="cashMantasFileUpdateJobFileWriter" />
                    </batch:streams>
                </batch:chunk>
            </batch:tasklet>
            <batch:listeners merge="true">
                <batch:listener ref="cashMantasFileUpdateJobListener" />
            </batch:listeners>
        </batch:step>

        <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
        <!--  Batch Job Listener to handle before and after job tasks                           -->
        <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
        <batch:listeners merge="true">
            <batch:listener ref="cashMantasFileUpdateJobListener" />
        </batch:listeners>

    </batch:job>
    <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
    <!-- ###################################################################################### -->



    <!-- ###################################################################################### -->
    <!--  Write Data to the files                                                               -->
    <!-- ###################################################################################### -->
    <bean name="cashMantasFileUpdateJobFileReader" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step">
        <property name="resource" ref = "cashMantasFileUpdateJobFileReaderResource" />
        <property name="lineMapper" ref="cashMantasFileUpdateJobFileReaderLineMapper" />
    </bean>

    <bean name="cashMantasFileUpdateJobFileReaderResource" class="org.springframework.core.io.FileSystemResource" scope="step">
        <constructor-arg value="#{stepExecutionContext['MantasInFilename']}" />
    </bean>

    <bean id="cashMantasFileUpdateJobFileReaderLineMapper" class="org.springframework.batch.item.file.mapping.PatternMatchingCompositeLineMapper" scope="step">
        <property name="tokenizers">
            <map>
                <entry key="#{stepExecutionContext['MantasFooterBusinessDate']}*" value-ref="cashMantasFileUpdateJobFileReaderLineTokenizerFooter" />
                <entry key="*" value-ref="cashMantasFileUpdateJobFileReaderLineTokenizer" />
            </map>
        </property>
        <property name="fieldSetMappers">
            <map>
                <entry key="#{stepExecutionContext['MantasFooterBusinessDate']}*" value-ref="cashMantasFileUpdateJobFileReaderFieldMapperFooter" />
                <entry key="*" value-ref="cashMantasFileUpdateJobFileReaderFieldMapper" />
            </map>
        </property>

    </bean>

    <bean name="cashMantasFileUpdateJobFileReaderLineTokenizerFooter" class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer" scope="step">
        <property name="delimiter" value="~"/>
        <property name="names" value="#{stepExecutionContext['MantasFooterColumnNames']}" />
    </bean>
    <bean name="cashMantasFileUpdateJobFileReaderLineTokenizer" class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer" scope="step">
        <property name="delimiter" value="~"/>
        <property name="names" value="#{stepExecutionContext['MantasColumnNames']}" />
    </bean>

    <bean name="cashMantasFileUpdateJobFileReaderFieldMapper" class="com.tdameritrade.ctg.cash.mantas.batch.mapper.MantasFieldSetMapper" scope="step"/>
    <bean name="cashMantasFileUpdateJobFileReaderFieldMapperFooter" class="com.tdameritrade.ctg.cash.mantas.batch.mapper.MantasFooterFieldSetMapper" scope="step">
        <property name="stepContext" value="#{stepExecutionContext}"/>
    </bean>
    <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
    <!-- ###################################################################################### -->



    <!-- ###################################################################################### -->
    <!--  Item Processor for updating records                                                   -->
    <!-- ###################################################################################### -->
<!--     <bean id="compositeItemProcessor" class="org.springframework.batch.item.support.CompositeItemProcessor"> -->
<!--         <property name="delegates"> -->
<!--             <list> -->
<!--                 <ref bean="cashMantasFileUpdateJobItemProcessor" /> -->
<!--                 <ref bean="cashMantasFileUpdateJobFooterItemProcessor" /> -->
<!--             </list> -->
<!--         </property> -->
<!--     </bean> -->

    <bean id="cashMantasFileUpdateJobItemProcessor" class="com.tdameritrade.ctg.cash.mantas.batch.processor.MantasFileUpdateJobItemProcessor" scope="step">
        <property name="wireTrnsfInService" ref="wireTrnsfInService" />
        <property name="vndrService" ref="vndrService" />
        <property name="transactionService" ref="transactionService" />
    </bean>
    <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
    <!-- ###################################################################################### -->



    <!-- ###################################################################################### -->
    <!--  Write Data to the files                                                               -->
    <!-- ###################################################################################### -->
    <bean id="cashMantasFileUpdateJobFileWriter" class="org.springframework.batch.item.file.FlatFileItemWriter" scope="step">
        <property name="resource" ref="cashMantasFileUpdateJobFileWriterResource" />
        <property name="lineAggregator" ref="cashMantasFileUpdateJobLineAggregator" />
        <property name="footerCallback" ref="cashMantasFileUpdateJobFooterCallback" />
        <property name="headerCallback" ref="cashMantasFileUpdateJobHeaderCallback" />
    </bean>

    <bean name="cashMantasFileUpdateJobFileWriterResource" class="org.springframework.core.io.FileSystemResource" scope="step" autowire-candidate="false" >
        <constructor-arg value="#{stepExecutionContext['MantasOutFilename']}" />
    </bean>

    <bean name="cashMantasFileUpdateJobLineAggregator" class="org.springframework.batch.item.file.transform.DelimitedLineAggregator" scope="step">
        <property name="delimiter" value="~"/>
        <property name="fieldExtractor" ref="cashMantasFileUpdateJobFieldExtractor" />
    </bean>

    <bean name="cashMantasFileUpdateJobFieldExtractor" class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor" scope="step">
        <property name="names" value="#{stepExecutionContext['MantasColumnNames']}"/>
    </bean>
    <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
    <!-- ###################################################################################### -->




<!--     ###################################################################################### -->
<!--      Validation                                                                            -->
<!--     ###################################################################################### -->
<!--     <bean id="cashMantasFileUpdateJobFileValidationTasklet" scope="step" class="com.tdameritrade.commons.batch.file.validation.tasklet.FileValidationTasklet"> -->
<!--         <property name="fileValidator" ref="cashMantasFileUpdateJobFileValidator" /> -->
<!--         <property name="batchExceptionManager" ref="batchExceptionManager" /> -->
<!--     </bean> -->

<!--     <bean id="cashMantasFileUpdateJobFileValidator" class="com.tdameritrade.commons.batch.file.validation.impl.FileValidatorImpl" scope="step"> -->
<!--         <property name="source" value="#{stepExecutionContext['MantasInFilename']}" /> -->
<!--         <property name="validateDataRecordCount" value="false" /> -->
<!--         <property name="footerLines" value="1" /> -->
<!--         <property name="headerLines" value="1" /> -->
<!--         <property name="headerValidator"> -->
<!--             <bean class="com.tdameritrade.commons.batch.file.validation.impl.LineValidator"> -->
<!--                 <property name="lineProcessors"> -->
<!--                     <map> -->
<!--                         <entry key="1"> -->
<!--                             <bean class="com.tdameritrade.ctg.cash.mantas.batch.processor.MantasFileHeaderProcessor"> -->
<!--                                 <property name="headerCount" value="1" /> -->
<!--                             </bean> -->
<!--                         </entry> -->
<!--                     </map> -->
<!--                 </property> -->
<!--             </bean> -->
<!--         </property> -->
<!--     </bean> -->
<!--     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<!--     ###################################################################################### -->



    <!-- ###################################################################################### -->
    <!--  Listeners                                                                             -->
    <!-- ###################################################################################### -->
    <bean id="cashMantasFileUpdateJobListener" class="com.tdameritrade.ctg.cash.mantas.batch.listener.MantasFileUpdateJobListener">
    </bean>
    <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
    <!-- ###################################################################################### -->



    <!-- ###################################################################################### -->
    <!-- Callbacks -->
    <!-- ###################################################################################### -->
    <bean id="cashMantasFileUpdateJobHeaderCallback" scope="step" class="com.tdameritrade.ctg.cash.mantas.batch.callback.MantasHeaderCallback">
        <property name="columnNames" value="#{jobExecutionContext['MantasColumnNames']}"></property>
    </bean>
    <bean id="cashMantasFileUpdateJobFooterCallback" scope="step" class="com.tdameritrade.ctg.cash.mantas.batch.callback.MantasFooterCallback">
        <property name="footer" value="#{stepExecutionContext['MantasFooterLine']}" />
    </bean>
    <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
    <!-- ###################################################################################### -->


</beans>

方法:

package com.tdameritrade.ctg.cash.mantas.batch.mapper;

import org.springframework.batch.core.scope.context.StepContext;
import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.validation.BindException;
import com.tdameritrade.ctg.cash.CashConstants;
import com.tdameritrade.ctg.cash.model.mantas.MantasFooterModel;
import com.tdameritrade.ctg.cash.util.ExecutionUtil;
import com.tdameritrade.ctg.cash.util.GeneralUtil;
import com.tdameritrade.ctg.cash.util.LoggingUtil;

public class MantasFooterFieldSetMapper implements FieldSetMapper<MantasFooterModel> {

    // Log manager
    private static LoggingUtil logMgr =
            LoggingUtil.getLogger(MantasFooterFieldSetMapper.class, CashConstants.TDA_CASH_APPLICATION_ID);

    private StepContext stepContext = null;

    @Override
    public MantasFooterModel mapFieldSet(final FieldSet set) throws BindException {
        MantasFooterModel footer = new MantasFooterModel();
        footer.setDate(GeneralUtil.checkString(set.readString(MantasFooterModel.FOOTER_DATE)));
        footer.setCount(GeneralUtil.checkString(set.readString(MantasFooterModel.FOOTER_COUNT)));
        ExecutionUtil.putInStepExecutionContext(stepContext.getStepExecution(),
                                                CashConstants.MANTAS_FOOTER_LINE,
                                                footer.createOutput());
        logMgr.info("MANTAS footer data is " + footer.toString() + " : " + set.toString());
        return footer;
    }

    /**
     * @param stepContext
     *            the stepContext to set
     */
    public void setStepContext(final StepContext stepContext) {
        this.stepContext = stepContext;
    }

}

我的问题是我的堆栈跟踪中出现以下错误。

/**
 * This method will is for when you desire to put an integer into the job execution but need to ensure that we have
 * a job execution and an execution context.
 *
 * @param stepExecution
 *            The Job Execution to add to
 *
 * @param key
 *            The key to use to place in the execution context
 *
 * @param value
 *            The value to put in the context
 *
 * @return
 *         The updated Job Execution
 *
 */
public static StepExecution putInStepExecutionContext(final StepExecution stepExecution, final String key, final String value) {

    // Check that we have a job execution
    if (stepExecution != null) {

        // Check that we have an Execution Context
        if (stepExecution.getExecutionContext() != null) {

            // Put the value in the job execution
            stepExecution.getExecutionContext().putString(key, value);

        }

    }

    // Return the job execution
    return stepExecution;
}

我需要的是能够将页脚的值放入页脚。我在原始文件页脚的字段集映射器中获取值,并希望将其存储在StepExecutionContext中以供写入器页脚回调读取。我最初尝试添加StepExecutionListener并在设置类变量的值后将其放在afterStep中。但是在afterStep中有一个断点,它从未被执行过。所以我发现你可以传入StepExecutionContext,但我一定做错了。

对所有建议开放。

0 个答案:

没有答案