Spring boot 如何防止在加载上下文后立即执行Spring批处理作业

Spring boot 如何防止在加载上下文后立即执行Spring批处理作业,spring-boot,spring-batch,Spring Boot,Spring Batch,我是SpringBatch的新手,现在,我有一个从Quartz调度器触发SpringBatch作业的示例。我的应用程序是通过使用SpringBoot加载Spring上下文启动的 我的问题是,每次加载完应用程序上下文后,spring批处理作业都会立即执行,这是出乎意料的。之后,该作业由Quartz触发,作为在cron表达式中设置的时间 我希望有一个解决方案,用于删除在加载spring上下文之后立即发生的作业的第一次运行。谁能给我一个方法吗 下面是我的配置文件,我的作业名为“importUserJo

我是SpringBatch的新手,现在,我有一个从Quartz调度器触发SpringBatch作业的示例。我的应用程序是通过使用SpringBoot加载Spring上下文启动的

我的问题是,每次加载完应用程序上下文后,spring批处理作业都会立即执行,这是出乎意料的。之后,该作业由Quartz触发,作为在cron表达式中设置的时间

我希望有一个解决方案,用于删除在加载spring上下文之后立即发生的作业的第一次运行。谁能给我一个方法吗

下面是我的配置文件,我的作业名为“importUserJob”

