springboot+vue 根据数据库cron执行定时任务

新建定时类
QuartzConfig

import org.jeecg.modules.yuerin.job.SyncLargescaleScheduledJob;
import org.quartz.Trigger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

/**
 * Quartz定時任務類
 * @author
 **/
@Configuration
public class QuartzConfig {
    /**
     * Details:配置定时任务
     */
    @Bean(name = "jobDetail")
    public MethodInvokingJobDetailFactoryBean detailFactoryBean(SyncLargescaleScheduledJob syncLargescaleScheduledJob) { // quartzScheduledTasks为需要执行的任务
        MethodInvokingJobDetailFactoryBean jobDetail = new MethodInvokingJobDetailFactoryBean();
//           是否并发执行
//           例如每5s执行一次任务,但是当前任务还没有执行完,就已经过了5s了,
//          如果此处为true,则下一个任务会执行,如果此处为false,则下一个任务会等待上一个任务执行完后,再开始执行
        jobDetail.setConcurrent(false);
        jobDetail.setName("srd-chhliu");// 设置任务的名字
        jobDetail.setGroup("srd");// 设置任务的分组,这些属性都可以存储在数据库中,在多任务的时候使用
        // 为需要执行的实体类对应的对象
        jobDetail.setTargetObject(syncLargescaleScheduledJob);
        //test为需要执行的方法
        //通过这几个配置,告诉JobDetailFactoryBean我们需要执行定时执行ScheduleTask类中的sayHello方法
        jobDetail.setTargetMethod("executeInternal");
        return jobDetail;
    }

    /**
     * Details:配置定时任务的触发器,也就是什么时候触发执行定时任务
     */
    @Bean(name = "jobTriggers")
    public CronTriggerFactoryBean cronJobTrigger(MethodInvokingJobDetailFactoryBean jobDetail) {
        CronTriggerFactoryBean tigger = new CronTriggerFactoryBean();
        tigger.setJobDetail(jobDetail.getObject());
        EmailConfigVO emailConfigVO = draSysConfigService
                .getConfigSystemByConfigKey(SysConfigEnum.Email, EmailConfigVO.class);//查数据库
        String cron =emailConfigVO.getCron();//数据库cron字段
        if(cron==null){

        }else{
            tigger.setCronExpression(cron);
        }
        //tigger.setCronExpression("0/1 * * * * ? *");
        tigger.setName("srd-chhliu");//
        return tigger;
    }

    /**
     * Details:定义quartz调度工厂
     */
    @Bean(name = "scheduler")
    public SchedulerFactoryBean schedulerFactory(Trigger cronJobTrigger) {
        SchedulerFactoryBean bean = new SchedulerFactoryBean();
        // 用于quartz集群,QuartzScheduler 启动时更新己存在的Job
        bean.setOverwriteExistingJobs(true);
        // 延时启动,应用启动1秒后
        bean.setStartupDelay(1);
        // 注册触发器
        bean.setTriggers(cronJobTrigger);
        return bean;
    }
}

新建任务类
SyncLargescaleScheduledJob

import com.aliyun.oss.ClientException;
import org.jeecg.modules.yuerin.entity.YuerLargescale;
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 javax.mail.MessagingException;

@Component
@Configurable
@EnableScheduling
public class SyncLargescaleScheduledJob {
    @Autowired
    private YuerLargescaleService yuerLargescaleService;

    public void executeInternal()
    {
        try {
            yuerLargescale.sendEmailScheduled();//执行定时执行的代码
        } catch (MessagingException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}

执行所要定时执行代码

@Transactional(rollbackFor = Exception.class)
    public void sendEmailScheduled() throws MessagingException, ClientException {
        LOGGER.info("sendEmailScheduled--定时任务");
    }

你可能感兴趣的:(数据库,spring,boot,vue.js)