谷歌包-moli-去掉quart

This commit is contained in:
khalil
2024-04-16 16:21:08 +08:00
parent 99c7666dab
commit 15f183b050
30 changed files with 0 additions and 2854 deletions

View File

@@ -83,17 +83,6 @@
<artifactId>UserAgentUtils</artifactId>
<version>${bitwalker.version}</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>${quartz.version}</version>
<exclusions>
<exclusion>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP-java7</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 金融级实人认证 -->
<dependency>
<groupId>com.aliyun</groupId>

View File

@@ -25,7 +25,6 @@
<spring-context.version>3.2.9.RELEASE</spring-context.version>
<spring.security.version>3.2.5.RELEASE</spring.security.version>
<mysql-connector-java.version>8.0.17</mysql-connector-java.version>
<quartz.version>2.3.2</quartz.version>
<servlet-api.version>3.1.0</servlet-api.version>
<http-client.version>4.5</http-client.version>
<guava.version>31.1-jre</guava.version>

View File

@@ -1,22 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.accompany</groupId>
<artifactId>accompany-scheduler</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>accompany-scheduler-sdk</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.accompany</groupId>
<artifactId>accompany-core</artifactId>
<version>${revision}</version>
</dependency>
</dependencies>
</project>

View File

@@ -1,31 +0,0 @@
package com.accompany.scheduler.constant;
/**
* @author PaperCut
* Created by PaperCut on 2018/8/1.
* Quartz任务类型
*/
public enum QuartzJobType {
JavaJobClass(1, "JavaJobClass"), SqlJob(2, "SqlJob"), HttpGetJob(3, "HttpGetJob");
public final int value;
private final String desc;
QuartzJobType(int value, String desc) {
this.value = value;
this.desc = desc;
}
public String getDesc() {
return desc;
}
public static String getDesc(int type) {
for (QuartzJobType enumType : QuartzJobType.values()) {
if (enumType.value == type) {
return enumType.getDesc();
}
}
return "" + type;
}
}

View File

@@ -1,42 +0,0 @@
package com.accompany.scheduler.model;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
@Data
public class QuartzJob implements Serializable {
private Long id;
private String expression;
private String description;
private String jobName;
private String jobClassName;
private Integer jobType;
private String jobData;
private Boolean isOneTime;
private Date nextFireTime;
private Date prevFireTime;
private String triggerState;
private String triggerType;
private Date startTime;
private Date endTime;
private Date createTime;
private Date updateTime;
}

View File

@@ -13,11 +13,6 @@
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.accompany</groupId>
<artifactId>accompany-scheduler-sdk</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>com.accompany</groupId>
<artifactId>accompany-business-service</artifactId>

View File

@@ -1,23 +0,0 @@
package com.accompany.scheduler.exception;
public class JobException extends RuntimeException {
public JobException() {
super();
}
public JobException(String message) {
super(message);
}
public JobException(String message, Throwable cause) {
super(message, cause);
}
public JobException(Throwable cause) {
super(cause);
}
protected JobException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}

View File

@@ -1,136 +0,0 @@
package com.accompany.scheduler.job;
import com.accompany.core.model.Room;
import com.accompany.common.netease.ErBanNetEaseService;
import com.accompany.core.service.common.JedisService;
import com.accompany.business.service.room.RoomHotService;
import com.accompany.business.service.room.RoomService;
import com.accompany.business.vo.RoomVo;
import com.google.gson.Gson;
import com.accompany.common.constant.Constant;
import com.accompany.common.netease.neteaseacc.result.RoomUserListRet;
import com.accompany.common.redis.RedisKey;
import org.apache.commons.lang3.StringUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import java.util.*;
import java.util.Map.Entry;
/**
* 定时检查房间是否异常关闭任务
*
* @author yanghaoyu
*/
public class CheckRoomExceptionJob implements Job {
private static final Logger logger = LoggerFactory.getLogger(CheckRoomExceptionJob.class);
@Autowired
private RoomService roomService;
@Autowired
private ErBanNetEaseService erBanNetEaseService;
@Autowired
private JedisService jedisService;
@Autowired
private RoomHotService roomHotService;
private Gson gson = new Gson();
@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
try {
logger.info("正在执行定时任务:查询异常退出房间数据");
/* 从缓存中查询正在开播的房间id */
List<Long> keepAliveRoomUids = roomHotService.getVipRoomListByCheckRoom();
Map<String, String> map = jedisService.hgetAll(RedisKey.room_running.getKey());
List<RoomVo> roomList = new ArrayList<>();
if (map != null && map.size() > 0) {
Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, String> entry = it.next();
String key = entry.getKey();
if (StringUtils.isNotEmpty(key)) {
String roomVoStr = jedisService.hget(RedisKey.room.getKey(), key.toString());
RoomVo roomVo = gson.fromJson(roomVoStr, RoomVo.class);
roomList.add(roomVo);
}
}
} else {
roomList = roomService.getHomeRunningRoomList();
}
for (RoomVo roomVo : roomList) {
/* 是否是开播状态 */
if (roomVo!=null && roomVo.getValid()!= null && roomVo.getValid()) {
/* 是否是断开连接状态 */
/* 查询房间信息 */
if (!CollectionUtils.isEmpty(keepAliveRoomUids)) {
boolean flag = keepAliveRoomUids.contains(roomVo.getUid());
if (flag) {
logger.info("当前房间是vip房间不进行异常关闭检查.uid=" + roomVo.getUid());
continue;
}
}
if (roomVo.getIsExceptionClose()) {
/* 断开连接时间是否超过5分钟 */
if ((System.currentTimeMillis() - roomVo.getExceptionCloseTime().getTime()) > 60000*10) {
logger.info("当前房间已经断开连接超过10分钟判断是否重连:uid=" + roomVo.getUid());
/* 判断是否重连 */
/* 查询房间所有成员信息 */
Room room = new Room();
room.setUid(roomVo.getUid());
//查询云信中当前房间的状态
RoomUserListRet roomUserListRet = erBanNetEaseService
.getRoomMemberListInfo(roomVo.getRoomId(), roomVo.getUid());
//房间没没有在线人员 关闭房间
if (roomUserListRet == null) {
roomService.closeRoom(roomVo.getUid());
continue;
}
//或者房间类型为陪伴房的时候 关闭房间
if (roomUserListRet != null) {
if (null != roomVo.getType()) {
if (roomVo.getType().equals(Constant.RoomType.companion)) {
roomService.closeRoom(roomVo.getUid());
continue;
}
}
}
for (Entry<String, List<Map<String, Object>>> entry : roomUserListRet.getDesc()
.entrySet()) {
for (Entry<String, Object> entry2 : entry.getValue().get(0).entrySet()) {
if ("onlineStat".equals(entry2.getKey())) {
boolean onlineStat = (boolean) entry2.getValue();
/* 是否在线在线为true */
if (onlineStat) {
logger.info("当前房间已经重连,uid:" + roomVo.getUid());
/* 重新连接上 将异常信息改为false */
room.setIsExceptionClose(false);
} else {
logger.info("当前房间未重连,正在关闭房间。uid:" + roomVo.getUid());
/* 未重新链接,关闭房间 */
roomService.closeRoom(roomVo.getUid());
room.setValid(false);
room.setIsExceptionClose(false);
}
roomService.updateRunningRoom(room);
break;
}
}
}
}
}
}
}
} catch (Exception e) {
logger.error("定时任务:查询异常房间失败。", e);
}
}
}

