spring boot 整合 Quartz

1.配置类

package com.zorasoft.workstack.web.configuration;

import com.zorasoft.workstack.web.infrastructures.schedulings.BillingStatisticsCurrentMonthJob;
import com.zorasoft.workstack.web.infrastructures.schedulings.BillingStatisticsLastMonthJob;
import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 定时任务配置文件
 *@Author TangYinJian
 *@Date 2019-06-28 12:13:14
 *@Version 1.0
 */
@Configuration
public class QuartzConfigration {

    public static String JOB_GROUP = " BILLING_STATISTICS_GROUP";

    public enum JOBTYPE{
        CURRENTMONTHCODE("统计当月账单", "STATISTICS-CURRENT-MONTH"),
        LASTMONTHCODE("统计上月账单", "STATISTICS-LAST-MONTH")
        ;

        private String value;
        private String name;

        JOBTYPE(String name, String value) {
            this.value = value;
            this.name = name;
        }

        public String getValue() {
            return value;
        }

        public String getName() {
            return name;
        }
    }

    @Bean
    public JobDetail billingStatisticsCurrentMonthJobDetail() {
        return JobBuilder.newJob(BillingStatisticsCurrentMonthJob.class)                         // 填写执行任务类
                .withIdentity(JOBTYPE.CURRENTMONTHCODE.getValue(), JOB_GROUP)       // 填写执行任务的name和group,多个job group可以相同,name不能相同
                .storeDurably()
                .build();
    }

    @Bean
    public Trigger billingStatisticsCurrentMonthJobTrigger(JobDetail billingStatisticsCurrentMonthJobDetail) {
        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0 0 21 * * ?"); // 1-10日每天晚上9点
        //CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0 12 9 1-30 * ?");//测试用
        return TriggerBuilder.newTrigger().forJob(billingStatisticsCurrentMonthJobDetail)   // 要执行的job
                .withIdentity(JOBTYPE.CURRENTMONTHCODE.getValue())                      // 要执行的job name
                .withSchedule(scheduleBuilder)
                .build();
    }


    @Bean
    public JobDetail billingStatisticsLastMonthJobDetail() {
        return JobBuilder.newJob(BillingStatisticsLastMonthJob.class)                         // 填写执行任务类
            .withIdentity(JOBTYPE.LASTMONTHCODE.getValue(), JOB_GROUP)       // 填写执行任务的name和group,多个job group可以相同,name不能相同
            .storeDurably()
            .build();
    }

    @Bean
    public Trigger billingStatisticsLastMonthJobTrigger(JobDetail billingStatisticsLastMonthJobDetail) {
        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0 0 21 1-10 * ?"); // 每天晚上九点执行
        //CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0 36 15 1-10 * ?");//测试用
        return TriggerBuilder.newTrigger().forJob(billingStatisticsLastMonthJobDetail)   // 要执行的job
            .withIdentity(JOBTYPE.LASTMONTHCODE.getValue())                      // 要执行的job name
            .withSchedule(scheduleBuilder)
            .build();
    }

}
 

2.业务类

package com.zorasoft.workstack.web.infrastructures.schedulings;

import com.zorasoft.workstack.web.domains.services.consumptionBillDetailsService.ConsumptionBillDetailsService;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @ClassName BillingStatisticsCurrentMonthJob
 * @Description 根据计费日志统计每月的账单
 * 1.定时(暂定每晚9点执行)查询所有套餐绑定企业,当月10号前根据每个企业统计上个月的所有费用,然后入库到账单表,如果已缴费则不统计;
 * 2.定时(暂定每晚9点执行)查询所有套餐绑定企业,根据每个企业统计当月的所有费用,然后入库到账单表。
 * @Author TangYinJian
 * @Date 2019-06-28 12:13:14
 * @Version 1.0
 **/
@Component
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
public class BillingStatisticsCurrentMonthJob extends QuartzJobBean {

    private final static Logger logger = LoggerFactory.getLogger(BillingStatisticsCurrentMonthJob.class);

    @Autowired
    private ConsumptionBillDetailsService consumptionBillDetailsService;

    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        try {
            this.consumptionBillDetailsService.billingStatisticsCurrentMonth();
        } catch (Exception e) {
            logger.error(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+":************** 当月账单统计失败*******************");
            logger.error(e.getMessage(), e);
        }
    }

}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值