Spring批处理 - 运行JobLauncher

时间:2015-09-30 08:43:20

标签: spring spring-batch

我在项目中集成了Spring Batch,我在运行JobLauncher时遇到了问题。

在我的班级JobLauncher中我有这个:

  public class XXXJobLauncher {
@Autowired
@Qualifier("testJob")
Job testJob;

@Autowired
JobLauncher jobLauncher;

@ServiceActivator
public void run(FileMetaData fileMetaData) {
    ///

    JobParametersBuilder jobParametersBuilder = new JobParametersBuilder();
    jobParametersBuilder.addString("uuiid", fileMetaData.getUuid());
    jobParametersBuilder.addLong("id", dataImport.getId());

    try {
        jobLauncher.run(testJob, jobParametersBuilder.toJobParameters());
    } catch (JobExecutionAlreadyRunningException | JobRestartException | JobParametersInvalidException | JobInstanceAlreadyCompleteException e) {
        e.printStackTrace();
    }
}

配置我使用XML配置:

ConfigBatch.xml:

     <import resource="classpath:job-config.xml"/>

<bean id="jobRepositoryFactory" class="org.springframework.batch.core.explore.support.JobExplorerFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="tablePrefix" value="BATCH_"/>
</bean>

<bean id="jobRepository" class="org.springframework.batch.core.repository.support.SimpleJobRepository"
      factory-bean="jobRepositoryFactory"/>

<bean id="batchTaskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"
      destroy-method="shutdown">
    <property name="corePoolSize" value="6"/>
    <property name="allowCoreThreadTimeOut" value="true"/>
    <property name="waitForTasksToCompleteOnShutdown" value="true"/>
</bean>

<bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
    <property name="jobRepository" ref="jobRepository"/>
    <property name="taskExecutor" ref="batchTaskExecutor"/>
</bean>

在job-Config.xml中我有:

   <batch:job id="testJob"
           incrementer="incrementer"
           job-repository="jobRepository">
    <batch:step id="Step1">
        <batch:tasklet throttle-limit="4"
                       transaction-manager="transactionManager"
                       task-executor="batchTaskExecutor">
            <batch:chunk reader="XXXReader"
                         processor="passThroughItemProcessor"
                         writer="XXXWriter"
                         skip-limit="100"
                         commit-interval="10">
                <batch:skippable-exception-classes>
                    <batch:include class="org.springframework.batch.item.file.FlatFileParseException"/>
                </batch:skippable-exception-classes>
                <batch:listeners>
                    <batch:listener ref="XXXListener"/>
                </batch:listeners>
            </batch:chunk>
        </batch:tasklet>
    </batch:step>
</batch:job>

<bean id="XXXReader"
      class="com.reader.XXXReader"
      scope="step">
    <constructor-arg value="#{jobParameters[uuiid]}" name="uuiid"/>
</bean>

<bean id="XXXWriter"
      class="com.writer.XXXWriter"
      scope="step">
    <constructor-arg value="#{jobParameters[ id]}" name="id"/>
</bean>

<bean id="XXXListener"
      class="com.listener.XXXListener">
</bean>

<bean id="passThroughItemProcessor" class="org.springframework.batch.item.support.PassThroughItemProcessor"/>

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

当我在JobLauncher课程中调试时,它停在jobLuancher.run中,我也不例外,它看起来像春天批次没有认识到读者和whriter !!有什么建议吗?

1 个答案:

答案 0 :(得分:1)

我在batchConfig.xml中解决了问题:

<batch:job-repository id="jobRepository"
                      data-source="dataSource"
                      transaction-manager="batchTransactionManager"/>

<bean class="org.springframework.batch.core.configuration.support.JobRegistryBeanPostProcessor">
    <property name="jobRegistry" ref="jobRegistry"/>
</bean>

<bean id="jobRegistry" class="org.springframework.batch.core.configuration.support.MapJobRegistry"/>

<bean id="jobLauncher"
      class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
    <property name="jobRepository" ref="jobRepository"/>
</bean>

<bean id="batchTransactionManager"
      class="org.springframework.batch.support.transaction.ResourcelessTransactionManager"/>

<bean id="batchTaskExecutor"
      class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor"
      destroy-method="shutdown">
    <property name="corePoolSize" value="4"/>
    <property name="allowCoreThreadTimeOut" value="true"/>
    <property name="waitForTasksToCompleteOnShutdown" value="true"/>
</bean>

<bean id="jobExplorer"
      class="org.springframework.batch.core.explore.support.JobExplorerFactoryBean">
    <property name="dataSource" ref="dataSource"/>
</bean>