View File

@@ -1,35 +0,0 @@
package com.accompany.scheduler.job;
import com.accompany.scheduler.model.QuartzJob;
import com.accompany.scheduler.util.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import java.io.IOException;
/**
* Created by PaperCut on 2018/6/11.
* Http请求任务实体
*/
@Slf4j
public class HttpGetJobDetail implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext) {
JobDataMap dataMap = jobExecutionContext.getMergedJobDataMap();
QuartzJob job = (QuartzJob) dataMap.get("jobBean");
String url = job.getJobData();
log.info("Invoking http GET: {}", url);
try {
String resp = HttpUtils.get(null, url);
if (resp == null) {
log.error("Failed to request http get: {}, result is null", url);
} else {
log.info("Successful http get: {}", url);
}
} catch (IOException e) {
log.error("Failed to invoke http GET: {}, msg: {}", url, e.getMessage());
}
}
}

View File

@@ -1,26 +0,0 @@
package com.accompany.scheduler.job;
import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.scheduling.quartz.AdaptableJobFactory;
import org.springframework.stereotype.Service;
/**
*
* @author yanghaoyu 将quartz中的job注入到spring容器中交给他管理。
*/
@Service("jobFactory")
public class JobFactory extends AdaptableJobFactory {
@Autowired
private AutowireCapableBeanFactory capableBeanFactory;
@Override
protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
Object jobInstance = super.createJobInstance(bundle);
/* 进行注入 */
capableBeanFactory.autowireBean(jobInstance);
return jobInstance;
}
}

View File

@@ -1,29 +0,0 @@
package com.accompany.scheduler.job;
import com.accompany.business.service.RobotService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
public class RefreshRobotJob implements Job {
private static final Logger LOGGER = LoggerFactory.getLogger(RefreshRobotJob.class);
@Autowired
private RobotService robotService;
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
try {
robotService.addRobotToPermitRoom();
} catch (Exception e) {
LOGGER.error("RefreshRobotJob error...",e);
}
}
}

View File

@@ -1,25 +0,0 @@
package com.accompany.scheduler.job;
import com.accompany.core.base.SpringContextHolder;
import com.accompany.scheduler.model.QuartzJob;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.springframework.jdbc.core.JdbcTemplate;
/** Created by PaperCut on 2018/6/11. Sql任务实体 */
@Slf4j
public class SqlJobDetail implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext) {
long start = System.currentTimeMillis();
JobDataMap dataMap = jobExecutionContext.getMergedJobDataMap();
QuartzJob job = (QuartzJob) dataMap.get("jobBean");
log.info("获取 jdbcTemplate");
JdbcTemplate template = SpringContextHolder.getBean("jdbcTemplate");
template.execute(job.getJobData());
log.info(job.getJobName() + " executed in {} ms", System.currentTimeMillis() - start);
}
}

View File

@@ -1,49 +0,0 @@
package com.accompany.scheduler.mapper;
import com.accompany.scheduler.model.QuartzJob;
import com.accompany.scheduler.model.QuartzJobExample;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface QuartzJobMapper {
int countByExample(QuartzJobExample example);
int deleteByExample(QuartzJobExample example);
int deleteByPrimaryKey(Long id);
int insert(QuartzJob record);
int insertSelective(QuartzJob record);
List<QuartzJob> selectByExample(QuartzJobExample example);
QuartzJob selectByPrimaryKey(Long id);
int updateByExampleSelective(@Param("record") QuartzJob record, @Param("example") QuartzJobExample example);
int updateByExample(@Param("record") QuartzJob record, @Param("example") QuartzJobExample example);
int updateByPrimaryKeySelective(QuartzJob record);
int updateByPrimaryKey(QuartzJob record);
QuartzJob getPeriodic(String name);
QuartzJob getOneTime(String name);
String getJobNameById(Long id);
int deleteByName(String name);
QuartzJob getByName(String name);
int countPeriodic();
int countOneTime();
List<QuartzJob> selectPeriodicByPage(@Param("start") int start, @Param("count") int count);
List<QuartzJob> selectOneTimeByPage(@Param("start") int start, @Param("count") int count);
}

View File

@@ -1,73 +0,0 @@
package com.accompany.scheduler.repository;
import com.accompany.scheduler.mapper.QuartzJobMapper;
import com.accompany.scheduler.model.QuartzJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
/**
* Created by PaperCut on 2018/6/11.
*/
@Component
public class JdbcQuartzJobRepository implements QuartzJobRepository {
@Autowired
QuartzJobMapper quartzJobMapper;
@Override
public void add(QuartzJob job) {
quartzJobMapper.insert(job);
}
@Override
public void update(QuartzJob job) {
quartzJobMapper.updateByPrimaryKey(job);
}
@Override
public void delete(QuartzJob job) {
quartzJobMapper.deleteByPrimaryKey(job.getId());
}
@Override
public QuartzJob get(Long id) {
return quartzJobMapper.selectByPrimaryKey(id);
}
@Override
public QuartzJob getByName(String name) {
return quartzJobMapper.getByName(name);
}
@Override
public QuartzJob getPeriod(String name) {
return quartzJobMapper.getPeriodic(name);
}
@Override
public QuartzJob getOneTime(String name) {
return quartzJobMapper.getOneTime(name);
}
@Override
public List<QuartzJob> findPeriodByPage(Integer page, Integer pageSize) {
return quartzJobMapper.selectPeriodicByPage((page - 1) * pageSize, pageSize);
}
@Override
public Integer countPeriod() {
return quartzJobMapper.countPeriodic();
}
@Override
public List<QuartzJob> findOneTimeByPage(Integer page, Integer pageSize) {
return quartzJobMapper.selectOneTimeByPage((page - 1) * pageSize, pageSize);
}
@Override
public Integer countOneTime() {
return quartzJobMapper.countOneTime();
}
}