import java.util.HashMap;
导入java.util.Map;
导入javax.sql.DataSource;
导入javax.transaction.TransactionManager;
导入org.quartz.JobDataMap;
导入org.springframework.batch.core.Job;
导入org.springframework.batch.core.JobExecutionListener;
导入org.springframework.batch.core.Step;
导入org.springframework.batch.core.configuration.JobRegistry;
导入org.springframework.batch.core.configuration.annotation.BatchConfigurer;
导入org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
导入org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
导入org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
导入org.springframework.batch.core.configuration.support.JobRegistryBeanPostProcessor;
导入org.springframework.batch.core.configuration.support.MapJobRegistry;
导入org.springframework.batch.core.explore.JobExplorer;
导入org.springframework.batch.core.explore.support.JobExplorerFactoryBean;
导入org.springframework.batch.core.launch.JobLauncher;
导入org.springframework.batch.core.launch.support.RunIdIncrementer;
导入org.springframework.batch.core.launch.support.simplejoblancher;
导入org.springframework.batch.core.repository.JobRepository;
导入org.springframework.batch.core.repository.support.JobRepositoryFactoryBean;
导入org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean;
导入org.springframework.batch.core.repository.support.SimpleJobRepository;
导入org.springframework.batch.item.ItemReader;
导入org.springframework.batch.item.NonTransientResourceException;
导入org.springframework.batch.item.ParseException;
导入org.springframework.batch.item.UnexpectedInputException;
导入org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider;
导入org.springframework.batch.item.database.JdbcBatchItemWriter;
导入org.springframework.batch.item.file.FlatFileItemReader;
导入org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
导入org.springframework.batch.item.file.mapping.DefaultLineMapper;
导入org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.beans.factory.annotation.Qualifier;
导入org.springframework.context.annotation.Bean;
导入org.springframework.context.annotation.Configuration;
导入org.springframework.core.io.ClassPathResource;
导入org.springframework.core.task.SimpleAsyncTaskExecutor;
导入org.springframework.core.task.TaskExecutor;
导入org.springframework.jdbc.core.jdbc模板;
导入org.springframework.scheduling.Trigger;
导入org.springframework.scheduling.TriggerContext;
导入org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
导入org.springframework.scheduling.quartz.CronTriggerFactoryBean;
导入org.springframework.scheduling.quartz.JobDetailFactoryBean;
导入org.springframework.scheduling.quartz.SchedulerFactoryBean;
导入org.springframework.scheduling.support.CronTrigger;
导入org.springframework.stereotype.Component;
导入org.springframework.transaction.PlatformTransactionManager;
@配置
@启用批处理
公共类BatchConfiguration实现BatchConfigurer{
@自动连线
公共建筑商建筑商工厂;
@自动连线
公共StepBuilderFactory StepBuilderFactory;
@自动连线
公共数据源;
@自动连线
@限定符(“jpaTxnManagerBean”)
公共平台transactionManager transactionManager;
@豆子
公共PersonItemProcessor处理器(){
返回新的PersonItemProcessor();
}
@Bean(name=“importUserJob”)
公共作业导入器作业(作业完成通知侦听器侦听器){
返回jobBuilderFactory.get(“importUserJob”)
.incrementer(新的RunIdIncrementer())
.listener(侦听器)
.flow(步骤1())
(完)
.build();
}
@豆子
公共步骤第1步(){
返回stepBuilderFactory.get(“step1”)。
区块(10)
.reader(reader())
.processor(processor()).transactionManager(transactionManager)
.build();
}
@豆子
公共任务执行器任务执行器(){
ThreadPoolTaskExecutor taskExecutor=新的ThreadPoolTaskExecutor();
taskExecutor.setMaxPoolSize(10);
taskExecutor.setCorePoolSize(5);
taskExecutor.AfterPropertieSet();
返回任务执行器;
}
@豆子
公共项目阅读器(){
返回新的ItemReader(){
int i=0;
@凌驾
公共对象读取()
引发异常、UnexpectedInputException、ParseException、非TransientResourceException{
如果(i==100)返回null;
if(i%10==0)System.out.println(“\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu”);
i++;
System.out.println(“读卡器访问:+i”);
返回新字符串(“abc”);//null无法运行处理器
}
};
}
@豆子
公共SchedulerFactoryBean SchedulerFactoryBean(JobCompletionNotificationListener侦听器)引发异常{
SchedulerFactoryBean
import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;
import javax.transaction.TransactionManager;

import org.quartz.JobDataMap;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecutionListener;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.JobRegistry;
import org.springframework.batch.core.configuration.annotation.BatchConfigurer;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.support.JobRegistryBeanPostProcessor;
import org.springframework.batch.core.configuration.support.MapJobRegistry;
import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.core.explore.support.JobExplorerFactoryBean;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.core.launch.support.SimpleJobLauncher;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.repository.support.JobRepositoryFactoryBean;
import org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean;
import org.springframework.batch.core.repository.support.SimpleJobRepository;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.NonTransientResourceException;
import org.springframework.batch.item.ParseException;
import org.springframework.batch.item.UnexpectedInputException;
import org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider;
import org.springframework.batch.item.database.JdbcBatchItemWriter;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.core.task.TaskExecutor;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;

@Configuration
@EnableBatchProcessing
public class BatchConfiguration implements BatchConfigurer{

    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    public StepBuilderFactory stepBuilderFactory;

    @Autowired
    public DataSource dataSource;

    @Autowired
    @Qualifier("jpaTxnManagerBean")
    public PlatformTransactionManager transactionManager;

    @Bean
    public PersonItemProcessor processor() {
        return new PersonItemProcessor();
    }

    @Bean(name="importUserJob")
    public Job importUserJob(JobCompletionNotificationListener listener) {
        return jobBuilderFactory.get("importUserJob")
                .incrementer(new RunIdIncrementer())
                .listener(listener)
                .flow(step1())
                .end()
                .build();
    }

    @Bean
    public Step step1() {
        return stepBuilderFactory.get("step1").
             chunk(10)
             .reader(reader())
                .processor(processor()).transactionManager(transactionManager)
                .build();
    }

    @Bean
    public TaskExecutor taskExecutor(){
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setMaxPoolSize(10);
        taskExecutor.setCorePoolSize(5);
        taskExecutor.afterPropertiesSet();
        return taskExecutor;
    }

    @Bean
    public ItemReader<Object> reader() {
        return new ItemReader<Object>() {
            int i = 0;
            @Override
            public Object read()
                    throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {

                if(i == 100) return null;
                if(i%10 == 0) System.out.println("_________________");
                i++;
                System.out.println("Access to reader: "+i);

                return new String("abc"); // null could not run processor
            }
        };
    }

    @Bean
    public SchedulerFactoryBean schedulerFactoryBean(JobCompletionNotificationListener listener) throws Exception{
        SchedulerFactoryBean schedulerFac = new SchedulerFactoryBean();
        CronTriggerFactoryBean cronTrigger = new CronTriggerFactoryBean();
        cronTrigger.setCronExpression("00 31 23 ? * TUE-SAT *");

        cronTrigger.setName("EveryWeekDay");
        cronTrigger.setGroup("quatz-group");

        JobDetailFactoryBean jobDetail = new JobDetailFactoryBean();
        jobDetail.setName("importUserJob");
        jobDetail.setJobClass(JobLauncherDetails.class);
        jobDetail.setDurability(true);
        jobDetail.setGroup("quatz-group");
        Map<String, Object> map = new HashMap<>();
        map.put("jobName", "importUserJob");
        map.put("jobLocator", getJobRegistry());
        map.put("jobLauncher", getJobLauncher());
        jobDetail.setJobDataAsMap(map);
        jobDetail.afterPropertiesSet();

        cronTrigger.setJobDetail(jobDetail.getObject());
        cronTrigger.afterPropertiesSet();

        schedulerFac.setTriggers(cronTrigger.getObject());
        schedulerFac.afterPropertiesSet();

        return schedulerFac;
    }

    @Bean
    public JobRegistryBeanPostProcessor jobRegistryBeanPostProcessor() throws Exception{
        JobRegistryBeanPostProcessor postProcessor = new JobRegistryBeanPostProcessor();
        postProcessor.setJobRegistry(getJobRegistry());
        postProcessor.afterPropertiesSet();
        return postProcessor;
    }

    @Bean
    public JobRegistry getJobRegistry(){
        MapJobRegistry jobRegistry = new MapJobRegistry();
        return jobRegistry;
    }

    @Override
    public JobExplorer getJobExplorer() throws Exception {
        JobExplorerFactoryBean jobExplorer = new JobExplorerFactoryBean();
        jobExplorer.setDataSource(dataSource);
        jobExplorer.afterPropertiesSet();
        return jobExplorer.getObject();
    }

    @Override
    public JobLauncher getJobLauncher() throws Exception {
        SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
        jobLauncher.setJobRepository(getJobRepository());
        jobLauncher.setTaskExecutor(taskExecutor());
        jobLauncher.afterPropertiesSet();
        return jobLauncher;
    }

    @Override
    public JobRepository getJobRepository() throws Exception {
        JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
        factory.setDataSource(dataSource);
        factory.setTransactionManager(getTransactionManager());
        factory.afterPropertiesSet();
        return  (JobRepository) factory.getObject();
    }

    @Override
    public PlatformTransactionManager getTransactionManager() throws Exception {
        return transactionManager;
    }

}