spring boot quartz 使用 单机模式 多任务

pom文件依赖:

    org.quartz-scheduler
    quartz
    2.2.1
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.expression.ParseException;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

@Slf4j
@Configuration
public class SchedledConfiguration {

    @Bean(name = "detailFactoryBean")
    public MethodInvokingJobDetailFactoryBean detailFactoryBean(ScheduledTasks scheduledTasks) {
        MethodInvokingJobDetailFactoryBean bean = new MethodInvokingJobDetailFactoryBean();
        //这儿设置对应的Job对象
        bean.setTargetObject(scheduledTasks);
        //这儿设置对应的方法名  与执行具体任务调度类中的方法名对应
        bean.setTargetMethod("work");
        bean.setConcurrent(false);
        return bean;
    }

    @Bean(name = "cronTriggerBean")
    public CronTriggerFactoryBean cronTriggerBean(@Qualifier("detailFactoryBean") MethodInvokingJobDetailFactoryBean detailFactoryBean) {
        CronTriggerFactoryBean trigger = new CronTriggerFactoryBean();
        trigger.setJobDetail(detailFactoryBean.getObject());
        try {
              trigger.setCronExpression ("00 00 00 * * ?");//每天00:00:00执行一次
              //trigger.setCronExpression("0/10 * * * * ? ");//每10秒执行一次
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return trigger;
    }


    /**
     * 注入扣费逻辑
     */
    @Bean(name = "deductFeeFactoryBean")
    public MethodInvokingJobDetailFactoryBean deductFeeFactoryBean(ScheduledTasks scheduledTasks) {
        MethodInvokingJobDetailFactoryBean bean = new MethodInvokingJobDetailFactoryBean();
        bean.setTargetObject(scheduledTasks);
        bean.setTargetMethod("deduct");
        bean.setConcurrent(false);
        return bean;
    }

    @Bean(name = "deductFeeCronTriggerBean")
    public CronTriggerFactoryBean deductFeeCronTriggerBean(@Qualifier("deductFeeFactoryBean") MethodInvokingJobDetailFactoryBean deductFeeFactoryBean) {
        CronTriggerFactoryBean trigger = new CronTriggerFactoryBean();
        trigger.setJobDetail(deductFeeFactoryBean.getObject());
        try {
            trigger.setCronExpression ("0/1 * * * * ? ");

        } catch (ParseException e) {
            log.info("deductFeeCronTriggerBean==>{}",e.getMessage());
        }
        return trigger;
    }



    @Bean
    public SchedulerFactoryBean schedulerFactory(@Qualifier("cronTriggerBean")CronTriggerFactoryBean cronTriggerBean,@Qualifier("deductFeeCronTriggerBean")CronTriggerFactoryBean deductFeeCronTriggerBean) {
        SchedulerFactoryBean schedulerFactory = new SchedulerFactoryBean();
        schedulerFactory.setTriggers(cronTriggerBean.getObject(),deductFeeCronTriggerBean.getObject());
        return schedulerFactory;
    }

}

 

import com.juphoon.miyou.cloud.service.CompanyService;
import com.juphoon.miyou.cloud.service.SaveStatisMessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;
import java.io.FileNotFoundException;

@Slf4j
@Component
@Configurable
@EnableScheduling
public class ScheduledTasks {


    public void work() throws FileNotFoundException {

         log.info("任务1!");
    }


    public void deduct(){

        log.info("任务2!");
        
    }
}

你可能感兴趣的:(java,spring,boot)