View File

@@ -1,29 +0,0 @@
package com.accompany.scheduler.repository;
import com.accompany.scheduler.model.QuartzJob;
import java.util.List;
public interface QuartzJobRepository {
void add(QuartzJob job);
void update(QuartzJob job);
void delete(QuartzJob job);
QuartzJob get(Long id);
QuartzJob getByName(String name);
QuartzJob getPeriod(String name);
QuartzJob getOneTime(String name);
List<QuartzJob> findPeriodByPage(Integer page, Integer pageSize);
Integer countPeriod();
List<QuartzJob> findOneTimeByPage(Integer page, Integer pageSize);
Integer countOneTime();
}

View File

@@ -1,134 +0,0 @@
package com.accompany.scheduler.service;
import com.accompany.scheduler.constant.QuartzJobType;
import com.accompany.scheduler.exception.JobException;
import com.accompany.scheduler.model.QuartzJob;
import com.accompany.scheduler.repository.QuartzJobRepository;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Date;
/**
* @author PaperCut
* Created by PaperCut on 2018/12/14.
* 抽象任务处理类
*/
@Slf4j
public abstract class AbstractQuartzJobService implements QuartzJobService {
protected QuartzJobRepository quartzJobRepository;
protected Scheduler quartzScheduler;
@Autowired
public AbstractQuartzJobService(QuartzJobRepository quartzJobRepository, Scheduler quartzScheduler) {
this.quartzJobRepository = quartzJobRepository;
this.quartzScheduler = quartzScheduler;
// 执行初始化
init();
}
public void init() {
try {
quartzScheduler.getListenerManager().addTriggerListener(new SimpleTriggerListener());
} catch (SchedulerException e) {
log.error("Failed to initialize QuartJobService.", e);
}
}
@Override
public QuartzJob add(QuartzJob job) throws Exception {
boolean isExists = exists(job.getJobName());
if (isExists) {
throw new JobException("该任务名已存在");
}
job.setCreateTime(new Date());
quartzJobRepository.add(job);
schedule(job);
return job;
}
@Override
public QuartzJob update(QuartzJob job) throws Exception {
Trigger.TriggerState state = quartzScheduler.getTriggerState(getTriggerKey(job.getJobName()));
quartzScheduler.deleteJob(getJobKey(job.getJobName()));
schedule(job);
if (state == Trigger.TriggerState.PAUSED) {
quartzScheduler.pauseJob(getJobKey(job.getJobName()));
}
job.setUpdateTime(new Date());
quartzJobRepository.update(job);
return job;
}
@Override
public void delete(Long id) throws Exception {
QuartzJob job = quartzJobRepository.get(id);
if (job == null) {
throw new JobException("未找到该任务");
}
quartzJobRepository.delete(job);
quartzScheduler.deleteJob(getJobKey(job.getJobName()));
}
@Override
public void deleteByName(String name) throws Exception {
QuartzJob job = quartzJobRepository.getByName(name);
if (job == null) {
throw new JobException("未找到该任务");
}
quartzJobRepository.delete(job);
quartzScheduler.deleteJob(getJobKey(job.getJobName()));
}
@Override
public boolean exists(String name) throws Exception {
return quartzScheduler.checkExists(getJobKey(name));
}
@Override
public void resume(String name) throws JobException, SchedulerException {
JobDetail jobDetail = quartzScheduler.getJobDetail(getJobKey(name));
TriggerKey triggerKey = getTriggerKey(name);
Trigger trigger = quartzScheduler.getTrigger(triggerKey);
if (trigger != null) {
QuartzJob jobEntity = quartzJobRepository.getPeriod(name);
CronTrigger newTrigger = TriggerBuilder.newTrigger()
.withIdentity(getTriggerKey(name))
.forJob(jobDetail)
.withSchedule(CronScheduleBuilder.cronSchedule(jobEntity.getExpression()))
.build();
quartzScheduler.rescheduleJob(triggerKey, newTrigger);
}
}
@Override
public void pause(String name) throws Exception {
quartzScheduler.pauseJob(getJobKey(name));
}
protected JobFactory getFactory(QuartzJob job) {
Integer jobType = job.getJobType();
if (jobType == QuartzJobType.SqlJob.value) {
return new SqlJobFactory();
} else if (jobType == QuartzJobType.JavaJobClass.value) {
return new JavaJobFactory();
} else if (jobType == QuartzJobType.HttpGetJob.value) {
return new HttpGetJobFactory();
} else {
throw new IllegalArgumentException("Not found this job type for job factory.");
}
}
abstract void schedule(QuartzJob job) throws Exception;
abstract String getGroup();
abstract JobKey getJobKey(String name);
abstract TriggerKey getTriggerKey(String name);
}

View File

@@ -1,37 +0,0 @@
package com.accompany.scheduler.service;
import com.accompany.scheduler.job.HttpGetJobDetail;
import com.accompany.scheduler.model.QuartzJob;
import org.quartz.JobBuilder;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
/**
* Created by PaperCut on 2018/6/11.
* http请求类型任务
*/
public class HttpGetJobFactory implements JobFactory {
@Override
public JobDetail create(QuartzJob job) throws ClassNotFoundException {
JobDataMap jobDataMap = new JobDataMap();
jobDataMap.put("jobBean", job);
JobDetail jobDetail = JobBuilder.newJob(HttpGetJobDetail.class)
.withIdentity(job.getJobName())
.withDescription(job.getDescription())
.usingJobData(jobDataMap)
.build();
return jobDetail;
}
@Override
public JobDetail create(String group, QuartzJob job) {
JobDataMap jobDataMap = new JobDataMap();
jobDataMap.put("jobBean", job);
JobDetail jobDetail = JobBuilder.newJob(HttpGetJobDetail.class)
.withIdentity(job.getJobName(), group)
.withDescription(job.getDescription())
.usingJobData(jobDataMap)
.build();
return jobDetail;
}
}

