Spring Batch:是否存在范围作业

时间:2014-03-06 16:36:17

标签: java spring scope spring-batch

我收到了以下batch job

<batch:job id="importPersonsJob" job-repository="jobRepository">
    <batch:step id="importPersonStep">
        <batch:tasklet transaction-manager="transactionManager">
            <batch:chunk reader="personItemReader" writer="personItemWriter"
                commit-interval="5" skip-limit="10">
                <batch:skippable-exception-classes>
                    <batch:include class="java.lang.Throwable" />
                </batch:skippable-exception-classes>
            </batch:chunk>
            <batch:listeners>
                <batch:listener ref="skipListener" />
            </batch:listeners>
        </batch:tasklet>
    </batch:step>
    <batch:listeners>
        <batch:listener ref="authenticationJobListener" />
        <batch:listener ref="jobListener" />
    </batch:listeners>
</batch:job>

我需要访问skipListener和jobListener中的JobParameters。为此,我尝试了以下方法:

<bean id="skipListener" class="job.batch.listener.Log4JSkipListener" scope="step">
    <property name="logPath" value="#{jobParameters['logPath']}" />
</bean>

<bean id="jobListener" class="job.batch.listener.JobListener" scope="job">
    <property name="propertyAccessManager" ref="propertyAccessManager" />
    <property name="logPath" value="#{jobParameters['logPath']}" />
</bean>

它似乎适用于skipListener,因为我得到了异常:

 [java] Caused by: java.lang.IllegalStateException: No Scope registered for scope 'job'
 [java]     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:325)
 [java]     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
 [java]     at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:322)
 [java]     at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106)
 [java]     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1360)
 [java]     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1118)
 [java]     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
 [java]     ... 43 more

是否存在范围作业?我在page找到了它。知道怎么解决我的问题吗?我甚至尝试使用scope=batch,这导致了同样的异常。

这是来自常春藤的依赖:

<dependency org="org.springframework.batch" name="spring-batch-core" rev="2.1.9.RELEASE" conf="default->default">
    <exclude org="org.aspectj" name="aspectjweaver"/>
</dependency>

2 个答案:

答案 0 :(得分:1)

您发布的API文档的链接显示“自3.0以来”,所以我想您将不得不使用Spring Batch 3.0(我认为还不是GA)。

答案 1 :(得分:0)

如果您使用的是3.0之前的SB版本(因为此版本的scope="job"可用,因为@DaveSyer写了),您可以获得相同的结果,将您的侦听器声明为单例并执行logPath参数提取在job.batch.listener.JobListener.beforeJob()

<bean id="jobListener" class="job.batch.listener.JobListener">
    <property name="propertyAccessManager" ref="propertyAccessManager" />
</bean>

class job.batch.listener.JobListener implements JobExecutionListener {
  private String logPath;

  void beforeJob(JobExecution jobExecution) {
    jobExecution.getJobParameters().getString("logPath");
  }
}