具有不同属性文件的相同批处理作业取决于启动

时间:2014-10-15 13:56:27

标签: java spring-batch

我在xml中有一个作业定义:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:batch="http://www.springframework.org/schema/batch"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/batch
                           http://www.springframework.org/schema/batch/spring-batch-2.2.xsd">

    <import resource="../config/config.xml"/>
    <import resource="../config/common.xml"/>
    <context:property-placeholder order="0"
location="classpath:spring/batch/properties/ProcessAccountingMessages.properties"/>
    <!-- Batch job -->
    <batch:job id="processEventMessages" parent="abstractJob">
        <batch:step id="processEventMessagesStep" parent="abstractStep">
            <batch:tasklet transaction-manager="transactionManager">
                <batch:chunk reader="processEventMessagesReader"
                             processor="processEventMessagesProcessor"
                             writer="processEventMessagesWriter"
                             commit-interval="1" skip-limit="10">
                    <batch:skippable-exception-classes>
                        <batch:include class="com.ing.crs.frwk.exp.CRS2FunctionalException" />
                    </batch:skippable-exception-classes>
                </batch:chunk>
            </batch:tasklet>
        </batch:step>
    </batch:job>
    <!-- Beans -->
    <!-- Tasklets -->
    <!-- Readers -->
    <bean id="processEventMessagesReader"
          class="org.springframework.batch.item.database.JpaPagingItemReader" scope="step">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
        <property name="queryString"
                  value="SELECT a.id FROM TempRawEventEntity a where a.integrationTryCounter > 0 and a.serviceId = '#{jobParameters['partition.key']}' and (a.typeError IS null or a.typeError = com.ing.crs.data.dbenum.EventIntegrationErrorCode.WAITING) order by a.id"/>
        <property name="transacted" value="false"/>
    </bean>
    <!-- Proccessors -->
    <bean id="processEventMessagesProcessor"
          class="com.ing.crs.batch.spring.processevents.processor.ProcessEventProcessor" scope="step">
        <property name="sourceApplicationClassName" value="#{jobParameters['source.application.name']}"/>
    </bean>
    <!-- Writers -->
    <bean id="processEventMessagesWriter"
          class="com.ing.crs.batch.spring.common.writer.DummyWriter"/>
    <!-- Listeners -->
    <!-- Utils -->
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource"/>
    </bean>
</beans>

我这样推出:

        // Create application context based on the provided configuration.
        final ApplicationContext context = new ClassPathXmlApplicationContext(config);
        // Create job launcher. Uses fixed job launcher name at the moment!
        final JobLauncher jobLauncher = (JobLauncher) context.getBean(JOB_LAUNCHER_NAME);
        // Create the job.
        final Job job = (Job) context.getBean(jobName);

        jobLauncher.run(job, jobParameters);

但现在我需要完全相同的工作定义,但唯一的区别是我想使用不同的属性。

<context:property-placeholder order="0"
    location="classpath:spring/batch/properties/ProcessAccountingMessages.properties"/>

那么我怎样才能轻松地做到这一点而无需使用相同的工作创建一个新的xml只是为了更改属性占位符的行?我也不想为此使用jobParameters。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我建议使用bean配置文件。对要使用的每个版本的属性使用配置文件将允许您指定在运行时使用哪个属性。您可以在此处阅读有关bean配置文件的更多信息:http://spring.io/blog/2011/02/11/spring-framework-3-1-m1-released/