View File

@@ -1,51 +0,0 @@
package com.accompany.scheduler.service;
import com.accompany.scheduler.model.QuartzJob;
import org.quartz.JobBuilder;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* Created by PaperCut on 2018/6/11.
* Java类型任务
*/
public class JavaJobFactory implements JobFactory {
private final Map<String, Class> clazzCache = new ConcurrentHashMap<>();
private Class findClass(String className) throws ClassNotFoundException {
Class instance = null;
if ((instance = clazzCache.get(className)) == null) {
Class clazz = Class.forName(className);
clazzCache.put(className, clazz);
instance = clazzCache.get(className);
}
return instance;
}
@Override
public JobDetail create(QuartzJob job) throws ClassNotFoundException {
JobDataMap jobDataMap = new JobDataMap();
jobDataMap.put("jobBean", job);
JobDetail jobDetail = JobBuilder.newJob(findClass(job.getJobClassName()))
.withIdentity(job.getJobName())
.withDescription(job.getDescription())
.usingJobData(jobDataMap)
.build();
return jobDetail;
}
@Override
public JobDetail create(String group, QuartzJob job) throws ClassNotFoundException {
JobDataMap jobDataMap = new JobDataMap();
jobDataMap.put("jobBean", job);
JobDetail jobDetail = JobBuilder.newJob(findClass(job.getJobClassName()))
.withIdentity(job.getJobName(), group)
.withDescription(job.getDescription())
.usingJobData(jobDataMap)
.build();
return jobDetail;
}
}

View File

@@ -1,14 +0,0 @@
package com.accompany.scheduler.service;
import com.accompany.scheduler.model.QuartzJob;
import org.quartz.JobDetail;
/**
* Created by PaperCut on 2018/12/14.
* Job工厂接口
*/
public interface JobFactory {
JobDetail create(QuartzJob job) throws ClassNotFoundException;
JobDetail create(String group, QuartzJob job) throws ClassNotFoundException;
}

View File

@@ -1,67 +0,0 @@
package com.accompany.scheduler.service;
import com.accompany.scheduler.model.QuartzJob;
import com.accompany.scheduler.repository.QuartzJobRepository;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @author PaperaCut
* Created by PaperCut on 2018/12/14.
* 持久任务服务类
*/
@Service
public class PeriodQuartzJobService extends AbstractQuartzJobService {
@Autowired
public PeriodQuartzJobService(QuartzJobRepository quartzJobRepository, Scheduler quartzScheduler) {
super(quartzJobRepository, quartzScheduler);
}
@Override
String getGroup() {
return "CRON";
}
@Override
public JobKey getJobKey(String name) {
return new JobKey(name, getGroup());
}
@Override
public TriggerKey getTriggerKey(String name) {
return new TriggerKey(name + "_trigger", getGroup());
}
@Override
public void schedule(QuartzJob job) throws Exception {
JobDetail jobDetail = getFactory(job).create(getGroup(), job);
CronTrigger trigger = TriggerBuilder.newTrigger()
.withIdentity(getTriggerKey(job.getJobName()))
.withSchedule(CronScheduleBuilder.cronSchedule(job.getExpression()))
.build();
quartzScheduler.scheduleJob(jobDetail, trigger);
}
/**
* 分页获取周期性任务
*
* @param page
* @param pageSize
* @return
*/
public List<QuartzJob> findPeriodByPage(Integer page, Integer pageSize) {
return quartzJobRepository.findPeriodByPage(page, pageSize);
}
/**
* 统计获取周期性任务数量
*
* @return
*/
public Integer countPeriod() {
return quartzJobRepository.countPeriod();
}
}

View File

@@ -1,24 +0,0 @@
package com.accompany.scheduler.service;
import com.accompany.scheduler.model.QuartzJob;
import org.quartz.SchedulerException;
/**
* Created by PaperCut on 2018/12/14.
* 任务管理接口
*/
public interface QuartzJobService {
QuartzJob add(QuartzJob job) throws Exception;
QuartzJob update(QuartzJob job) throws Exception;
void delete(Long id) throws Exception;
void deleteByName(String name) throws Exception;
boolean exists(String name) throws Exception;
void resume(String name) throws SchedulerException;
void pause(String name) throws Exception;
}

View File

