java - 为什么 SpringBatch MultiResourcePartitioner 无法识别 jobParameters?更重要的是,该怎么办?

标签 java batch-processing spring-batch applicationcontext

这是一个非常简单的设置。我有一个可能包含 5m 条记录的文件,我需要读取该文件并进行一些处理,然后将其发送到数据库。处理和写作的机制并不重要。我需要能够将路径和文件名 [/opt/etc/app/partner/input_file.csv ] 作为参数传递到进程中。这很简单,将其添加到 JobParameters 并将其提供给 JobLauncher。

JobParametersBuilder jpBuilder = new JobParametersBuilder() ;
jpBuilder.addString("filePath", "/opt/etc/app/partner/input_file.csv") ;
jobLauncher.run(job, jpBuilder.toJobParameters() ;

完成,现在让上下文意识到它。引用 jobParameters 也是一个简单的问题。

<?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:batch="http://www.springframework.org/schema/batch"
    xsi:schemaLocation="
    http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.1.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <description>PoC to demonstrate variable row content handling</description>

    <batch:job id="poc" job-repository="jobRepository" incrementer="runIdIncrementer" restartable="true">
        <batch:step id="pocReadWriteStep">
            <batch:partition step="step" partitioner="partitioner">
                <batch:handler task-executor="taskExecutor"/>
            </batch:partition>
        </batch:step>
    </batch:job>

    <batch:step id="step">
        <batch:tasklet task-executor="taskExecutor" throttle-limit="20" transaction-manager="transactionManager" allow-start-if-complete="true">
            <batch:transaction-attributes isolation="READ_UNCOMMITTED"/>
            <batch:chunk
                reader="reader"
                processor="processor"
                writer="writer"
                commit-interval="20">
            </batch:chunk>
        </batch:tasklet>
    </batch:step>

    <bean id="reader" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step">
        <property name="resource" value="file:#{jobParameters['filePath']}"/>
        <property name="lineMapper">
            <bean class="org.springframework.batch.item.file.mapping.PatternMatchingCompositeLineMapper">
                <property name="tokenizers">
                    <map>
                        <entry key="*" value-ref="lineTokenizer"/>
                    </map>
                </property>
                <property name="fieldSetMappers">
                    <map>
                        <entry key="*" value-ref="fieldSetMapper"/>
                    </map>
                </property>
            </bean>
        </property>
    </bean>

    <bean id="lineTokenizer" class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer"/>

    <bean id="fieldSetMapper" class="org.springframework.batch.item.file.mapping.PassThroughFieldSetMapper"/>

    <bean id="processor" class="com.verifi.springbatchpoc.PocProcessor"/>

    <bean id="writer" class="com.verifi.springbatchpoc.PocWriter"/>

    <bean id="runIdIncrementer" class="org.springframework.batch.core.launch.support.RunIdIncrementer"/>

    <bean id="partitioner" class="org.springframework.batch.core.partition.support.MultiResourcePartitioner">
        <property name="resources" value="file:#{jobParameters['filePath']}"/>
    </bean>

    <bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
        <property name="corePoolSize" value="20"/>
    </bean>

</beans>

除了在 MultiResourcePartitioner 中它似乎无法识别对 jobParameters 的引用。我可以对路径和文件名进行硬编码,这很高兴,但这并没有携带此实现的邮件。

因为我确信有人会问,所以这是错误的堆栈跟踪(无论如何都是错误的一部分)。

    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1086)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
    ... 46 more
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 0): Field or property 'jobParameters' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext'
    at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:207)
    at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:71)
    at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:52)
    at org.springframework.expression.spel.ast.SpelNodeImpl.getTypedValue(SpelNodeImpl.java:102)
    at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:97)
    at org.springframework.expression.common.CompositeStringExpression.getValue(CompositeStringExpression.java:82)
    at org.springframework.expression.common.CompositeStringExpression.getValue(CompositeStringExpression.java:1)
    at org.springframework.context.expression.StandardBeanExpressionResolver.evaluate(StandardBeanExpressionResolver.java:138)
    ... 52 more

想法、评论、建议?

最佳答案

我认为你应该添加scope="step",例如:

 <bean id="partitioner" class="org.springframework.batch.core.partition.support.MultiResourcePartitioner"
scope="step">
        <property name="resources" value="file:#{jobParameters['filePath']}"/>
    </bean>

关于java - 为什么 SpringBatch MultiResourcePartitioner 无法识别 jobParameters?更重要的是,该怎么办?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14628612/

相关文章:

java/安卓 : editing an object after clone/duplicate also edits the original

asp.net-mvc - 设计长期运行的 ASP.net MVC Web 请求

python - 批处理运行时显示或留下 python 错误消息

java - 在 Spark 中执行两次 groupbykey 的最佳实践?

java - Spring 批处理 : JobExecution from PreparedStatementSetter

java - 如何配置 RabbitMQ 客户端以使用 SSL?

java - JDOQL 通过 "contains"请求获取结果时出现问题

java - 无法使用netbeans 7.4连接mysql

数据列表中每个项目的 Spring Batch-Repeat 步骤

java - JSR 352 分区 block 处理