feat: init定时任务
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
package com.cnbm.scheduletask.service;
|
||||
|
||||
import com.cnbm.common.page.PageData;
|
||||
import com.cnbm.common.service.BaseService;
|
||||
import com.cnbm.scheduletask.dto.ScheduleJobLogDTO;
|
||||
import com.cnbm.scheduletask.entity.ScheduleJobLogEntity;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @Author weihongyang
|
||||
* @Date 2022/6/23 4:44 PM
|
||||
* @Version 1.0
|
||||
*/
|
||||
public interface ScheduleJobLogService extends BaseService<ScheduleJobLogEntity> {
|
||||
|
||||
PageData<ScheduleJobLogDTO> page(Map<String, Object> params);
|
||||
|
||||
ScheduleJobLogDTO get(Long id);
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package com.cnbm.scheduletask.service;
|
||||
|
||||
import com.cnbm.common.page.PageData;
|
||||
import com.cnbm.common.service.BaseService;
|
||||
import com.cnbm.scheduletask.dto.ScheduleJobDTO;
|
||||
import com.cnbm.scheduletask.entity.ScheduleJobEntity;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @Author weihongyang
|
||||
* @Date 2022/6/23 4:43 PM
|
||||
* @Version 1.0
|
||||
*/
|
||||
public interface ScheduleJobService extends BaseService<ScheduleJobEntity> {
|
||||
|
||||
PageData<ScheduleJobDTO> page(Map<String, Object> params);
|
||||
|
||||
ScheduleJobDTO get(Long id);
|
||||
|
||||
/**
|
||||
* 保存定时任务
|
||||
*/
|
||||
void save(ScheduleJobDTO dto);
|
||||
|
||||
/**
|
||||
* 更新定时任务
|
||||
*/
|
||||
void update(ScheduleJobDTO dto);
|
||||
|
||||
/**
|
||||
* 批量删除定时任务
|
||||
*/
|
||||
void deleteBatch(Long[] ids);
|
||||
|
||||
/**
|
||||
* 批量更新定时任务状态
|
||||
*/
|
||||
int updateBatch(Long[] ids, int status);
|
||||
|
||||
/**
|
||||
* 立即执行
|
||||
*/
|
||||
void run(Long[] ids);
|
||||
|
||||
/**
|
||||
* 暂停运行
|
||||
*/
|
||||
void pause(Long[] ids);
|
||||
|
||||
/**
|
||||
* 恢复运行
|
||||
*/
|
||||
void resume(Long[] ids);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
package com.cnbm.scheduletask.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.cnbm.common.constant.Constant;
|
||||
import com.cnbm.common.page.PageData;
|
||||
import com.cnbm.common.service.impl.BaseServiceImpl;
|
||||
import com.cnbm.common.utils.ConvertUtils;
|
||||
import com.cnbm.scheduletask.dao.ScheduleJobLogDao;
|
||||
import com.cnbm.scheduletask.dto.ScheduleJobLogDTO;
|
||||
import com.cnbm.scheduletask.entity.ScheduleJobLogEntity;
|
||||
import com.cnbm.scheduletask.service.ScheduleJobLogService;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @Author weihongyang
|
||||
* @Date 2022/6/23 4:46 PM
|
||||
* @Version 1.0
|
||||
*/
|
||||
@Service
|
||||
public class ScheduleJobLogServiceImpl extends BaseServiceImpl<ScheduleJobLogDao, ScheduleJobLogEntity> implements ScheduleJobLogService {
|
||||
|
||||
@Override
|
||||
public PageData<ScheduleJobLogDTO> page(Map<String, Object> params) {
|
||||
IPage<ScheduleJobLogEntity> page = baseDao.selectPage(
|
||||
getPage(params, Constant.CREATE_DATE, false),
|
||||
getWrapper(params)
|
||||
);
|
||||
return getPageData(page, ScheduleJobLogDTO.class);
|
||||
}
|
||||
|
||||
private QueryWrapper<ScheduleJobLogEntity> getWrapper(Map<String, Object> params){
|
||||
String jobId = (String)params.get("jobId");
|
||||
|
||||
QueryWrapper<ScheduleJobLogEntity> wrapper = new QueryWrapper<>();
|
||||
wrapper.eq(StringUtils.isNotBlank(jobId), "job_id", jobId);
|
||||
|
||||
return wrapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScheduleJobLogDTO get(Long id) {
|
||||
ScheduleJobLogEntity entity = baseDao.selectById(id);
|
||||
|
||||
return ConvertUtils.sourceToTarget(entity, ScheduleJobLogDTO.class);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
package com.cnbm.scheduletask.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.cnbm.common.constant.Constant;
|
||||
import com.cnbm.common.page.PageData;
|
||||
import com.cnbm.common.service.impl.BaseServiceImpl;
|
||||
import com.cnbm.common.utils.ConvertUtils;
|
||||
import com.cnbm.scheduletask.dao.ScheduleJobDao;
|
||||
import com.cnbm.scheduletask.dto.ScheduleJobDTO;
|
||||
import com.cnbm.scheduletask.entity.ScheduleJobEntity;
|
||||
import com.cnbm.scheduletask.service.ScheduleJobService;
|
||||
import com.cnbm.scheduletask.utils.ScheduleUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.quartz.Scheduler;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @Author weihongyang
|
||||
* @Date 2022/6/23 4:45 PM
|
||||
* @Version 1.0
|
||||
*/
|
||||
@Service
|
||||
public class ScheduleJobServiceImpl extends BaseServiceImpl<ScheduleJobDao, ScheduleJobEntity> implements ScheduleJobService {
|
||||
@Autowired
|
||||
private Scheduler scheduler;
|
||||
|
||||
@Override
|
||||
public PageData<ScheduleJobDTO> page(Map<String, Object> params) {
|
||||
IPage<ScheduleJobEntity> page = baseDao.selectPage(
|
||||
getPage(params, Constant.CREATE_DATE, false),
|
||||
getWrapper(params)
|
||||
);
|
||||
return getPageData(page, ScheduleJobDTO.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ScheduleJobDTO get(Long id) {
|
||||
ScheduleJobEntity entity = baseDao.selectById(id);
|
||||
|
||||
return ConvertUtils.sourceToTarget(entity, ScheduleJobDTO.class);
|
||||
}
|
||||
|
||||
private QueryWrapper<ScheduleJobEntity> getWrapper(Map<String, Object> params){
|
||||
String beanName = (String)params.get("beanName");
|
||||
|
||||
QueryWrapper<ScheduleJobEntity> wrapper = new QueryWrapper<>();
|
||||
wrapper.like(StringUtils.isNotBlank(beanName), "bean_name", beanName);
|
||||
|
||||
return wrapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void save(ScheduleJobDTO dto) {
|
||||
ScheduleJobEntity entity = ConvertUtils.sourceToTarget(dto, ScheduleJobEntity.class);
|
||||
|
||||
entity.setStatus(Constant.ScheduleStatus.NORMAL.getValue());
|
||||
this.insert(entity);
|
||||
|
||||
ScheduleUtils.createScheduleJob(scheduler, entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void update(ScheduleJobDTO dto) {
|
||||
ScheduleJobEntity entity = ConvertUtils.sourceToTarget(dto, ScheduleJobEntity.class);
|
||||
|
||||
ScheduleUtils.updateScheduleJob(scheduler, entity);
|
||||
|
||||
this.updateById(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void deleteBatch(Long[] ids) {
|
||||
for(Long id : ids){
|
||||
ScheduleUtils.deleteScheduleJob(scheduler, id);
|
||||
}
|
||||
|
||||
//删除数据
|
||||
this.deleteBatchIds(Arrays.asList(ids));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int updateBatch(Long[] ids, int status){
|
||||
Map<String, Object> map = new HashMap<>(2);
|
||||
map.put("ids", ids);
|
||||
map.put("status", status);
|
||||
return baseDao.updateBatch(map);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void run(Long[] ids) {
|
||||
for(Long id : ids){
|
||||
ScheduleUtils.run(scheduler, this.selectById(id));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void pause(Long[] ids) {
|
||||
for(Long id : ids){
|
||||
ScheduleUtils.pauseJob(scheduler, id);
|
||||
}
|
||||
|
||||
updateBatch(ids, Constant.ScheduleStatus.PAUSE.getValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void resume(Long[] ids) {
|
||||
for(Long id : ids){
|
||||
ScheduleUtils.resumeJob(scheduler, id);
|
||||
}
|
||||
|
||||
updateBatch(ids, Constant.ScheduleStatus.NORMAL.getValue());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user