@@ -1,163 +0,0 @@
package com.accompany.scheduler.service;
import com.accompany.common.utils.DateTimeUtil;
import com.accompany.scheduler.constant.QuartzJobType;
import com.accompany.scheduler.job.SqlJobDetail;
import com.accompany.scheduler.mapper.QuartzJobMapper;
import com.accompany.scheduler.model.QuartzJob;
import org.apache.commons.lang3.time.FastDateFormat;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.ParseException;
import java.util.Date;
@Service
public class QuartzPeriodJobService {
private static final String GROUP_CRON = "CRON";
private static final String GROUP_ONE_TIME = "ONE_TIME";
@Autowired
Scheduler quartzScheduler;
@Autowired
QuartzJobMapper quartzJobMapper;
public void add(QuartzJob job) throws SchedulerException, ClassNotFoundException {
boolean isExists = exists(job.getJobName());
if (isExists) {
throw new SchedulerException("该任务名已存在");
}
Date curDate = new Date();
job.setCreateTime(curDate);
job.setUpdateTime(curDate);
schedulePeriodic(job);
}
public void addOneTime(QuartzJob job) throws SchedulerException, ParseException, ClassNotFoundException {
boolean isExists = exists(job.getJobName());
if (isExists) {
throw new SchedulerException("该任务名已存在");
}
job.setIsOneTime(true);
job.setCreateTime(new Date());
quartzJobMapper.insert(job);
scheduleOneTime(job);
}
private void scheduleOneTime(QuartzJob job) throws ClassNotFoundException, ParseException, SchedulerException {
JobDetail jobDetail = null;
JobDataMap dataMap = new JobDataMap();
dataMap.put("jobBean", job);
if (job.getJobType() == QuartzJobType.JavaJobClass.value) {
jobDetail = JobBuilder.newJob((Class<Job>) Class.forName(job.getJobClassName()))
.withIdentity(job.getJobName(), GROUP_CRON)
.withDescription(job.getDescription()).usingJobData(dataMap)
.build();
} else if (job.getJobType() == QuartzJobType.SqlJob.value) {
jobDetail = JobBuilder.newJob(SqlJobDetail.class)
.withIdentity(job.getJobName(), GROUP_ONE_TIME)
.withDescription(job.getDescription()).usingJobData(dataMap)
.build();
}
Date date = FastDateFormat.getInstance(DateTimeUtil.DEFAULT_DATE_PATTERN).parse(job.getExpression());
SimpleTrigger trigger = (SimpleTrigger) TriggerBuilder.newTrigger()
.withIdentity(job.getJobName() + "_trigger", GROUP_ONE_TIME)
.startAt(date) // some Date
.forJob(jobDetail)
.build();
quartzScheduler.scheduleJob(jobDetail, trigger);
}
private void schedulePeriodic(QuartzJob job) throws ClassNotFoundException, SchedulerException {
JobDetail jobDetail = null;
JobDataMap dataMap = new JobDataMap();
dataMap.put("jobBean", job);
if (job.getJobType() == QuartzJobType.JavaJobClass.value) {
jobDetail = JobBuilder.newJob((Class<Job>) Class.forName(job.getJobClassName()))
.withIdentity(job.getJobName(), GROUP_CRON)
.withDescription(job.getDescription()).usingJobData(dataMap)
.build();
} else if (job.getJobType() == QuartzJobType.SqlJob.value) {
jobDetail = JobBuilder.newJob(SqlJobDetail.class)
.withIdentity(job.getJobName(), GROUP_CRON)
.withDescription(job.getDescription()).usingJobData(dataMap)
.build();
}
CronTrigger trigger = TriggerBuilder.newTrigger()
.withIdentity(job.getJobName() + "_trigger", GROUP_CRON)
.forJob(jobDetail)
.withSchedule(CronScheduleBuilder.cronSchedule(job.getExpression()))
.build();
quartzScheduler.scheduleJob(jobDetail, trigger);
}
public void resume(String name) throws SchedulerException {
TriggerKey triggerKey = new TriggerKey(name + "_trigger", GROUP_CRON);
JobDetail jobDetail = quartzScheduler.getJobDetail(new JobKey(name, GROUP_CRON));
Trigger oldTrigger = quartzScheduler.getTrigger(triggerKey);
if (oldTrigger != null) {
QuartzJob job = quartzJobMapper.getPeriodic(name);
CronTrigger newTrigger = TriggerBuilder.newTrigger()
.withIdentity(triggerKey)
.forJob(jobDetail)
.withSchedule(CronScheduleBuilder.cronSchedule(job.getExpression()))
.build();
quartzScheduler.rescheduleJob(triggerKey, newTrigger);
}
}
public void update(QuartzJob job) throws SchedulerException, ClassNotFoundException {
Trigger.TriggerState state = quartzScheduler.getTriggerState(new TriggerKey(job.getJobName() + "_trigger", GROUP_CRON));
quartzScheduler.deleteJob(new JobKey(job.getJobName(), GROUP_CRON));
schedulePeriodic(job);
if (state == Trigger.TriggerState.PAUSED) {
quartzScheduler.pauseJob(new JobKey(job.getJobName(), GROUP_CRON));
}
job.setUpdateTime(new Date());
quartzJobMapper.updateByPrimaryKey(job);
}
public void pause(String name) throws SchedulerException {
quartzScheduler.pauseJob(new JobKey(name, GROUP_CRON));
}
public void runOnce(String name, boolean periodic) throws SchedulerException {
quartzScheduler.triggerJob(new JobKey(name, periodic ? GROUP_CRON : GROUP_ONE_TIME));
}
public boolean exists(String name) throws SchedulerException {
return quartzScheduler.checkExists(new JobKey(name, GROUP_CRON));
}
public void delete(Long id) throws SchedulerException {
String jobName = quartzJobMapper.getJobNameById(id);
quartzScheduler.deleteJob(new JobKey(jobName, GROUP_CRON));
quartzJobMapper.deleteByPrimaryKey(id);
}
public void deleteByOneTime(Long id) throws SchedulerException {
String jobName = quartzJobMapper.getJobNameById(id);
quartzScheduler.deleteJob(new JobKey(jobName, GROUP_ONE_TIME));
quartzJobMapper.deleteByPrimaryKey(id);
}
public void deleteByName(String name) throws SchedulerException {
quartzScheduler.deleteJob(new JobKey(name, GROUP_CRON));
quartzJobMapper.deleteByName(name);
}
public void deleteByNameForOneTime(String name) throws SchedulerException {
quartzScheduler.deleteJob(new JobKey(name, GROUP_ONE_TIME));
quartzJobMapper.deleteByName(name);
}
}

View File

@@ -1,73 +0,0 @@
package com.accompany.scheduler.service;
import com.accompany.common.utils.DateTimeUtil;
import com.accompany.scheduler.model.QuartzJob;
import com.accompany.scheduler.repository.QuartzJobRepository;
import org.apache.commons.lang3.time.FastDateFormat;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
/**
* @author PaperCut
* @since 2020-03-20
* 简单定时任务
*/
@Service
public class SimpleQuartzJobService extends AbstractQuartzJobService {
@Autowired
public SimpleQuartzJobService(QuartzJobRepository quartzJobRepository, Scheduler quartzScheduler) {
super(quartzJobRepository, quartzScheduler);
}
@Override
String getGroup() {
return "ONE_TIME";
}
@Override
JobKey getJobKey(String name) {
return new JobKey(name, getGroup());
}
@Override
TriggerKey getTriggerKey(String name) {
return new TriggerKey(name + "_trigger", getGroup());
}
@Override
public void schedule(QuartzJob job) throws Exception {
Date date = FastDateFormat.getInstance(DateTimeUtil.DEFAULT_DATE_PATTERN).parse(job.getExpression());
JobDetail jobDetail = getFactory(job).create(getGroup(), job);
SimpleTrigger trigger = (SimpleTrigger) TriggerBuilder.newTrigger()
.withIdentity(getTriggerKey(job.getJobName()))
.startAt(date)
.forJob(jobDetail)
.build();
quartzScheduler.scheduleJob(jobDetail, trigger);
}
/**
* 分页获取一次性任务
*
* @param page
* @param pageSize
* @return
*/
public List<QuartzJob> findOneTimeByPage(Integer page, Integer pageSize) {
return quartzJobRepository.findOneTimeByPage(page, pageSize);
}
/**
* 统计一次性任务数量
*
* @return
*/
public Integer countOneTime() {
return quartzJobRepository.countOneTime();
}
}

