gpt4 book ai didi

java - 来自 Spring Batch 的 JobParameters

转载 作者:搜寻专家 更新时间:2023-10-31 20:33:56 24 4
gpt4 key购买 nike

我正在尝试将作业参数注入(inject)自定义 ItemReader。我已经查看了关于该主题的所有 StackOverflow 注释(示例:How to get access to job parameters from ItemReader, in Spring Batch?),我发现这是一个常见的痛点,而且大部分都没有得到解决。我希望 spring 大师(@Michael Minella 任何人)会看到这一点并有一些见解。

我已经确定作业参数在大约十分之一的运行中可用,即使没有代码或配置更改也是如此。这是随机成功而非随机失败的案例,因此很难追踪。

我使用调试器深入研究了 spring 代码,并确定当此操作失败时,在注入(inject)发生时没有在 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;
}
}

作业启动参数:

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>

最佳答案

让作业参数起作用的重要步骤是定义 StepScope bean 并确保您的阅读器是一个 @StepScope 组件。

我会尝试以下方法:

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

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

然后,确保您的 bean 通过使用 @StepScope-annotation(几乎与您的示例中的一样)是步进范围的。注入(inject)不是 @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...
}

关于java - 来自 Spring Batch 的 JobParameters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27862027/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com