来自Spring Batch的JobParameters [英] JobParameters from Spring Batch

查看:36
本文介绍了来自Spring Batch的JobParameters的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将作业参数注入自定义ItemReader。我已经回顾了关于这个主题的所有StackOverflow注释(示例:How to get access to job parameters from ItemReader, in Spring Batch?),我发现这是一个常见的痛点,大部分都没有解决。我希望春天的大师(@Michael Minella Anyone)能看到这一点,并有一些洞察力。

我已经确定作业参数大约每10次运行中就有一次可用,即使没有代码或配置更改。这是一个随机成功的案例,而不是随机失败的案例,因此很难追踪。

我使用调试器深入研究了Spring代码,确定当此操作失败时,在发生注入时没有名为jobParameters的bean在Spring中注册。

我使用的是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;
    }
}

作业启动参数:

launch-context.xml job1 jobid(long)=1

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" />

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> 

JOB

让推荐答案参数正常工作的重要步骤是定义StepScopeBean,并确保您的读取器是@StepScope组件。

我将尝试以下操作:

首先,确保定义了一个步骤bean。这很适合使用Java配置进行设置:

@Configuration
public class JobFrameworkConfig {  
    @Bean
    public static StepScope scope() {
        return new StepScope();
    }
    // jobRegistry, transactionManager etc...
}
然后,确保您的bean通过使用@StepScope注释(与您的示例几乎相同)来确定步骤范围。插入不是@Lazy@Value

@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...
}

这篇关于来自Spring Batch的JobParameters的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