View File

@@ -1,24 +0,0 @@
package com.accompany.scheduler.service;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.quartz.Trigger;
import org.quartz.listeners.TriggerListenerSupport;
/**
* Created by PaperCut on 2018/6/12.
* 一次性任务触发器
*/
@Slf4j
public class SimpleTriggerListener extends TriggerListenerSupport {
@Override
public String getName() {
return "SimpleTriggerListener";
}
@Override
public void triggerComplete(Trigger trigger, JobExecutionContext context, Trigger.CompletedExecutionInstruction triggerInstructionCode) {
log.info("Completed trigger...");
super.triggerComplete(trigger, context, triggerInstructionCode);
}
}

View File

@@ -1,37 +0,0 @@
package com.accompany.scheduler.service;
import com.accompany.scheduler.job.SqlJobDetail;
import com.accompany.scheduler.model.QuartzJob;
import org.quartz.JobBuilder;
import org.quartz.JobDataMap;
import org.quartz.JobDetail;
/**
* Created by PaperCut on 2018/12/14.
* Sql类型任务
*/
public class SqlJobFactory implements JobFactory {
@Override
public JobDetail create(QuartzJob job) {
JobDataMap dataMap = new JobDataMap();
dataMap.put("jobBean", job);
JobDetail jobDetail = JobBuilder.newJob(SqlJobDetail.class)
.withIdentity(job.getJobName())
.withDescription(job.getDescription())
.usingJobData(dataMap)
.build();
return jobDetail;
}
@Override
public JobDetail create(String group, QuartzJob job) {
JobDataMap dataMap = new JobDataMap();
dataMap.put("jobBean", job);
JobDetail jobDetail = JobBuilder.newJob(SqlJobDetail.class)
.withIdentity(job.getJobName(), group)
.withDescription(job.getDescription())
.usingJobData(dataMap)
.build();
return jobDetail;
}
}

View File

