Spring Batch的JobParameters

poj*_*guy 5 java spring spring-batch

我试图将作业参数注入自定义ItemReader.我已经回顾了有关该主题的所有StackOverflow说明(例如:如何从Spring Batch中的ItemReader访问作业参数?),我发现这是一个常见的难点,大部分都没有解决.我希望春天的大师(@Michael Minella任何人)会看到这一点,并有一些见解.

即使没有代码或配置更改,我已经确定了大约十分之一的运行参数可用.这是随机成功而非随机失败的情况,因此很难追查.

我使用调试器挖掘了spring代码,并确定当失败时,在注入发生时没有在Spring中注册名为jobParameters的bean.

我使用Spring 4.1.4和spring-batch 3.0.2以及spring-data-jpa 1.7.1和spring-data-commons 1.9.1,在java 8中运行.

Java类

@Component("sourceSelectionReader")
@Scope("step")
public class SourceSelectionReaderImpl  
implements ItemReader<MyThing> {
    private Map<String,Object> jobParameters;

// ... snip ...


    @Autowired
    @Lazy
    @Qualifier(value="#{jobParameters}")
    public void setJobParameters(Map<String, Object> jobParameters) {
        this.jobParameters = jobParameters;
    }
}
Run Code Online (Sandbox Code Playgroud)

工作启动参数:

launch-context.xml job1 jobid(long)=1
Run Code Online (Sandbox Code Playgroud)

launch-context.xml(减去绒毛):

<context:property-placeholder location="classpath:batch.properties" />

<context:component-scan base-package="com.maxis.maximo.ilm" />

<jdbc:initialize-database data-source="myDataSource"  enabled="false">
    <jdbc:script location="${batch.schema.script}" />
</jdbc:initialize-database>

<batch:job-repository id="jobRepository" 
    data-source="myDataSource"
    transaction-manager="transactionManager"
    isolation-level-for-create="DEFAULT"
    max-varchar-length="1000"/>

<import resource="classpath:/META-INF/spring/module-context.xml" />
Run Code Online (Sandbox Code Playgroud)

Module-context.xml(减去绒毛):

<description>Example job to get you started. It provides a skeleton for a typical batch application.</description>

<import resource="classpath:/META-INF/spring/hibernate-context.xml"/>
<import resource="classpath:/META-INF/spring/myapp-context.xml"/>

<context:component-scan base-package="com.me" />
<bean class="org.springframework.batch.core.scope.StepScope" />

<batch:job id="job1">
    <batch:step id="step0002"  >            
        <batch:tasklet transaction-manager="transactionManager" start-limit="100" >
            <batch:chunk reader="sourceSelectionReader" writer="selectedDataWriter" commit-interval="1" />
        </batch:tasklet>
    </batch:step>
</batch:job> 
Run Code Online (Sandbox Code Playgroud)

was*_*ren 5

让作业参数发挥作用的重要步骤是定义StepScopebean 并确保您的读者是一个@StepScope组件。

我会尝试以下操作:

首先确保定义了一个step-bean。使用 Java 配置进行设置非常好:

@Configuration
public class JobFrameworkConfig {  
    @Bean
    public static StepScope scope() {
        return new StepScope();
    }
    // jobRegistry, transactionManager etc...
}
Run Code Online (Sandbox Code Playgroud)

然后,确保您的 bean 通过使用 -annotation 来确定范围@StepScope(几乎与您的示例中一样)。注入一个@Value不是的@Lazy

@Component("sourceSelectionReader")
@StepScope // required, also works with @Scope("step")
public class SourceSelectionReaderImpl implements ItemReader<MyThing> {
    private final long myParam;

    // Not lazy, specified param name for the jobParameters
    @Autowired
    public SourceSelectionReaderImpl(@Value("#{jobParameters['myParam']}") final long myParam) {
        this.myParam = myParam;
    }

    // the rest of the reader...
}
Run Code Online (Sandbox Code Playgroud)