how to stop spring batch scheduled jobs from running at first time when executing the code?

前端 未结 4 1487
有刺的猬
有刺的猬 2020-12-28 15:53

i\'m using spring batch 2.2.4 with quartz to run some jobs at certain time

the problem is the jobs always run after executing the code at the first time then it runs

相关标签:
4条回答
  • 2020-12-28 16:30

    I guess some configuration problem. Here is the configurations I tested with same cron expression. I've launch-context.xml with following configuration.

    <bean class="org.springframework.batch.core.configuration.support.AutomaticJobRegistrar">
        <property name="applicationContextFactories">
            <bean
                class="org.springframework.batch.core.configuration.support.ClasspathXmlApplicationContextsFactoryBean">
    
                <property name="resources">
                    <list>
                        <value>classpath*:configurations/kp-batch.xml</value>
                    </list>
                </property>
    
            </bean>
        </property>
    
        <property name="jobLoader" >
            <bean
                class="org.springframework.batch.core.configuration.support.DefaultJobLoader">
                <property name="jobRegistry" ref="jobRegistry" />
            </bean>
        </property>
    </bean>
    
    <bean id="jobRegistry"
        class="org.springframework.batch.core.configuration.support.MapJobRegistry" />
    
    <bean id="schedule"
        class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
        <property name="triggers">
            <list>
                <ref bean="cronTrigger1"/>
            </list>
        </property>
    </bean>
    
    <bean id="cronTrigger1"
        class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
        <property name="jobDetail" ref="kpJobDetail" />
        <property name="cronExpression" value="0 0 0 1/1 * ? *"/>
    </bean>
    
    <bean id="kpJobDetail" class="org.springframework.scheduling.quartz.JobDetailBean">
        <property name="jobClass"
            value="com.viasat.nbn.nms.webservices.util.SpringBatchQuartzJobLauncher" />
        <property name="jobDataAsMap">
            <map>
                <entry key="jobName" value="Trigger Job for 12AM" />
                <entry key="jobLocator" value-ref="jobRegistry" />
                <entry key="jobLauncher" value-ref="jobLauncher" />
            </map>
        </property>
    </bean>
    
    <bean id="batchTransactionManager"
        class="org.springframework.batch.support.transaction.ResourcelessTransactionManager">
        <property name="rollbackOnCommitFailure" value="false" />
    </bean>
    
    <bean id="jobLauncher"
        class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
        <property name="jobRepository" ref="jobRepository" />
    </bean>
    
    <bean id="jobRepository"
        class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
        <property name="transactionManager" ref="batchTransactionManager" />
    </bean>
    

    In the kp-batch.xml, I've defined job, itemreader, itemwriter etc.

    0 讨论(0)
  • 2020-12-28 16:33

    I had the same problem and determined that it was caused by Spring boot's autoconfiguration service. By default, it will run all configured job beans after application start.

    There are two properties that affect this behavior:

    • spring.batch.job.enabled
    • spring.batch.job.names

    The first prevents the launching of all jobs when set to false. The second accepts a comma-delimited list of job names that will be run.

    These two properties can be set a variety of ways specified in the Spring boot docs:

    1. Command line (--spring.batch.job.enabled=false)
    2. Java system properties (-Dspring.batch.job.enabled=false)
    3. OS environment variables
    4. @PropertySource annotations
    5. application.properties file in the jar directory
    6. application.properties file inside the jar
    7. SpringApplication.setDefaultProperties
    0 讨论(0)
  • 2020-12-28 16:35

    adding

    spring.batch.job.enabled=false
    

    in application.properties works with me.

    0 讨论(0)
  • 2020-12-28 16:51

    To solve this you will have to create one more properties file and name it "batch.properties".

    # Disable batch auto-start
    spring.batch.job.enabled=false
    

    You can give the reference to this file from your java configuration file.

    Sample:

    @Configuration
    @ComponentScan("com.code")
    @EnableBatchProcessing
    @PropertySource("classpath:batch.properties")
    public class AppConfig {
    
    }
    

    @PropertySource("classpath:batch.properties")

    Hope this helps.

    0 讨论(0)
提交回复
热议问题