@@ -1,291 +0,0 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.accompany.scheduler.mapper.QuartzJobMapper">
<resultMap id="BaseResultMap" type="com.accompany.scheduler.model.QuartzJob">
<id column="id" property="id" jdbcType="BIGINT"/>
<result column="job_name" property="jobName" jdbcType="VARCHAR"/>
<result column="expression" property="expression" jdbcType="VARCHAR"/>
<result column="description" property="description" jdbcType="VARCHAR"/>
<result column="job_class_name" property="jobClassName" jdbcType="VARCHAR"/>
<result column="job_type" property="jobType" jdbcType="INTEGER"/>
<result column="job_data" property="jobData" jdbcType="VARCHAR"/>
<result column="is_one_time" property="isOneTime" jdbcType="BIT"/>
<result column="next_fire_time" property="nextFireTime" jdbcType="TIMESTAMP"/>
<result column="prev_fire_time" property="prevFireTime" jdbcType="TIMESTAMP"/>
<result column="trigger_state" property="triggerState" jdbcType="VARCHAR"/>
<result column="trigger_type" property="triggerType" jdbcType="VARCHAR"/>
<result column="start_time" property="startTime" jdbcType="TIMESTAMP"/>
<result column="end_time" property="endTime" jdbcType="TIMESTAMP"/>
<result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
<result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
</resultMap>
<sql id="Base_Column_List">
qj.job_name as jobName
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long">
select
<include refid="Base_Column_List"/>
from quartz_job qj
where id = #{id,jdbcType=BIGINT}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
delete from quartz_job
where id = #{id,jdbcType=BIGINT}
</delete>
<insert id="insert" parameterType="com.accompany.scheduler.model.QuartzJob">
<selectKey resultType="java.lang.Long" keyProperty="id" order="AFTER">
SELECT LAST_INSERT_ID()
</selectKey>
insert into quartz_job (expression, description, job_name,
job_class_name, job_type, job_data,
is_one_time, next_fire_time, prev_fire_time,
trigger_state, trigger_type, start_time,
end_time, create_time, update_time
)
values (#{expression,jdbcType=VARCHAR}, #{description,jdbcType=VARCHAR}, #{jobName,jdbcType=VARCHAR},
#{jobClassName,jdbcType=VARCHAR}, #{jobType,jdbcType=INTEGER}, #{jobData,jdbcType=VARCHAR},
#{isOneTime,jdbcType=BIT}, #{nextFireTime,jdbcType=TIMESTAMP}, #{prevFireTime,jdbcType=TIMESTAMP},
#{triggerState,jdbcType=VARCHAR}, #{triggerType,jdbcType=VARCHAR}, #{startTime,jdbcType=TIMESTAMP},
#{endTime,jdbcType=TIMESTAMP}, #{createTime,jdbcType=TIMESTAMP}, #{updateTime,jdbcType=TIMESTAMP}
)
</insert>
<insert id="insertSelective" parameterType="com.accompany.scheduler.model.QuartzJob">
<selectKey resultType="java.lang.Long" keyProperty="id" order="AFTER">
SELECT LAST_INSERT_ID()
</selectKey>
insert into quartz_job
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="expression != null">
expression,
</if>
<if test="description != null">
description,
</if>
<if test="jobName != null">
job_name,
</if>
<if test="jobClassName != null">
job_class_name,
</if>
<if test="jobType != null">
job_type,
</if>
<if test="jobData != null">
job_data,
</if>
<if test="isOneTime != null">
is_one_time,
</if>
<if test="nextFireTime != null">
next_fire_time,
</if>
<if test="prevFireTime != null">
prev_fire_time,
</if>
<if test="triggerState != null">
trigger_state,
</if>
<if test="triggerType != null">
trigger_type,
</if>
<if test="startTime != null">
start_time,
</if>
<if test="endTime != null">
end_time,
</if>
<if test="createTime != null">
create_time,
</if>
<if test="updateTime != null">
update_time,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="expression != null">
#{expression,jdbcType=VARCHAR},
</if>
<if test="description != null">
#{description,jdbcType=VARCHAR},
</if>
<if test="jobName != null">
#{jobName,jdbcType=VARCHAR},
</if>
<if test="jobClassName != null">
#{jobClassName,jdbcType=VARCHAR},
</if>
<if test="jobType != null">
#{jobType,jdbcType=INTEGER},
</if>
<if test="jobData != null">
#{jobData,jdbcType=VARCHAR},
</if>
<if test="isOneTime != null">
#{isOneTime,jdbcType=BIT},
</if>
<if test="nextFireTime != null">
#{nextFireTime,jdbcType=TIMESTAMP},
</if>
<if test="prevFireTime != null">
#{prevFireTime,jdbcType=TIMESTAMP},
</if>
<if test="triggerState != null">
#{triggerState,jdbcType=VARCHAR},
</if>
<if test="triggerType != null">
#{triggerType,jdbcType=VARCHAR},
</if>
<if test="startTime != null">
#{startTime,jdbcType=TIMESTAMP},
</if>
<if test="endTime != null">
#{endTime,jdbcType=TIMESTAMP},
</if>
<if test="createTime != null">
#{createTime,jdbcType=TIMESTAMP},
</if>
<if test="updateTime != null">
#{updateTime,jdbcType=TIMESTAMP},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.accompany.scheduler.model.QuartzJob">
update quartz_job
<set>
<if test="expression != null">
expression = #{expression,jdbcType=VARCHAR},
</if>
<if test="description != null">
description = #{description,jdbcType=VARCHAR},
</if>
<if test="jobName != null">
job_name = #{jobName,jdbcType=VARCHAR},
</if>
<if test="jobClassName != null">
job_class_name = #{jobClassName,jdbcType=VARCHAR},
</if>
<if test="jobType != null">
job_type = #{jobType,jdbcType=INTEGER},
</if>
<if test="jobData != null">
job_data = #{jobData,jdbcType=VARCHAR},
</if>
<if test="isOneTime != null">
is_one_time = #{isOneTime,jdbcType=BIT},
</if>
<if test="nextFireTime != null">
next_fire_time = #{nextFireTime,jdbcType=TIMESTAMP},
</if>
<if test="prevFireTime != null">
prev_fire_time = #{prevFireTime,jdbcType=TIMESTAMP},
</if>
<if test="triggerState != null">
trigger_state = #{triggerState,jdbcType=VARCHAR},
</if>
<if test="triggerType != null">
trigger_type = #{triggerType,jdbcType=VARCHAR},
</if>
<if test="startTime != null">
start_time = #{startTime,jdbcType=TIMESTAMP},
</if>
<if test="endTime != null">
end_time = #{endTime,jdbcType=TIMESTAMP},
</if>
<if test="createTime != null">
create_time = #{createTime,jdbcType=TIMESTAMP},
</if>
<if test="updateTime != null">
update_time = #{updateTime,jdbcType=TIMESTAMP},
</if>
</set>
where id = #{id,jdbcType=BIGINT}
</update>
<update id="update" parameterType="com.accompany.scheduler.model.QuartzJob">
update quartz_job
set expression = #{expression,jdbcType=VARCHAR},
description = #{description,jdbcType=VARCHAR},
job_name = #{jobName,jdbcType=VARCHAR},
job_class_name = #{jobClassName,jdbcType=VARCHAR},
job_type = #{jobType,jdbcType=INTEGER},
job_data = #{jobData,jdbcType=VARCHAR},
is_one_time = #{isOneTime,jdbcType=BIT},
next_fire_time = #{nextFireTime,jdbcType=TIMESTAMP},
prev_fire_time = #{prevFireTime,jdbcType=TIMESTAMP},
trigger_state = #{triggerState,jdbcType=VARCHAR},
trigger_type = #{triggerType,jdbcType=VARCHAR},
start_time = #{startTime,jdbcType=TIMESTAMP},
end_time = #{endTime,jdbcType=TIMESTAMP},
create_time = #{createTime,jdbcType=TIMESTAMP},
update_time = #{updateTime,jdbcType=TIMESTAMP}
where id = #{id,jdbcType=BIGINT}
</update>
<select id="getPeriodic" resultMap="BaseResultMap" parameterType="java.lang.String">
select
<include refid="Base_Column_List"/>
from QRTZ_TRIGGERS qt
inner join QRTZ_JOB_DETAILS qjd on qt.job_name = qjd.job_name
inner join QRTZ_CRON_TRIGGERS qct on qct.trigger_name = qt.trigger_name
inner join quartz_job qj on qj.job_name = qjd.job_name and qj.is_deleted = false and qj.is_one_time = false
where qrtz_job_details.job_name = #{jobName,jdbcType=VARCHAR}
</select>
<select id="getOneTime" resultMap="BaseResultMap" parameterType="java.lang.String">
select
<include refid="Base_Column_List"/>
FROM quartz_job qj
LEFT JOIN QRTZ_JOB_DETAILS details ON details.job_name = qj.job_name
LEFT JOIN QRTZ_TRIGGERS qt ON qt.job_name = details.job_name
LEFT JOIN QRTZ_SIMPLE_TRIGGERS qst ON qst.trigger_name = qt.trigger_name
WHERE qj.is_deleted = false
AND qj.is_one_time = true
ORDER BY qj.create_time DESC
AND qj.job_name = #{jobName,jdbcType=VARCHAR}
</select>
<select id="getJobNameById" parameterType="java.lang.Long" resultType="java.lang.String">
select
job_name
from quartz_job
where id = #{id,jdbcType=BIGINT}
</select>
<update id="deleteByName" parameterType="java.lang.String">
delete from quartz_job where job_name = #{_parameter}
</update>
<select id="getByName" resultMap="BaseResultMap" parameterType="java.lang.String">
select
<include refid="Base_Column_List"/>
from QRTZ_TRIGGERS qt
inner join QRTZ_JOB_DETAILS qjd on qt.job_name = qjd.job_name
inner join QRTZ_CRON_TRIGGERS qct on qct.trigger_name = qt.trigger_name
inner join quartz_job qj on qj.job_name = qjd.job_name and qj.is_deleted = false and qj.is_one_time = false
where qjd.job_name = #{jobName,jdbcType=VARCHAR}
</select>
<select id="countPeriodic" resultType="java.lang.Integer">
select count(*) from quartz_job where is_deleted = false and is_one_time = false
</select>
<select id="countOneTime" resultType="java.lang.Integer">
select count(*) from quartz_job where is_deleted = false and is_one_time = true
</select>
<select id="selectPeriodicByPage" resultMap="BaseResultMap" parameterType="map">
select
<include refid="Base_Column_List"/>
from QRTZ_TRIGGERS qt
inner join QRTZ_JOB_DETAILS qjd on qt.job_name = qjd.job_name
inner join QRTZ_CRON_TRIGGERS qct on qct.trigger_name = qt.trigger_name
inner join quartz_job qj on qj.job_name = qjd.job_name and qj.is_deleted = false and qj.is_one_time = false
limit #{start},#{count}
</select>
<select id="selectOneTimeByPage" resultMap="BaseResultMap" parameterType="map">
select
<include refid="Base_Column_List"/>
FROM quartz_job qj
LEFT JOIN QRTZ_JOB_DETAILS details ON details.job_name = qj.job_name
LEFT JOIN QRTZ_TRIGGERS qt ON qt.job_name = details.job_name
LEFT JOIN QRTZ_SIMPLE_TRIGGERS qst ON qst.trigger_name = qt.trigger_name
WHERE qj.is_deleted = false
AND qj.is_one_time = true
ORDER BY qj.create_time DESC
limit #{start}, #{count}
</select>
</mapper>

