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);
}
}
}