com.github.hengboy.job.schedule.store.DefaultJobStore Maven / Gradle / Ivy
/*
* Copyright [2019] [恒宇少年 - 于起宇]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.hengboy.job.schedule.store;
import com.github.hengboy.job.core.enums.JobExecuteStatusEnum;
import com.github.hengboy.job.core.exception.JobException;
import com.github.hengboy.job.core.wrapper.JobWrapper;
import com.github.hengboy.job.core.wrapper.support.CronJobWrapper;
import com.github.hengboy.job.core.wrapper.support.LoopJobWrapper;
import com.github.hengboy.job.schedule.resource.MicroJobScheduleResource;
import com.github.hengboy.job.schedule.store.model.JobDetail;
import com.github.hengboy.job.schedule.store.model.JobExecuteLog;
import com.github.hengboy.job.schedule.store.model.JobExecuteQueue;
import java.sql.Timestamp;
import java.util.List;
import java.util.UUID;
/**
* 数据库方式的数据源
* 操作数据库内对应的数据表数据
*
* @author:恒宇少年 - 于起宇
*
* DateTime:2019-01-28 14:03
* Blog:http://blog.yuqiyu.com
* WebSite:http://www.jianshu.com/u/092df3f77bca
* Gitee:https://gitee.com/hengboy
* GitHub:https://github.com/hengyuboy
*/
public class DefaultJobStore extends JobStoreSupport {
/**
* 添加一个新的任务
* 将任务添加到任务队列数据表内
*
* @param jobWrapper 任务封装对象
* @return 任务队列主键值
* @throws JobException 任务异常
*/
@Override
public String addJob(JobWrapper jobWrapper) throws JobException {
// 添加任务定义信息 & 返回任务的主键值
JobDetail jobDetail = selectJob(jobWrapper.getJobKey());
if (jobDetail == null) {
throw new JobException("No job key defined as " + jobWrapper.getJobKey() + " was found");
}
// 根据任务定义的主键值进行添加任务队列 & 返回任务队列的主键值(用于创建quartz job)
String jobQueueId = getDelegate().addJobQueue(jobDetail.getMjdJobKey(), jobWrapper);
try {
// 根据执行方式进行更新相关内容
switch (jobWrapper.getJobExecuteAway()) {
case CRON_EXPRESSION:
// 转换cron封装对象
CronJobWrapper cronJobWrapper = (CronJobWrapper) jobWrapper;
// 执行更新cron表达式
getDelegate().updateJobQueueCron(jobQueueId, cronJobWrapper.getCron());
break;
case LOOP:
// 转换loop封装对象
LoopJobWrapper loopJobWrapper = (LoopJobWrapper) jobWrapper;
// 执行更新loop循环次数、循环间隔时间
getDelegate().updateJobQueueLoop(jobQueueId, loopJobWrapper.getLoopTimes(), loopJobWrapper.getLoopIntervalTime());
break;
case ONCE:
//..
break;
}
} catch (Exception e) {
throw new JobException("Execute addJob exception:" + e.getMessage(), e);
}
return jobQueueId;
}
/**
* 根据任务的定义key查询任务详细信息
*
* @param jobKey 任务定义key
* @return 任务详情
* @throws JobException 任务异常
*/
@Override
public JobDetail selectJob(String jobKey) throws JobException {
return MicroJobScheduleResource.getCONSUMER_JOBS().get(jobKey);
}
/**
* 查询任务队列
*
* @param jobQueueId 任务队列主键值
* @return 任务执行队列
* @throws JobException 任务异常
*/
@Override
public JobExecuteQueue selectJobQueueById(String jobQueueId) throws JobException {
return getDelegate().selectQueueById(jobQueueId);
}
/**
* 根据任务自定义key查询任务队列信息
*
* @param jobQueueKey 任务队列自定义key
* @return 任务执行队列
* @throws JobException 任务异常
*/
@Override
public JobExecuteQueue selectJobQueueByKey(String jobQueueKey) throws JobException {
return getDelegate().selectQueueByKey(jobQueueKey);
}
/**
* 根据自定义的任务队列key进行删除任务
*
* @param jobQueueId 任务队列主键
* @throws JobException 任务异常
*/
@Override
public void removeJob(String jobQueueId) throws JobException {
getDelegate().removeJobQueue(jobQueueId);
}
/**
* 根据自定义的任务队列key集合进行批量删除任务
*
* @param jobQueueIds 自定义任务队列的key集合
* @throws JobException 任务异常
*/
@Override
public void removeJobs(List jobQueueIds) throws JobException {
if (jobQueueIds != null) {
jobQueueIds.stream().forEach(jobQueueId -> removeJob(jobQueueId));
}
}
/**
* 暂停任务执行
* - 查询任务执行日志
* - 更新任务日志状态为暂停
*
* @param jobQueueId 自定义任务队列的key
* @throws JobException 任务异常
*/
@Override
public void pauseJob(String jobQueueId) throws JobException {
// 任务执行队列
JobExecuteQueue jobExecuteQueue = getDelegate().selectQueueById(jobQueueId);
// 更新日志状态
getDelegate().updateLogState(jobExecuteQueue.getJeqId(), JobExecuteStatusEnum.PAUSE.toString());
}
/**
* 启动任务
*
* @param jobQueueId 任务队列主键
* @throws JobException 任务异常
*/
@Override
public void startJob(String jobQueueId) throws JobException {
// 任务执行队列
JobExecuteQueue jobExecuteQueue = getDelegate().selectQueueById(jobQueueId);
// 更新日志状态
getDelegate().updateLogState(jobExecuteQueue.getJeqId(), JobExecuteStatusEnum.WAIT.toString());
}
/**
* 根据自定义的任务队列的key验证任务是否存在
*
* @param jobQueueId 任务队列主键
* @return true:任务存在,false:任务不存在
* @throws JobException 任务异常
*/
@Override
public boolean checkExists(String jobQueueId) throws JobException {
JobExecuteQueue jobExecuteQueue = getDelegate().selectQueueById(jobQueueId);
return jobExecuteQueue != null;
}
/**
* 添加任务执行日志
*
* @param jobQueueId 任务队列编号
* @return 任务日志主键
* @throws JobException 任务异常
*/
@Override
public String addJobLog(String jobQueueId) throws JobException {
// 日志主键值
String jobLogId = UUID.randomUUID().toString();
// 执行保存日志
JobExecuteLog jobExecuteLog = new JobExecuteLog();
jobExecuteLog.setJelId(jobLogId);
jobExecuteLog.setJelQueueId(jobQueueId);
getDelegate().addLog(jobExecuteLog);
return jobLogId;
}
/**
* 更新任务状态
*
* @param jobExecuteLogId 任务日志编号
* @param jobState 任务状态
* @throws JobException 任务异常
*/
@Override
public void updateJobState(String jobExecuteLogId, String jobState) throws JobException {
getDelegate().updateLogState(jobExecuteLogId, jobState);
}
/**
* 更新任务执行成功时间
*
* @param jobExecuteLogId 任务日志编号
* @throws JobException 任务异常
*/
@Override
public void updateJobExecuteSuccessTime(String jobExecuteLogId) throws JobException {
getDelegate().updateLogSuccessTime(jobExecuteLogId, new Timestamp(System.currentTimeMillis()));
}
/**
* 更新任务执行消费者地址
*
* @param jobExecuteLogId 任务日志编号
* @param consumerAddress 任务消费的消费者地址
* @throws JobException 任务异常
*/
@Override
public void updateJobExecuteConsumer(String jobExecuteLogId, String consumerAddress) throws JobException {
getDelegate().updateLogConsumerAddress(jobExecuteLogId, consumerAddress);
}
/**
* 更新日志重试次数
*
* @param jobExecuteLogId 任务执行日志编号
* @param retryTimes 重试次数
* @throws JobException 任务异常
*/
@Override
public void updateJobRetryTimes(String jobExecuteLogId, int retryTimes) throws JobException {
getDelegate().updateLogRetryCount(jobExecuteLogId, retryTimes);
}
}