View File

@@ -1,122 +0,0 @@
package com.accompany.scheduler.controller;
import com.accompany.common.result.BusiResult;
import com.accompany.scheduler.constant.QuartzJobType;
import com.accompany.scheduler.job.HttpGetJobDetail;
import com.accompany.scheduler.job.SqlJobDetail;
import com.accompany.scheduler.model.QuartzJob;
import com.accompany.scheduler.service.PeriodQuartzJobService;
import com.accompany.scheduler.service.SimpleQuartzJobService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* @author yangming
* @since 2020-03-30
* 定时任务
*/
@RestController
@RequestMapping("scheduler/api")
@Slf4j
public class PublicApiController {
@Autowired
private PeriodQuartzJobService periodQuartzJobService;
@Autowired
private SimpleQuartzJobService simpleQuartzJobService;
/**
* 删除一次性任务
*
* @param taskId
*/
@RequestMapping(value = "/cancelSimple")
public BusiResult cancelSimple(@RequestParam("taskId") Long taskId) throws Exception{
simpleQuartzJobService.delete(taskId);
return new BusiResult(true);
}
/**
* 删除周期性任务
*
* @param taskId
*/
@RequestMapping(value = "/cancelPeriod")
public BusiResult cancelPeriod(@RequestParam("taskId") Long taskId) throws Exception{
periodQuartzJobService.delete(taskId);
return new BusiResult(true);
}
/**
* 添加周期性任务
*/
@RequestMapping(value = "periodic")
public BusiResult periodic(@RequestParam("jobType") String jobType,
@RequestParam("jobParams") String jobParams,
@RequestParam("triggerValue") String triggerValue,
String className) throws Exception{
QuartzJob job = buildJob(false, jobType, jobParams, triggerValue, className);
job = periodQuartzJobService.add(job);
return new BusiResult(job.getId());
}
/**
* 添加一次性任务
*/
@RequestMapping(value = "simple")
public BusiResult simple(@RequestParam("jobType") String jobType,
@RequestParam("jobParams") String jobParams,
@RequestParam("triggerValue") String triggerValue,
String className) throws Exception{
QuartzJob job = buildJob(true, jobType, jobParams, triggerValue, className);
job = simpleQuartzJobService.add(job);
return new BusiResult(job.getId());
}
/**
* 根据参数构造任务对象
*
* @param isOneTime
* @param jobType
* @param jobParams
* @param scheduleExpression
* @param className
* @return
*/
public QuartzJob buildJob(boolean isOneTime, String jobType, String jobParams, String scheduleExpression, String className) {
QuartzJob job = new QuartzJob();
if (jobType.equalsIgnoreCase("SqlJob")) {
job.setJobName("SqlJob_" + System.currentTimeMillis());
job.setJobClassName(SqlJobDetail.class.getName());
job.setIsOneTime(isOneTime);
job.setJobType(QuartzJobType.SqlJob.value);
job.setJobData(jobParams);
job.setExpression(scheduleExpression);
job.setDescription("SqlJob");
} else if (jobType.equalsIgnoreCase("JavaJob")) {
job.setJobName("JavaJob_" + System.currentTimeMillis());
job.setJobClassName(className);
job.setIsOneTime(isOneTime);
job.setJobType(QuartzJobType.JavaJobClass.value);
job.setExpression(scheduleExpression);
job.setJobData(jobParams);
job.setDescription("JavaJobClass");
} else if (jobType.equalsIgnoreCase("HttpGetJob")) {
job.setJobName("HttpGetJob_" + System.currentTimeMillis());
job.setJobClassName(HttpGetJobDetail.class.getName());
job.setIsOneTime(isOneTime);
job.setJobType(QuartzJobType.HttpGetJob.value);
job.setJobData(jobParams);
job.setExpression(scheduleExpression);
job.setDescription("HttpGetJob");
} else {
throw new IllegalArgumentException("Unrecognized job type.");
}
return job;
}
}

View File

@@ -1,48 +0,0 @@
package com.accompany.scheduler.controller;
import com.accompany.common.result.BusiResult;
import com.accompany.scheduler.model.QuartzJob;
import com.accompany.scheduler.service.PeriodQuartzJobService;
import com.accompany.scheduler.service.SimpleQuartzJobService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @author yangming
* @date 2020-03-30
*/
@RestController
@RequestMapping("scheduler/quartz")
@Slf4j
public class QuartzSchedulerController {
@Autowired
PeriodQuartzJobService periodQuartzJobService;
@Autowired
SimpleQuartzJobService simpleQuartzJobService;
@RequestMapping(value = "periodList", method = RequestMethod.GET)
public BusiResult periodList(@RequestParam("page") Integer page,
@RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
if (page == null || page <= 0) {
page = 1;
}
List<QuartzJob> jobList = periodQuartzJobService.findPeriodByPage(page, pageSize);
return new BusiResult(jobList);
}
@RequestMapping(value = "oneTimeList", method = RequestMethod.GET)
public BusiResult oneTimeList(@RequestParam("page") Integer page,
@RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
if (page == null || page <= 0) {
page = 1;
}
List<QuartzJob> jobList = simpleQuartzJobService.findOneTimeByPage(page, pageSize);
return new BusiResult(jobList);
}
}

View File

@@ -13,7 +13,6 @@
<artifactId>accompany-scheduler</artifactId>
<packaging>pom</packaging>
<modules>
<module>accompany-scheduler-sdk</module>
<module>accompany-scheduler-service</module>
<module>accompany-scheduler-web</module>
</modules>